Skip to content

Commit

Permalink
fix(collect): improve line splits in between output
Browse files Browse the repository at this point in the history
  • Loading branch information
EdJoPaTo committed Aug 26, 2024
1 parent 17d6c5e commit 6ef6e2c
Show file tree
Hide file tree
Showing 2 changed files with 29 additions and 19 deletions.
46 changes: 28 additions & 18 deletions src/harness.rs
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
use std::io::Write;
use std::num::NonZeroUsize;
use std::path::Path;
use std::process::ExitStatus;
use std::process::{ExitStatus, Output};
use std::sync::atomic::{AtomicBool, Ordering};
use std::time::Duration;

Expand All @@ -15,6 +15,7 @@ pub struct Config {
no_header: bool,
result: CommandResult,

first: AtomicBool,
need_linesplit: AtomicBool,
}

Expand All @@ -33,6 +34,7 @@ impl Config {
no_header,
result,

first: AtomicBool::new(true),
need_linesplit: AtomicBool::new(false),
}
}
Expand All @@ -56,8 +58,8 @@ impl<'a> Harness<'a> {
if self.config.multithreaded || self.config.no_header {
return;
}
let need_linesplit = self.config.need_linesplit.swap(true, Ordering::Relaxed);
if need_linesplit {
let first = self.config.first.swap(false, Ordering::Relaxed);
if !first {
println!();
}
println!("{}", self.path());
Expand All @@ -68,30 +70,38 @@ impl<'a> Harness<'a> {
format!("{:width$} ", self.path())
}

pub fn collect(&self, stdout: &[u8], stderr: &[u8]) {
let any_output = !stdout.is_empty() || !stderr.is_empty();

let need_linesplit = self
.config
.need_linesplit
.swap(any_output, Ordering::Relaxed);
if need_linesplit || any_output {
println!();
pub fn collect(&self, output: &Output) {
let has_output = !output.stdout.is_empty() || !output.stderr.is_empty();
let show_result = self.config.result.print(output.status.success());
if !has_output && !show_result {
return;
}
let has_any_harness =
!self.config.no_header || !matches!(self.config.result, CommandResult::Never);
if has_any_harness {
let first = self.config.first.swap(false, Ordering::Relaxed);
let need_linesplit = self
.config
.need_linesplit
.swap(has_output, Ordering::Relaxed);
if need_linesplit || (has_output && !first) {
println!();
}
}
if let Some(last) = stdout.last() {
if let Some(last) = output.stdout.last() {
if !self.config.no_header {
println!("{} Stdout:", self.path());
println!("{} stdout:", self.path());
}
std::io::stdout().write_all(stdout).unwrap();
std::io::stdout().write_all(&output.stdout).unwrap();
if *last != b'\n' {
std::io::stdout().write_all(b"\n").unwrap();
}
}
if let Some(last) = stderr.last() {
if let Some(last) = output.stderr.last() {
if !self.config.no_header {
eprintln!("{} Stderr:", self.path());
eprintln!("{} stderr:", self.path());
}
std::io::stderr().write_all(stderr).unwrap();
std::io::stderr().write_all(&output.stderr).unwrap();
if *last != b'\n' {
std::io::stderr().write_all(b"\n").unwrap();
}
Expand Down
2 changes: 1 addition & 1 deletion src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -63,7 +63,7 @@ fn main() {
CommandOutput::Collect => {
let (output, took) = command.output();
let _stdout = std::io::stdout().lock();
harness.collect(&output.stdout, &output.stderr);
harness.collect(&output);
harness.result(took, output.status);
}
CommandOutput::Null => {
Expand Down

0 comments on commit 6ef6e2c

Please sign in to comment.