Add color to compare output.
All checks were successful
rust-test Build rust-test has succeeded
rust-build Build rust-build has succeeded
rustfmt Build rustfmt has succeeded

This commit is contained in:
Tom Alexander 2023-08-25 04:09:52 -04:00
parent 19432d91ab
commit 5134cece7b
Signed by: talexander
GPG Key ID: D3A179C9A53C0EDE

View File

@ -84,12 +84,24 @@ impl DiffResult {
match self.status { match self.status {
DiffStatus::Good => { DiffStatus::Good => {
if self.has_bad_children() { if self.has_bad_children() {
"BADCHILD" format!(
"{color}BADCHILD{reset}",
color = DiffResult::foreground_color(255, 255, 0),
reset = DiffResult::reset_color(),
)
} else { } else {
"GOOD" format!(
"{color}GOOD{reset}",
color = DiffResult::foreground_color(0, 255, 0),
reset = DiffResult::reset_color(),
)
} }
} }
DiffStatus::Bad => "BAD", DiffStatus::Bad => format!(
"{color}BAD{reset}",
color = DiffResult::foreground_color(255, 0, 0),
reset = DiffResult::reset_color(),
),
} }
}; };
println!( println!(
@ -117,6 +129,29 @@ impl DiffResult {
DiffStatus::Bad => true, DiffStatus::Bad => true,
} }
} }
fn foreground_color(red: u8, green: u8, blue: u8) -> String {
format!(
"\x1b[38;2;{red};{green};{blue}m",
red = red,
green = green,
blue = blue
)
}
#[allow(dead_code)]
fn background_color(red: u8, green: u8, blue: u8) -> String {
format!(
"\x1b[48;2;{red};{green};{blue}m",
red = red,
green = green,
blue = blue
)
}
fn reset_color() -> &'static str {
"\x1b[0m"
}
} }
fn compare_element<'s>( fn compare_element<'s>(