43 lines
906 B
Rust
43 lines
906 B
Rust
|
|
use std::borrow::Cow;
|
||
|
|
|
||
|
|
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 {
|
||
|
|
""
|
||
|
|
}
|
||
|
|
}
|