chronological terminal result (error and output interlaced)

This commit is contained in:
Penwing 2024-01-27 12:34:55 +01:00
parent fa2104b9de
commit 8436619c0e
6 changed files with 737 additions and 707 deletions

View file

@ -45,3 +45,4 @@ Async terminal !
Real Ui
# 1.1.0 :
Better error handling

View file

@ -130,12 +130,10 @@ impl Calcifer {
format!("\n{} {}", entry.env, entry.command),
);
ui.end_row();
if !entry.output.is_empty() {
ui.colored_label(entry_color, &entry.output);
ui.end_row();
}
if !entry.error.is_empty() {
ui.colored_label(super::RED, &entry.error);
for line in &entry.result {
let color =
if line.error { super::RED } else { entry_color };
ui.colored_label(color, &line.text);
ui.end_row();
}
}
@ -214,7 +212,9 @@ impl Calcifer {
});
ui.separator();
if self.project_mode && self.tabs[self.selected_tab.to_index()].language == PROJECT_EXTENSION {
if self.project_mode
&& self.tabs[self.selected_tab.to_index()].language == PROJECT_EXTENSION
{
self.draw_project_file(ui);
} else {
self.draw_code_file(ui);

View file

@ -176,7 +176,7 @@ impl Calcifer {
for (index, tab) in self.tabs.clone().iter().enumerate() {
if tab.path == path {
self.selected_tab = tools::TabNumber::from_index(index);
return
return;
}
}
}

View file

@ -25,7 +25,7 @@ mod build {
use build::SAVE_PATH;
use build::TITLE;
const PROJECT_EXTENSION : &str = "project";
const PROJECT_EXTENSION: &str = "project";
const TERMINAL_HEIGHT: f32 = 200.0;
const TERMINAL_RANGE: Range<f32> = 100.0..500.0;
const RED: egui::Color32 = egui::Color32::from_rgb(235, 108, 99);

View file

@ -4,7 +4,7 @@ use nix::fcntl::FcntlArg;
use nix::fcntl::OFlag;
use std::io::BufRead;
use std::io::BufReader;
use std::io::Read;
//use std::io::Read;
use std::os::fd::AsRawFd;
use std::process::Stdio;
use std::{env, path::Path, path::PathBuf, process::Command};
@ -14,44 +14,62 @@ pub struct Buffer {
pub error_buffer: BufReader<std::process::ChildStderr>,
}
pub struct Line {
pub text: String,
pub error: bool,
}
impl Line {
fn output(text: String) -> Self {
Self {
text: remove_line_break(text),
error: false,
}
}
fn error(text: String) -> Self {
Self {
text: remove_line_break(text),
error: true,
}
}
}
pub struct CommandEntry {
pub env: String,
pub command: String,
pub output: String,
pub error: String,
pub result: Vec<Line>,
pub buffer: Option<Buffer>,
}
impl CommandEntry {
pub fn new(env: String, command: String) -> Self {
let (buffer, error) = match execute(command.clone()) {
Ok(command_buffer) => (Some(command_buffer), String::new()),
Err(err) => (None, format!("failed to get results: {}", err)),
let (buffer, result) = match execute(command.clone()) {
Ok(command_buffer) => (Some(command_buffer), vec![]),
Err(err) => (
None,
vec![Line::error(format!("failed to get results: {}", err))],
),
};
CommandEntry {
env,
command,
output: String::new(),
error,
result,
buffer,
}
}
pub fn update(&mut self) {
if let Some(buffer) = &mut self.buffer {
for line in buffer.output_buffer.by_ref().lines() {
match line {
Ok(line) => self.output += &format!("{}\n", line),
Err(_) => return,
}
}
for line in buffer.error_buffer.by_ref().lines() {
match line {
Ok(line) => self.error += &format!("{}\n", line),
Err(_) => return,
let mut output = String::new();
let _ = buffer.output_buffer.read_line(&mut output);
if !remove_line_break(output.to_string()).is_empty() {
self.result.push(Line::output(format!("{}\n", output)));
}
let mut error = String::new();
let _ = buffer.error_buffer.read_line(&mut error);
if !remove_line_break(error.to_string()).is_empty() {
self.result.push(Line::error(format!("{}\n", error)));
}
}
}
@ -129,3 +147,14 @@ pub fn execute(command: String) -> Result<Buffer, std::io::Error> {
error_buffer,
})
}
fn remove_line_break(input: String) -> String {
let mut text = input.clone();
while text.ends_with('\n') {
text.pop();
if text.ends_with('\r') {
text.pop();
}
}
text
}