refactor: remove ansi_term dependency

This commit is contained in:
Amaan Qureshi 2024-05-25 19:59:50 -04:00
parent 35c0fad26f
commit c440f2a7c6
7 changed files with 164 additions and 181 deletions

View file

@ -7,7 +7,7 @@ use std::{
str,
};
use ansi_term::Colour;
use anstyle::{AnsiColor, Color, Style};
use anyhow::{anyhow, Context, Result};
use difference::{Changeset, Difference};
use indoc::indoc;
@ -240,7 +240,7 @@ pub fn get_tmp_test_file(target_test: u32, color: bool) -> Result<(PathBuf, Vec<
println!(
"{target_test}. {}\n",
opt_color(color, Colour::Green, test_name)
paint(color.then_some(AnsiColor::Green), test_name)
);
Ok((test_path, languages))
@ -270,8 +270,8 @@ pub fn check_queries_at_path(language: &Language, path: &Path) -> Result<()> {
pub fn print_diff_key() {
println!(
"\ncorrect / {} / {}",
Colour::Green.paint("expected"),
Colour::Red.paint("unexpected")
paint(Some(AnsiColor::Green), "expected"),
paint(Some(AnsiColor::Red), "unexpected")
);
}
@ -288,14 +288,14 @@ pub fn print_diff(actual: &str, expected: &str, use_color: bool) {
}
Difference::Add(part) => {
if use_color {
print!("{}{}", Colour::Green.paint(part), changeset.split);
println!("{}{}", paint(Some(AnsiColor::Green), part), changeset.split);
} else {
print!("expected:\n{part}{}", changeset.split);
}
}
Difference::Rem(part) => {
if use_color {
print!("{}{}", Colour::Red.paint(part), changeset.split);
println!("{}{}", paint(Some(AnsiColor::Red), part), changeset.split);
} else {
print!("unexpected:\n{part}{}", changeset.split);
}
@ -305,12 +305,9 @@ pub fn print_diff(actual: &str, expected: &str, use_color: bool) {
println!();
}
pub fn opt_color(use_color: bool, color: ansi_term::Colour, text: &str) -> String {
if use_color {
color.paint(text).to_string()
} else {
text.to_string()
}
pub fn paint(color: Option<AnsiColor>, text: &str) -> String {
let style = Style::new().fg_color(color.map(Color::Ansi));
format!("{style}{text}{style:#}")
}
/// This will return false if we want to "fail fast". It will bail and not parse any more tests.
@ -340,7 +337,7 @@ fn run_tests(
println!(
"{:>3}.  {}",
opts.test_num,
opt_color(opts.color, Colour::Yellow, &name),
paint(opts.color.then_some(AnsiColor::Yellow), &name),
);
return Ok(true);
}
@ -349,7 +346,7 @@ fn run_tests(
println!(
"{:>3}.  {}",
opts.test_num,
opt_color(opts.color, Colour::Purple, &name)
paint(opts.color.then_some(AnsiColor::Magenta), &name),
);
return Ok(true);
}
@ -369,13 +366,13 @@ fn run_tests(
println!(
"{:>3}.  {}",
opts.test_num,
opt_color(opts.color, Colour::Green, &name)
paint(opts.color.then_some(AnsiColor::Green), &name)
);
} else {
println!(
"{:>3}.  {}",
opts.test_num,
opt_color(opts.color, Colour::Red, &name)
paint(opts.color.then_some(AnsiColor::Red), &name)
);
}
@ -392,7 +389,7 @@ fn run_tests(
println!(
"{:>3}. ✓ {}",
opts.test_num,
opt_color(opts.color, Colour::Green, &name),
paint(opts.color.then_some(AnsiColor::Green), &name)
);
if opts.update {
let input = String::from_utf8(input.clone()).unwrap();
@ -438,14 +435,14 @@ fn run_tests(
println!(
"{:>3}. ✓ {}",
opts.test_num,
opt_color(opts.color, Colour::Blue, &name)
paint(opts.color.then_some(AnsiColor::Blue), &name),
);
}
} else {
println!(
"{:>3}. ✗ {}",
opts.test_num,
opt_color(opts.color, Colour::Red, &name)
paint(opts.color.then_some(AnsiColor::Red), &name),
);
}
failures.push((name.clone(), actual, output.clone()));