Skip to content

Commit

Permalink
feat(util): revert log simplification
Browse files Browse the repository at this point in the history
  • Loading branch information
madonuko committed Nov 5, 2024
1 parent 18b83ad commit 650aacd
Showing 1 changed file with 9 additions and 8 deletions.
17 changes: 9 additions & 8 deletions src/util.rs
Original file line number Diff line number Diff line change
Expand Up @@ -79,24 +79,24 @@ pub trait CommandLog {
#[async_trait::async_trait]
impl CommandLog for Command {
async fn log(&mut self) -> Result<()> {
fn print_log(output: &str, out: ConsoleOut) {
fn print_log(process: &str, output: &str, out: ConsoleOut) {
// check if no_color is set
let no_color = std::env::var("NO_COLOR").is_ok();

let process = {
if no_color {
style("| ")
style(process)
} else {
match out {
ConsoleOut::Stdout => style("│ ").cyan(),
ConsoleOut::Stderr => style("│ ").yellow(),
ConsoleOut::Stdout => style(process).cyan(),
ConsoleOut::Stderr => style(process).yellow(),
}
}
};

let output = output.replace('\r', &format!("\r{process}"));
let output = output.replace('\r', &format!("\r{process}"));

println!("{process}{output}");
println!("{process}{output}");
}

// make process name a constant string that we can reuse every time we call print_log
Expand Down Expand Up @@ -131,6 +131,7 @@ impl CommandLog for Command {
})?;

// HACK: Rust ownership is very fun.
let t = process.clone();

let stdout = output.stdout.take().unwrap();
let mut stdout_lines = tokio::io::BufReader::new(stdout).lines();
Expand All @@ -142,13 +143,13 @@ impl CommandLog for Command {
for task in [
tokio::spawn(async move {
while let Some(line) = stdout_lines.next_line().await.unwrap() {
print_log(&line, ConsoleOut::Stdout);
print_log(&t, &line, ConsoleOut::Stdout);
}
Ok(())
}),
tokio::spawn(async move {
while let Some(line) = stderr_lines.next_line().await.unwrap() {
print_log(&line, ConsoleOut::Stderr);
print_log(&process, &line, ConsoleOut::Stderr);
}
Ok(())
}),
Expand Down

0 comments on commit 650aacd

Please sign in to comment.