2026-03-05 16:19:06 +01:00
|
|
|
// SPDX-FileCopyrightText: 2026 Qyriad <qyriad@qyriad.me>
|
|
|
|
|
//
|
|
|
|
|
// SPDX-License-Identifier: EUPL-1.1
|
|
|
|
|
|
2026-02-16 18:02:39 +01:00
|
|
|
use std::{
|
|
|
|
|
env,
|
|
|
|
|
sync::{LazyLock, OnceLock},
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
#[allow(unused_imports)]
|
|
|
|
|
use crate::prelude::*;
|
|
|
|
|
|
|
|
|
|
/// The actual, final value for whether color should be used, based on CLI and environment values.
|
|
|
|
|
pub static SHOULD_COLOR: LazyLock<bool> = LazyLock::new(|| is_clicolor_forced() || is_color_reqd());
|
|
|
|
|
|
|
|
|
|
/// Initialized from the `--color` value from the CLI, along with `io::stdin().is_terminal()`.
|
|
|
|
|
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)
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/// 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)
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
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()
|
|
|
|
|
}));
|
|
|
|
|
|
|
|
|
|
pub(crate) const ANSI_CYAN: _LazyLockDisplay = _LazyLockDisplay(LazyLock::new(|| {
|
|
|
|
|
SHOULD_COLOR.then_some("\x1b[36m").unwrap_or_default()
|
|
|
|
|
}));
|
|
|
|
|
|
|
|
|
|
pub(crate) const ANSI_RESET: _LazyLockDisplay = _LazyLockDisplay(LazyLock::new(|| {
|
|
|
|
|
SHOULD_COLOR.then_some("\x1b[0m").unwrap_or_default()
|
|
|
|
|
}));
|