restore old files now!
This commit is contained in:
parent
76b5ac628d
commit
dfdf027bc6
11 changed files with 1676 additions and 16 deletions
53
src/color.rs
Normal file
53
src/color.rs
Normal file
|
|
@ -0,0 +1,53 @@
|
|||
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()
|
||||
}));
|
||||
Loading…
Add table
Add a link
Reference in a new issue