mirror of
https://github.com/NotAShelf/microfetch.git
synced 2025-11-26 01:02:50 +00:00
Signed-off-by: NotAShelf <raf@notashelf.dev> Change-Id: I517d855b14c015569a325deb64948f3b6a6a6964
81 lines
1.9 KiB
Rust
81 lines
1.9 KiB
Rust
use std::{env, sync::LazyLock};
|
|
|
|
pub struct Colors {
|
|
pub reset: &'static str,
|
|
pub blue: &'static str,
|
|
pub cyan: &'static str,
|
|
pub green: &'static str,
|
|
pub yellow: &'static str,
|
|
pub red: &'static str,
|
|
pub magenta: &'static str,
|
|
}
|
|
|
|
impl Colors {
|
|
const fn new(is_no_color: bool) -> Self {
|
|
if is_no_color {
|
|
Self {
|
|
reset: "",
|
|
blue: "",
|
|
cyan: "",
|
|
green: "",
|
|
yellow: "",
|
|
red: "",
|
|
magenta: "",
|
|
}
|
|
} else {
|
|
Self {
|
|
reset: "\x1b[0m",
|
|
blue: "\x1b[34m",
|
|
cyan: "\x1b[36m",
|
|
green: "\x1b[32m",
|
|
yellow: "\x1b[33m",
|
|
red: "\x1b[31m",
|
|
magenta: "\x1b[35m",
|
|
}
|
|
}
|
|
}
|
|
}
|
|
|
|
pub static COLORS: LazyLock<Colors> = LazyLock::new(|| {
|
|
// Check for NO_COLOR once at startup
|
|
let is_no_color = env::var("NO_COLOR").is_ok();
|
|
Colors::new(is_no_color)
|
|
});
|
|
|
|
#[must_use]
|
|
#[cfg_attr(feature = "hotpath", hotpath::measure)]
|
|
pub fn print_dots() -> String {
|
|
// Pre-calculate capacity: 6 color codes + " " (glyph + 2 spaces) per color
|
|
const GLYPH: &str = "";
|
|
let capacity = COLORS.blue.len()
|
|
+ COLORS.cyan.len()
|
|
+ COLORS.green.len()
|
|
+ COLORS.yellow.len()
|
|
+ COLORS.red.len()
|
|
+ COLORS.magenta.len()
|
|
+ COLORS.reset.len()
|
|
+ (GLYPH.len() + 2) * 6;
|
|
|
|
let mut result = String::with_capacity(capacity);
|
|
result.push_str(COLORS.blue);
|
|
result.push_str(GLYPH);
|
|
result.push_str(" ");
|
|
result.push_str(COLORS.cyan);
|
|
result.push_str(GLYPH);
|
|
result.push_str(" ");
|
|
result.push_str(COLORS.green);
|
|
result.push_str(GLYPH);
|
|
result.push_str(" ");
|
|
result.push_str(COLORS.yellow);
|
|
result.push_str(GLYPH);
|
|
result.push_str(" ");
|
|
result.push_str(COLORS.red);
|
|
result.push_str(GLYPH);
|
|
result.push_str(" ");
|
|
result.push_str(COLORS.magenta);
|
|
result.push_str(GLYPH);
|
|
result.push_str(" ");
|
|
result.push_str(COLORS.reset);
|
|
|
|
result
|
|
}
|