#![allow(dead_code)]
use std::borrow::Cow;
use std::env;
use std::sync::LazyLock;
#[cfg(not(tarpaulin_include))]
#[allow(unreachable_code)]
pub static NO_COLOR: LazyLock<bool> = LazyLock::new(|| {
#[cfg(test)]
{
return false;
}
env::var_os("NO_COLOR").is_some_and(|v| !v.is_empty())
});
pub const GREEN: &str = "\x1b[0;92m";
pub const YELLOW: &str = "\x1b[0;93m";
pub const RED: &str = "\x1b[0;91m";
pub const BLUE: &str = "\x1b[0;94m";
pub const CYAN: &str = "\x1b[0;96m";
pub const RESET: &str = "\x1b[0m";
pub const HIGHLIGHT: &str = GREEN;
pub const ATTENUATE: &str = "\x1b[0;90m";
pub const BOLD: &str = "\x1b[1m";
pub const ITALIC: &str = "\x1b[3m";
pub const UNDERLINE: &str = "\x1b[4m";
pub struct Color;
impl Color {
#[must_use]
pub fn error(string: &str) -> Cow<str> {
Self::color(RED, string)
}
#[must_use]
pub fn warning(string: &str) -> Cow<str> {
Self::color(YELLOW, string)
}
#[must_use]
pub fn none(string: &str) -> Cow<str> {
Cow::Borrowed(string)
}
#[must_use]
fn color<'a>(color: &str, string: &'a str) -> Cow<'a, str> {
if *NO_COLOR {
#[cfg(not(tarpaulin_include))] return Cow::Borrowed(string);
}
Cow::Owned(format!("{color}{string}{RESET}"))
}
#[must_use]
pub fn maybe_color(color: &str) -> &str {
if *NO_COLOR {
#[cfg(not(tarpaulin_include))] return "";
}
color
}
}
#[cfg(test)]
mod tests {
use super::*;
#[test]
fn color_error_is_red() {
assert_eq!(
Color::error("this is an error"),
"\x1b[0;91mthis is an error\x1b[0m"
);
}
#[test]
fn color_warning_is_yellow() {
assert_eq!(
Color::warning("this is a warning"),
"\x1b[0;93mthis is a warning\x1b[0m"
);
}
#[test]
fn color_none_has_no_effect() {
assert_eq!(Color::none("same as input"), "same as input");
}
}