hexz_cli/ui/color.rs
1//! ANSI color palette for CLI output.
2//!
3//! Returns populated escape sequences when stdout is an interactive terminal
4//! and `NO_COLOR` is unset; otherwise returns empty strings so output remains
5//! plain for pipes, redirects, and CI environments.
6
7use std::io::IsTerminal;
8
9/// A set of ANSI escape sequences (or empty strings when color is disabled).
10#[derive(Debug)]
11pub struct Palette {
12 /// Bold text.
13 pub bold: &'static str,
14 /// Dim text.
15 pub dim: &'static str,
16 /// Reset all attributes.
17 pub reset: &'static str,
18 /// Bright cyan — used for label keys.
19 pub cyan: &'static str,
20 /// Bright green — used for file sizes and success status.
21 pub green: &'static str,
22 /// Bright yellow — used for annotations and scalar values.
23 pub yellow: &'static str,
24 /// Dark gray — used for tree chrome and auxiliary info.
25 pub gray: &'static str,
26 /// Bright red — used for error/failure status.
27 pub red: &'static str,
28}
29
30static COLORS: Palette = Palette {
31 bold: "\x1b[1m",
32 dim: "\x1b[2m",
33 reset: "\x1b[0m",
34 cyan: "\x1b[96m",
35 green: "\x1b[92m",
36 yellow: "\x1b[93m",
37 gray: "\x1b[90m",
38 red: "\x1b[91m",
39};
40
41static PLAIN: Palette = Palette {
42 bold: "",
43 dim: "",
44 reset: "",
45 cyan: "",
46 green: "",
47 yellow: "",
48 gray: "",
49 red: "",
50};
51
52/// Returns the color palette appropriate for stdout.
53///
54/// Emits ANSI codes if stdout is a terminal and `NO_COLOR` is not set;
55/// otherwise every field is an empty string.
56pub fn palette() -> &'static Palette {
57 if std::io::stdout().is_terminal() && std::env::var_os("NO_COLOR").is_none() {
58 &COLORS
59 } else {
60 &PLAIN
61 }
62}