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
4 changed files with 67 additions and 58 deletions

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 {
""
}
}