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 Real Ui
# 1.1.0 : # 1.1.0 :
Better error handling

View file

@ -130,12 +130,10 @@ impl Calcifer {
format!("\n{} {}", entry.env, entry.command), format!("\n{} {}", entry.env, entry.command),
); );
ui.end_row(); ui.end_row();
if !entry.output.is_empty() { for line in &entry.result {
ui.colored_label(entry_color, &entry.output); let color =
ui.end_row(); if line.error { super::RED } else { entry_color };
} ui.colored_label(color, &line.text);
if !entry.error.is_empty() {
ui.colored_label(super::RED, &entry.error);
ui.end_row(); ui.end_row();
} }
} }
@ -214,7 +212,9 @@ impl Calcifer {
}); });
ui.separator(); 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); self.draw_project_file(ui);
} else { } else {
self.draw_code_file(ui); self.draw_code_file(ui);

View file

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

View file

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