From 255230207df20c8495d18fa8b0ff1cac55c78ba9 Mon Sep 17 00:00:00 2001 From: Andriamanitra Date: Mon, 22 Apr 2024 15:11:08 +0300 Subject: [PATCH 1/3] support NO_COLOR environment variable --- src/clash/test_case.rs | 10 ++------ src/formatter.rs | 19 ++++++++------ src/main.rs | 53 ++++++++++++---------------------------- src/outputstyle.rs | 34 ++++++++++++++++++++++++++ src/solution/test_run.rs | 24 +++++++++--------- 5 files changed, 74 insertions(+), 66 deletions(-) diff --git a/src/clash/test_case.rs b/src/clash/test_case.rs index c3b01060..9b90b8d4 100644 --- a/src/clash/test_case.rs +++ b/src/clash/test_case.rs @@ -52,16 +52,10 @@ impl TestCase { } pub fn styled_input(&self, ostyle: &OutputStyle) -> String { - match ostyle.input_whitespace { - Some(ws_style) => show_whitespace(&self.test_in, &ostyle.input, &ws_style), - None => ostyle.input.paint(&self.test_in).to_string(), - } + show_whitespace(&self.test_in, &ostyle.input, &ostyle.input_whitespace) } pub fn styled_output(&self, ostyle: &OutputStyle) -> String { - match ostyle.output_whitespace { - Some(ws_style) => show_whitespace(&self.test_out, &ostyle.output, &ws_style), - None => ostyle.output.paint(&self.test_out).to_string(), - } + show_whitespace(&self.test_out, &ostyle.output, &ostyle.output_whitespace) } } diff --git a/src/formatter.rs b/src/formatter.rs index 4a6ce685..1bb39e12 100644 --- a/src/formatter.rs +++ b/src/formatter.rs @@ -227,13 +227,18 @@ fn format_remove_excessive_newlines(text: &str) -> String { /// 1. Replaces spaces with • and newlines with ⏎. Paints them with `ws_style`. /// 2. Paints the rest with `style`. -pub fn show_whitespace(text: &str, style: &Style, ws_style: &Style) -> String { - let newl = format!("{}\n", ws_style.paint("⏎")); - let space = format!("{}", ws_style.paint("•")); - let fmt_non_ws = RE_NONWHITESPACE - .replace_all(text, |caps: ®ex::Captures| style.paint(&caps[0]).to_string()) - .to_string(); - fmt_non_ws.replace('\n', &newl).replace(' ', &space) +pub fn show_whitespace(text: &str, style: &Style, ws_style: &Option