1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
//! Demonstrates environment variable color detection with `detect_color_env()`.
//!
//! gilt respects a 5-tier priority chain of environment variables that
//! control whether terminal output is colored:
//!
//! 1. `NO_COLOR` — Any value disables color (https://no-color.org/)
//! 2. `FORCE_COLOR` — `0` = off, `1`/`2` = standard/256, `3` = truecolor
//! 3. `CLICOLOR_FORCE` — Any non-`"0"` value forces color on
//! 4. `CLICOLOR` — `"0"` disables color
//! 5. (none set) — Normal terminal auto-detection
//!
//! Try running with different env vars to see the effect:
//!
//! cargo run --example color_env
//! NO_COLOR=1 cargo run --example color_env
//! FORCE_COLOR=3 cargo run --example color_env
//! FORCE_COLOR=0 cargo run --example color_env
//! CLICOLOR=0 cargo run --example color_env
//! CLICOLOR_FORCE=1 cargo run --example color_env
use gilt::color_env::{detect_color_env, ColorEnvOverride};
use gilt::console::Console;
use gilt::rule::Rule;
use gilt::styled_str::Stylize;
fn main() {
// ── Detect the current override ──────────────────────────────────────
let detected = detect_color_env();
println!("Color environment detection result: {:?}", detected);
println!();
match detected {
ColorEnvOverride::NoColor => {
println!(" Color is DISABLED by an environment variable.");
println!(" (NO_COLOR, FORCE_COLOR=0, or CLICOLOR=0 is set)");
}
ColorEnvOverride::ForceColor => {
println!(" Color is FORCED ON (standard/256).");
println!(" (FORCE_COLOR=1/2 or CLICOLOR_FORCE is set)");
}
ColorEnvOverride::ForceColorTruecolor => {
println!(" Color is FORCED ON (truecolor/24-bit).");
println!(" (FORCE_COLOR=3 is set)");
}
ColorEnvOverride::None => {
println!(" No color override detected.");
println!(" Normal terminal auto-detection will be used.");
}
}
println!();
// ── Show styled output via Console ───────────────────────────────────
// The Console respects env vars automatically, so if NO_COLOR is set,
// the styled output below will appear without ANSI codes.
let mut console = Console::builder().width(72).force_terminal(true).build();
console.print(&Rule::with_title("Styled Output"));
console.print(&"This text should be bold and red".bold().red());
console.print(&"This text should be green".green());
console.print(&"This text should be blue with italic".blue().italic());
console.print(&Rule::with_title("Try It Yourself"));
// Print instructions for testing
let instructions = [
"Run with different environment variables:",
"",
" cargo run --example color_env # auto-detect",
" NO_COLOR=1 cargo run --example color_env # disable color",
" FORCE_COLOR=3 cargo run --example color_env # force truecolor",
" FORCE_COLOR=0 cargo run --example color_env # force off",
" CLICOLOR=0 cargo run --example color_env # disable via CLICOLOR",
" CLICOLOR_FORCE=1 cargo run --example color_env # force on",
];
for line in &instructions {
console.print_text(line);
}
}