Skip to content

Commit

Permalink
Merge pull request #44 from hanna-kruppe/anstream
Browse files Browse the repository at this point in the history
Switch from termcolor to anstream
  • Loading branch information
LukasKalbertodt authored Oct 5, 2024
2 parents af6e427 + 6097b93 commit 57d60db
Show file tree
Hide file tree
Showing 2 changed files with 24 additions and 26 deletions.
5 changes: 3 additions & 2 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ name = "libtest-mimic"
version = "0.7.3"
authors = ["Lukas Kalbertodt <[email protected]>"]
edition = "2021"
rust-version = "1.60"
rust-version = "1.65"

description = """
Write your own test harness that looks and behaves like the built-in test \
Expand All @@ -21,8 +21,9 @@ exclude = [".github"]
[dependencies]
clap = { version = "4.0.8", features = ["derive"] }
threadpool = "1.8.1"
termcolor = "1.0.5"
escape8259 = "0.5.2"
anstream = "0.6.14"
anstyle = "1.0.7"

[dev-dependencies]
fastrand = "1.8.0"
Expand Down
45 changes: 21 additions & 24 deletions src/printer.rs
Original file line number Diff line number Diff line change
Expand Up @@ -6,17 +6,18 @@
//! - `format` (and `quiet`)
//! - `logfile`
use std::{fs::File, time::Duration};
use std::{fs::File, io::Write, time::Duration};

use termcolor::{Ansi, Color, ColorChoice, ColorSpec, NoColor, StandardStream, WriteColor};
use anstream::AutoStream;
use anstyle::{AnsiColor, Color, Style};

use crate::{
Arguments, ColorSetting, Conclusion, Failed, FormatSetting, Measurement, Outcome, TestInfo,
Trial,
};

pub(crate) struct Printer {
out: Box<dyn WriteColor>,
out: Box<dyn Write>,
format: FormatSetting,
name_width: usize,
kind_width: usize,
Expand All @@ -29,20 +30,20 @@ impl Printer {
let color_arg = args.color.unwrap_or(ColorSetting::Auto);

// Determine target of all output
let out = if let Some(logfile) = &args.logfile {
let out: Box<dyn Write> = if let Some(logfile) = &args.logfile {
let f = File::create(logfile).expect("failed to create logfile");
if color_arg == ColorSetting::Always {
Box::new(Ansi::new(f)) as Box<dyn WriteColor>
Box::new(AutoStream::always(f))
} else {
Box::new(NoColor::new(f))
Box::new(AutoStream::never(f))
}
} else {
let choice = match color_arg {
ColorSetting::Auto => ColorChoice::Auto,
ColorSetting::Always => ColorChoice::Always,
ColorSetting::Never => ColorChoice::Never,
ColorSetting::Auto => anstream::ColorChoice::Auto,
ColorSetting::Always => anstream::ColorChoice::Always,
ColorSetting::Never => anstream::ColorChoice::Never,
};
Box::new(StandardStream::stdout(choice))
Box::new(AutoStream::new(std::io::stdout(), choice))
};

// Determine correct format
Expand Down Expand Up @@ -160,9 +161,8 @@ impl Printer {
}
};

self.out.set_color(&color_of_outcome(outcome)).unwrap();
write!(self.out, "{}", c).unwrap();
self.out.reset().unwrap();
let style = color_of_outcome(outcome);
write!(self.out, "{style}{}{style:#}", c).unwrap();
}
FormatSetting::Json => {
if let Outcome::Measured(Measurement { avg, variance }) = outcome {
Expand Down Expand Up @@ -319,9 +319,8 @@ impl Printer {
Outcome::Measured { .. } => "bench",
};

self.out.set_color(&color_of_outcome(outcome)).unwrap();
write!(self.out, "{}", s).unwrap();
self.out.reset().unwrap();
let style = color_of_outcome(outcome);
write!(self.out, "{style}{}{style:#}", s).unwrap();

if let Outcome::Measured(Measurement { avg, variance }) = outcome {
write!(
Expand All @@ -347,14 +346,12 @@ pub fn fmt_with_thousand_sep(mut v: u64) -> String {
}

/// Returns the `ColorSpec` associated with the given outcome.
fn color_of_outcome(outcome: &Outcome) -> ColorSpec {
let mut out = ColorSpec::new();
fn color_of_outcome(outcome: &Outcome) -> Style {
let color = match outcome {
Outcome::Passed => Color::Green,
Outcome::Failed { .. } => Color::Red,
Outcome::Ignored => Color::Yellow,
Outcome::Measured { .. } => Color::Cyan,
Outcome::Passed => AnsiColor::Green,
Outcome::Failed { .. } => AnsiColor::Red,
Outcome::Ignored => AnsiColor::Yellow,
Outcome::Measured { .. } => AnsiColor::Cyan,
};
out.set_fg(Some(color));
out
Style::new().fg_color(Some(Color::Ansi(color)))
}

0 comments on commit 57d60db

Please sign in to comment.