From 87ad274c73ebb35b82b6eedd0ce122109cb77e27 Mon Sep 17 00:00:00 2001 From: Ed Page Date: Thu, 28 Sep 2023 15:47:35 -0500 Subject: [PATCH] refactor(test): Switch termcolor to anstream --- Cargo.lock | 7 ++-- Cargo.toml | 4 +-- crates/cargo-test-support/Cargo.toml | 3 +- crates/cargo-test-support/src/diff.rs | 48 +++++++++++---------------- 4 files changed, 28 insertions(+), 34 deletions(-) diff --git a/Cargo.lock b/Cargo.lock index 14e79442141c..74c2086670ee 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -39,9 +39,9 @@ dependencies = [ [[package]] name = "anstyle" -version = "1.0.3" +version = "1.0.4" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "b84bf0a05bbb2a83e5eb6fa36bb6e87baa08193c35ff52bbf6b38d8af2890e46" +checksum = "7079075b41f533b8c61d2a4d073c4676e1f8b249ff94a393b0595db304e0dd87" [[package]] name = "anstyle-parse" @@ -394,6 +394,8 @@ version = "0.1.0" name = "cargo-test-support" version = "0.1.0" dependencies = [ + "anstream", + "anstyle", "anyhow", "cargo-test-macro", "cargo-util", @@ -408,7 +410,6 @@ dependencies = [ "serde_json", "snapbox", "tar", - "termcolor", "time", "toml", "url", diff --git a/Cargo.toml b/Cargo.toml index dc603dd1fefc..73c11a0a5bbd 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -16,8 +16,8 @@ edition = "2021" license = "MIT OR Apache-2.0" [workspace.dependencies] -anstream = { version = "0.6.0", default-features = false } -anstyle = "1.0.3" +anstream = "0.6.0" +anstyle = "1.0.4" anstyle-termcolor = "1.1.0" anyhow = "1.0.75" base64 = "0.21.3" diff --git a/crates/cargo-test-support/Cargo.toml b/crates/cargo-test-support/Cargo.toml index cfcf139de21a..fc32e1c9cbac 100644 --- a/crates/cargo-test-support/Cargo.toml +++ b/crates/cargo-test-support/Cargo.toml @@ -10,6 +10,8 @@ publish = false doctest = false [dependencies] +anstream.workspace = true +anstyle.workspace = true anyhow.workspace = true cargo-test-macro.workspace = true cargo-util.workspace = true @@ -24,7 +26,6 @@ serde = { workspace = true, features = ["derive"] } serde_json.workspace = true snapbox.workspace = true tar.workspace = true -termcolor.workspace = true time.workspace = true toml.workspace = true url.workspace = true diff --git a/crates/cargo-test-support/src/diff.rs b/crates/cargo-test-support/src/diff.rs index f3b283b109f0..202a6e6a3402 100644 --- a/crates/cargo-test-support/src/diff.rs +++ b/crates/cargo-test-support/src/diff.rs @@ -7,7 +7,6 @@ use std::fmt; use std::io::Write; -use termcolor::{Ansi, Color, ColorSpec, NoColor, WriteColor}; /// A single line change to be applied to the original. #[derive(Debug, Eq, PartialEq)] @@ -111,42 +110,35 @@ where } pub fn render_colored_changes(changes: &[Change]) -> String { - // termcolor is not very ergonomic, but I don't want to bring in another dependency. - let mut red = ColorSpec::new(); - red.set_fg(Some(Color::Red)); - let mut green = ColorSpec::new(); - green.set_fg(Some(Color::Green)); - let mut dim = ColorSpec::new(); - dim.set_dimmed(true); - let mut v = Vec::new(); - let mut result: Box = if crate::is_ci() { + // anstyle is not very ergonomic, but I don't want to bring in another dependency. + let red = anstyle::AnsiColor::Red.on_default().render(); + let green = anstyle::AnsiColor::Green.on_default().render(); + let dim = (anstyle::Style::new() | anstyle::Effects::DIMMED).render(); + let bold = (anstyle::Style::new() | anstyle::Effects::BOLD).render(); + let reset = anstyle::Reset.render(); + + let choice = if crate::is_ci() { // Don't use color on CI. Even though GitHub can display colors, it // makes reading the raw logs more difficult. - Box::new(NoColor::new(&mut v)) + anstream::ColorChoice::Never } else { - Box::new(Ansi::new(&mut v)) + anstream::AutoStream::choice(&std::io::stdout()) }; + let mut buffer = anstream::AutoStream::new(anstream::Buffer::new(), choice); for change in changes { let (nums, sign, color, text) = match change { - Change::Add(i, s) => (format!(" {:<4} ", i), '+', &green, s), - Change::Remove(i, s) => (format!("{:<4} ", i), '-', &red, s), - Change::Keep(x, y, s) => (format!("{:<4}{:<4} ", x, y), ' ', &dim, s), + Change::Add(i, s) => (format!(" {:<4} ", i), '+', green, s), + Change::Remove(i, s) => (format!("{:<4} ", i), '-', red, s), + Change::Keep(x, y, s) => (format!("{:<4}{:<4} ", x, y), ' ', dim, s), }; - result.set_color(&dim).unwrap(); - write!(result, "{}", nums).unwrap(); - let mut bold = color.clone(); - bold.set_bold(true); - result.set_color(&bold).unwrap(); - write!(result, "{}", sign).unwrap(); - result.reset().unwrap(); - result.set_color(&color).unwrap(); - write!(result, "{}", text).unwrap(); - result.reset().unwrap(); - writeln!(result).unwrap(); + write!( + buffer, + "{dim}{nums}{reset}{bold}{sign}{reset}{color}{text}{reset}" + ) + .unwrap(); } - drop(result); - String::from_utf8(v).unwrap() + String::from_utf8(buffer.into_inner().as_bytes().to_owned()).unwrap() } #[cfg(test)]