2026-01-27 11:29:10 +01:00
|
|
|
use std::{
|
|
|
|
|
env,
|
|
|
|
|
sync::{LazyLock, OnceLock},
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
#[allow(unused_imports)]
|
|
|
|
|
use crate::prelude::*;
|
|
|
|
|
|
|
|
|
|
pub static CLI_ENABLE_COLOR: OnceLock<bool> = OnceLock::new();
|
|
|
|
|
|
|
|
|
|
fn is_color_reqd() -> bool {
|
|
|
|
|
CLI_ENABLE_COLOR.get().copied().unwrap_or(false)
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
fn is_clicolor_forced() -> bool {
|
|
|
|
|
env::var("CLICOLOR_FORCE")
|
|
|
|
|
.map(|value| {
|
|
|
|
|
if value.is_empty() || value == "0" {
|
|
|
|
|
false
|
|
|
|
|
} else {
|
|
|
|
|
true
|
|
|
|
|
}
|
|
|
|
|
})
|
|
|
|
|
.unwrap_or(false)
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
pub static SHOULD_COLOR: LazyLock<bool> = LazyLock::new(|| is_clicolor_forced() || is_color_reqd());
|
|
|
|
|
|
|
|
|
|
/// Silly wrapper around LazyLock<&'static str> to impl Display.
|
|
|
|
|
pub(crate) struct _LazyLockDisplay(LazyLock<&'static str>);
|
|
|
|
|
impl Display for _LazyLockDisplay {
|
|
|
|
|
fn fmt(&self, f: &mut Formatter) -> FmtResult {
|
|
|
|
|
Display::fmt(&*self.0, f)
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2026-01-27 17:20:04 +01:00
|
|
|
pub(crate) const ANSI_GREEN: _LazyLockDisplay = _LazyLockDisplay(LazyLock::new(|| {
|
|
|
|
|
SHOULD_COLOR.then_some("\x1b[32m").unwrap_or_default()
|
|
|
|
|
}));
|
|
|
|
|
|
|
|
|
|
pub(crate) const ANSI_MAGENTA: _LazyLockDisplay = _LazyLockDisplay(LazyLock::new(|| {
|
|
|
|
|
SHOULD_COLOR.then_some("\x1b[35m").unwrap_or_default()
|
|
|
|
|
}));
|
|
|
|
|
|
2026-01-27 11:29:10 +01:00
|
|
|
pub(crate) const ANSI_CYAN: _LazyLockDisplay = _LazyLockDisplay(LazyLock::new(|| {
|
2026-01-27 17:20:04 +01:00
|
|
|
SHOULD_COLOR.then_some("\x1b[36m").unwrap_or_default()
|
2026-01-27 11:29:10 +01:00
|
|
|
}));
|
|
|
|
|
|
|
|
|
|
pub(crate) const ANSI_RESET: _LazyLockDisplay = _LazyLockDisplay(LazyLock::new(|| {
|
|
|
|
|
SHOULD_COLOR
|
|
|
|
|
// C'mon rustfmt, just format it to match ^.
|
|
|
|
|
.then_some("\x1b[0m")
|
|
|
|
|
.unwrap_or_default()
|
|
|
|
|
}));
|