Move terminal colors to the shared util module.

This commit is contained in:
Tom Alexander 2023-12-27 10:57:40 -05:00
parent 4bfea41291
commit 3cb251ea6c
Signed by: talexander
GPG Key ID: D3A179C9A53C0EDE
4 changed files with 67 additions and 58 deletions

View File

@ -9,7 +9,9 @@ use crate::parser::parse_file_with_settings;
use crate::parser::parse_with_settings;
use crate::util::emacs_parse_anonymous_org_document;
use crate::util::emacs_parse_file_org_document;
use crate::util::foreground_color;
use crate::util::print_versions;
use crate::util::reset_color;
pub async fn run_anonymous_compare<P: AsRef<str>>(
org_contents: P,
@ -67,8 +69,8 @@ pub async fn run_anonymous_compare_with_settings<'g, 's, P: AsRef<str>>(
} else if !silent {
println!(
"{color}Entire document passes.{reset}",
color = DiffResult::foreground_color(0, 255, 0),
reset = DiffResult::reset_color(),
color = foreground_color(0, 255, 0),
reset = reset_color(),
);
}
@ -120,8 +122,8 @@ pub async fn run_compare_on_file_with_settings<'g, 's, P: AsRef<Path>>(
} else if !silent {
println!(
"{color}Entire document passes.{reset}",
color = DiffResult::foreground_color(0, 255, 0),
reset = DiffResult::reset_color(),
color = foreground_color(0, 255, 0),
reset = reset_color(),
);
}

View File

@ -109,6 +109,8 @@ use crate::types::Verbatim;
use crate::types::VerseBlock;
use crate::types::WarningDelayType;
use crate::types::Year;
use crate::util::foreground_color;
use crate::util::reset_color;
#[derive(Debug)]
pub enum DiffEntry<'b, 's> {
@ -200,21 +202,21 @@ impl<'b, 's> DiffResult<'b, 's> {
if self.has_bad_children() {
format!(
"{color}BADCHILD{reset}",
color = DiffResult::foreground_color(255, 255, 0),
reset = DiffResult::reset_color(),
color = foreground_color(255, 255, 0),
reset = reset_color(),
)
} else {
format!(
"{color}GOOD{reset}",
color = DiffResult::foreground_color(0, 255, 0),
reset = DiffResult::reset_color(),
color = foreground_color(0, 255, 0),
reset = reset_color(),
)
}
}
DiffStatus::Bad => format!(
"{color}BAD{reset}",
color = DiffResult::foreground_color(255, 0, 0),
reset = DiffResult::reset_color(),
color = foreground_color(255, 0, 0),
reset = reset_color(),
),
}
};
@ -239,45 +241,6 @@ impl<'b, 's> DiffResult<'b, 's> {
.iter()
.any(|child| child.is_immediately_bad() || child.has_bad_children())
}
pub(crate) fn foreground_color(red: u8, green: u8, blue: u8) -> String {
if DiffResult::should_use_color() {
format!(
"\x1b[38;2;{red};{green};{blue}m",
red = red,
green = green,
blue = blue
)
} else {
String::new()
}
}
#[allow(dead_code)]
pub(crate) fn background_color(red: u8, green: u8, blue: u8) -> String {
if DiffResult::should_use_color() {
format!(
"\x1b[48;2;{red};{green};{blue}m",
red = red,
green = green,
blue = blue
)
} else {
String::new()
}
}
pub(crate) fn reset_color() -> &'static str {
if DiffResult::should_use_color() {
"\x1b[0m"
} else {
""
}
}
fn should_use_color() -> bool {
!std::env::var("NO_COLOR").is_ok_and(|val| !val.is_empty())
}
}
impl<'b, 's> DiffLayer<'b, 's> {
@ -295,14 +258,14 @@ impl<'b, 's> DiffLayer<'b, 's> {
let status_text = if self.has_bad_children() {
format!(
"{color}BADCHILD{reset}",
color = DiffResult::foreground_color(255, 255, 0),
reset = DiffResult::reset_color(),
color = foreground_color(255, 255, 0),
reset = reset_color(),
)
} else {
format!(
"{color}GOOD{reset}",
color = DiffResult::foreground_color(0, 255, 0),
reset = DiffResult::reset_color(),
color = foreground_color(0, 255, 0),
reset = reset_color(),
)
};
println!(

View File

@ -1,3 +1,4 @@
use std::borrow::Cow;
use std::path::Path;
use tokio::process::Command;
@ -189,3 +190,44 @@ fn global_settings_elisp(global_settings: &GlobalSettings) -> String {
}
ret
}
fn should_use_color() -> bool {
!std::env::var("NO_COLOR").is_ok_and(|val| !val.is_empty())
}
pub(crate) fn foreground_color(red: u8, green: u8, blue: u8) -> Cow<'static, str> {
if should_use_color() {
format!(
"\x1b[38;2;{red};{green};{blue}m",
red = red,
green = green,
blue = blue
)
.into()
} else {
Cow::from("")
}
}
#[allow(dead_code)]
pub(crate) fn background_color(red: u8, green: u8, blue: u8) -> Cow<'static, str> {
if should_use_color() {
format!(
"\x1b[48;2;{red};{green};{blue}m",
red = red,
green = green,
blue = blue
)
.into()
} else {
Cow::from("")
}
}
pub(crate) fn reset_color() -> &'static str {
if should_use_color() {
"\x1b[0m"
} else {
""
}
}

View File

@ -3,7 +3,9 @@ use crate::compare::sexp;
use crate::context::GlobalSettings;
use crate::parser::parse_with_settings;
use crate::util::emacs_parse_anonymous_org_document;
use crate::util::foreground_color;
use crate::util::print_versions;
use crate::util::reset_color;
use crate::wasm::ToWasm;
use crate::wasm::ToWasmContext;
@ -48,11 +50,11 @@ pub async fn wasm_run_anonymous_compare_with_settings<'g, 's, P: AsRef<str>>(
if diff_result.is_bad() {
return Ok(false);
} else if !silent {
// println!(
// "{color}Entire document passes.{reset}",
// color = WasmDiffResult::foreground_color(0, 255, 0),
// reset = WasmDiffResult::reset_color(),
// );
println!(
"{color}Entire document passes.{reset}",
color = foreground_color(0, 255, 0),
reset = reset_color(),
);
}
Ok(true)