Skip to main content

heddle_cli_render/cli/
style.rs

1// SPDX-License-Identifier: Apache-2.0
2//! Tasteful terminal styling for Heddle CLI output.
3//!
4//! Heddle's brand voice ("precise, calm, conversational") translates
5//! to a deliberately restrained terminal palette: dim/bright contrast
6//! and bold weight do most of the structural work; saturated color
7//! appears only at semantic seams (success/warning/error,
8//! confidence band, identity vs. id). No rainbow output, no syntax
9//! highlighting density.
10//!
11//! Color decisions are made **once** at CLI startup via
12//! [`init_from_cli`], which consults — in precedence order:
13//!
14//! 1. `--no-color` CLI flag (force off)
15//! 2. `NO_COLOR` env var (per <https://no-color.org>) — force off
16//! 3. `CLICOLOR_FORCE=1` env var — force on, even on a non-TTY
17//! 4. stdout isatty — auto-detected default
18//!
19//! The decision is stored in a process-wide [`OnceLock`] so render
20//! sites consult a `bool` rather than re-querying the environment per
21//! line. JSON output is *always* uncolored; that decision happens at
22//! the print site, not here — `should_output_json` short-circuits
23//! before any styled helper runs.
24
25use std::{
26    io::IsTerminal,
27    sync::atomic::{AtomicI8, Ordering},
28};
29
30use anstyle::{Color, Style};
31use heddle_cli_args::Cli;
32
33/// Process-wide gate, encoded as a tristate atomic so tests can
34/// override the value freely without rebuilding the cell.
35///
36/// - `0`  — uninitialized (treat as "color off" so we never leak
37///   escapes into log files when `init_from_cli` was skipped)
38/// - `1`  — color enabled
39/// - `-1` — color disabled (explicit)
40///
41/// Atomic-relaxed is sufficient: the value is set once at startup
42/// before any rendering begins, and tests use a single thread.
43static COLOR_STATE: AtomicI8 = AtomicI8::new(0);
44
45const STATE_OFF: i8 = -1;
46const STATE_ON: i8 = 1;
47
48/// Resolve the color decision once at CLI startup.
49///
50/// Subsequent calls overwrite the previous decision — tests need
51/// this so they can flip the gate mid-process. Production only
52/// calls this once, from `main`.
53pub fn init_from_cli(cli: &Cli) {
54    let enabled = decide_color_enabled(cli, &EnvProbe::real());
55    COLOR_STATE.store(
56        if enabled { STATE_ON } else { STATE_OFF },
57        Ordering::Relaxed,
58    );
59}
60
61/// Returns the active color decision. If `init_from_cli` was never
62/// called (e.g. in a library test that bypasses `main`), this
63/// defaults to `false` to avoid leaking escapes.
64pub fn color_enabled() -> bool {
65    COLOR_STATE.load(Ordering::Relaxed) == STATE_ON
66}
67
68/// Test-only override. Use this from any test that wants to assert
69/// styled or unstyled output without depending on the ambient TTY
70/// state.
71#[cfg(any(test, feature = "test-utils"))]
72pub fn force_for_test(enabled: bool) {
73    COLOR_STATE.store(
74        if enabled { STATE_ON } else { STATE_OFF },
75        Ordering::Relaxed,
76    );
77}
78
79/// Tiny env-var indirection so the decision logic stays unit-testable
80/// without touching the real environment. Each closure-style accessor
81/// returns the env value if set; `EnvProbe::real()` is the only
82/// production constructor, but tests can build a literal struct.
83struct EnvProbe<'a> {
84    no_color: Option<&'a str>,
85    clicolor_force: Option<&'a str>,
86    is_tty: bool,
87}
88
89impl EnvProbe<'_> {
90    fn real() -> EnvProbe<'static> {
91        // We leak these strings deliberately — they live for the
92        // duration of one decision call and are never observed
93        // afterwards. The alternative (`String`) would require
94        // generic lifetimes that aren't worth the complexity here.
95        let no_color = std::env::var("NO_COLOR").ok().map(|s| {
96            let leaked: &'static str = Box::leak(s.into_boxed_str());
97            leaked
98        });
99        let clicolor_force = std::env::var("CLICOLOR_FORCE").ok().map(|s| {
100            let leaked: &'static str = Box::leak(s.into_boxed_str());
101            leaked
102        });
103        EnvProbe {
104            no_color,
105            clicolor_force,
106            is_tty: std::io::stdout().is_terminal(),
107        }
108    }
109}
110
111fn decide_color_enabled(cli: &Cli, env: &EnvProbe<'_>) -> bool {
112    // 1. Explicit CLI flag wins. The user typed `--no-color`; honour
113    //    it regardless of any env var.
114    if cli.no_color {
115        return false;
116    }
117    // 2. `NO_COLOR` is the cross-tool standard
118    //    (<https://no-color.org>). Any non-empty value disables.
119    if let Some(v) = env.no_color
120        && !v.is_empty()
121    {
122        return false;
123    }
124    // 3. `CLICOLOR_FORCE=1` is the conventional escape hatch for
125    //    pipes that want color preserved (e.g. piping to `less -R`).
126    //    We require literal "1" to match the convention used by
127    //    `ls`, `grep`, and bat.
128    if let Some(v) = env.clicolor_force
129        && v == "1"
130    {
131        return true;
132    }
133    // 4. Otherwise: color iff stdout is an interactive TTY.
134    env.is_tty
135}
136
137// =====================================================================
138// Palette
139// =====================================================================
140//
141// Brand calls for warm/technical, never the saturated 16-color
142// defaults. We use anstyle's 8-bit (256-color) palette to land on
143// muted, deliberate hues:
144//
145// - `accent`: ANSI 8-bit 71 — a warm sage/green, used for success,
146//   "current", and confidence ≥ 0.9. Cooler than 34 (lime) and warmer
147//   than 28 (forest); reads well on both light and dark terminals.
148// - `warn`:   ANSI 8-bit 178 — a warm amber, mid-warning. Avoids the
149//   safety-vest 220 (yellow) and the orange 208 which reads as error.
150// - `error`:  ANSI 8-bit 167 — a muted rust/terracotta. Cooler and
151//   more deliberate than the default red 9; signals failure without
152//   shouting.
153// - `dim`:    standard "faint" weight — terminal-theme aware, since
154//   8-bit grays clash with light backgrounds.
155// - `bold`:   standard bold weight, no color shift.
156
157const ACCENT_COLOR: Color = Color::Ansi256(anstyle::Ansi256Color(71));
158const WARN_COLOR: Color = Color::Ansi256(anstyle::Ansi256Color(178));
159const ERROR_COLOR: Color = Color::Ansi256(anstyle::Ansi256Color(167));
160
161fn accent_style() -> Style {
162    Style::new().fg_color(Some(ACCENT_COLOR))
163}
164
165fn warn_style() -> Style {
166    Style::new().fg_color(Some(WARN_COLOR))
167}
168
169fn error_style() -> Style {
170    Style::new().fg_color(Some(ERROR_COLOR))
171}
172
173fn dim_style() -> Style {
174    Style::new().dimmed()
175}
176
177fn bold_style() -> Style {
178    Style::new().bold()
179}
180
181// =====================================================================
182// Helpers
183// =====================================================================
184//
185// All helpers return `String`. We could return `impl Display` to
186// avoid the allocation, but `Style` doesn't implement `Display` on its
187// own — it expects a wrapped payload — and the call-site ergonomics
188// (passing into `format!`/`println!`) are cleaner with a concrete
189// `String`. Cost is one heap allocation per styled fragment, which
190// is negligible against the syscall cost of writing to a terminal.
191
192fn paint(style: Style, s: &str) -> String {
193    if !color_enabled() {
194        return s.to_string();
195    }
196    format!("{}{}{}", style.render(), s, style.render_reset())
197}
198
199/// Success/positive/current — warm sage/green (ANSI 8-bit 71).
200pub fn accent(s: &str) -> String {
201    paint(accent_style(), s)
202}
203
204/// Mid-warning — warm amber (ANSI 8-bit 178).
205pub fn warn(s: &str) -> String {
206    paint(warn_style(), s)
207}
208
209/// Hard error — muted rust (ANSI 8-bit 167).
210pub fn error(s: &str) -> String {
211    paint(error_style(), s)
212}
213
214/// De-emphasis — used for IDs, timestamps, paths, and other text
215/// that's structurally important but shouldn't draw the eye.
216pub fn dim(s: &str) -> String {
217    paint(dim_style(), s)
218}
219
220/// Structural emphasis — intent text, headers, the principal name.
221pub fn bold(s: &str) -> String {
222    paint(bold_style(), s)
223}
224
225/// Section heading used for human output blocks.
226pub fn section(s: &str) -> String {
227    bold(s)
228}
229
230/// Small successful status marker. Keep the word short so it scans
231/// like a status glyph but still works in plain terminals.
232pub fn ok_marker() -> String {
233    accent("[ok]")
234}
235
236/// Small in-progress status marker.
237pub fn working_marker() -> String {
238    warn("[working]")
239}
240
241/// Small warning status marker.
242pub fn warn_marker() -> String {
243    warn("[warn]")
244}
245
246/// Small failure status marker.
247pub fn error_marker() -> String {
248    error("[error]")
249}
250
251/// Render a calm label/value row.
252pub fn field(label: &str, value: &str) -> String {
253    format!("{} {}", dim(&format!("{label}:")), value)
254}
255
256/// Render a compact count with the number emphasized.
257pub fn count(value: usize, noun: &str) -> String {
258    let suffix = if value == 1 { "" } else { "s" };
259    format!("{} {noun}{suffix}", bold(&value.to_string()))
260}
261
262/// Confidence band: maps the recorded numeric value to a semantic
263/// color. Render the formatted text yourself (e.g. via
264/// `format_confidence`) and pass it here; this keeps the formatting
265/// rule in `repo` and the styling rule here.
266pub fn confidence(value: Option<f32>, formatted: &str) -> String {
267    match value {
268        None => dim(formatted),
269        Some(v) if v >= 0.9 => accent(formatted),
270        Some(v) if v >= 0.75 => warn(formatted),
271        Some(_) => error(formatted),
272    }
273}
274
275/// Change-id styling: dim. We don't apply a monospace marker here —
276/// terminals already render text monospaced. The "dim+monospace"
277/// label in the spec was about *visual treatment*, which the
278/// terminal grants for free.
279pub fn state_id(id: &str) -> String {
280    dim(id)
281}
282
283/// Principal styling: name in bold, email dimmed. Returns the
284/// pre-composed `"Name <email>"` string so callers don't have to
285/// thread two fragments through `println!` arguments.
286pub fn principal(name: &str, email: &str) -> String {
287    if !color_enabled() {
288        return format!("{} <{}>", name, email);
289    }
290    format!("{} <{}>", bold(name), dim(email))
291}
292
293/// Thread-state styling: `active`/`ready`/`promoted` are accent;
294/// `merged`/`abandoned` are dim (historical, not current);
295/// `blocked`/`stale`/`draft` are warn. Unknown variants fall back
296/// to plain text. The matcher is case-insensitive against the
297/// `Display` form so callers can pass `state.to_string()` directly.
298pub fn thread_state(state: &str) -> String {
299    match state.to_ascii_lowercase().as_str() {
300        "active" | "ready" | "promoted" | "current" => accent(state),
301        "merged" | "abandoned" => dim(state),
302        "blocked" | "stale" | "draft" | "diverged" => warn(state),
303        _ => state.to_string(),
304    }
305}
306
307#[cfg(test)]
308mod tests {
309    use serial_test::serial;
310
311    use super::*;
312
313    /// All helpers must return ANSI-free strings when color is off.
314    /// Important: every render site relies on this — if the gate
315    /// regresses, escape codes leak into log files, JSON pipelines,
316    /// and test fixtures.
317    ///
318    /// Tests in this module touch a shared atomic (`COLOR_STATE`)
319    /// so we serialize them under a single name to keep one test's
320    /// `force_for_test` from racing another's read.
321    #[test]
322    #[serial(color_state)]
323    fn helpers_emit_no_ansi_when_disabled() {
324        force_for_test(false);
325        for s in [
326            accent("ok"),
327            warn("careful"),
328            error("boom"),
329            dim("hs-abc123"),
330            bold("Capture audit pipeline"),
331            confidence(Some(0.95), "0.95"),
332            confidence(None, "—"),
333            state_id("hs-abc123"),
334            principal("Ada Lovelace", "ada@analytical.engine"),
335            thread_state("active"),
336        ] {
337            assert!(!s.contains('\x1b'), "expected no ANSI escape in {:?}", s);
338        }
339    }
340
341    /// With color enabled, each helper emits an escape prefix.
342    #[test]
343    #[serial(color_state)]
344    fn helpers_emit_ansi_when_enabled() {
345        force_for_test(true);
346        for s in [
347            accent("ok"),
348            warn("careful"),
349            error("boom"),
350            dim("hs-abc123"),
351            bold("Capture audit pipeline"),
352            confidence(Some(0.95), "0.95"),
353            state_id("hs-abc123"),
354            principal("Ada Lovelace", "ada@analytical.engine"),
355            thread_state("active"),
356        ] {
357            assert!(s.contains('\x1b'), "expected ANSI escape in {:?}", s);
358        }
359    }
360
361    /// Unknown thread-state strings render plain — we don't want
362    /// to invent semantics for a state the matcher doesn't know.
363    #[test]
364    #[serial(color_state)]
365    fn thread_state_unknown_is_plain() {
366        force_for_test(true);
367        let out = thread_state("zorblax");
368        assert_eq!(out, "zorblax", "unknown state should not be styled");
369    }
370
371    /// Confidence bands map to the documented thresholds.
372    #[test]
373    #[serial(color_state)]
374    fn confidence_bands() {
375        force_for_test(true);
376        // None → dim
377        let none = confidence(None, "—");
378        assert!(
379            none.contains("\x1b[2m"),
380            "None should be dimmed: {:?}",
381            none
382        );
383
384        // ≥0.9 → accent (sage 71)
385        let high = confidence(Some(0.95), "0.95");
386        assert!(high.contains("38;5;71"), "high should be sage: {:?}", high);
387
388        // ≥0.75 and <0.9 → warn (amber 178)
389        let mid = confidence(Some(0.80), "0.80");
390        assert!(mid.contains("38;5;178"), "mid should be amber: {:?}", mid);
391
392        // <0.75 → error (rust 167)
393        let low = confidence(Some(0.50), "0.50");
394        assert!(low.contains("38;5;167"), "low should be rust: {:?}", low);
395    }
396
397    /// Decision logic: `--no-color` overrides every other signal,
398    /// `NO_COLOR` overrides `CLICOLOR_FORCE`, and TTY auto-detect
399    /// is the fallback.
400    #[test]
401    fn decision_no_color_flag_wins() {
402        let cli = test_cli(true);
403        let env = EnvProbe {
404            no_color: None,
405            clicolor_force: Some("1"),
406            is_tty: true,
407        };
408        assert!(!decide_color_enabled(&cli, &env));
409    }
410
411    #[test]
412    fn decision_no_color_env_overrides_force() {
413        let cli = test_cli(false);
414        let env = EnvProbe {
415            no_color: Some("1"),
416            clicolor_force: Some("1"),
417            is_tty: true,
418        };
419        assert!(
420            !decide_color_enabled(&cli, &env),
421            "NO_COLOR must beat CLICOLOR_FORCE per no-color.org precedence"
422        );
423    }
424
425    #[test]
426    fn decision_force_color_overrides_non_tty() {
427        let cli = test_cli(false);
428        let env = EnvProbe {
429            no_color: None,
430            clicolor_force: Some("1"),
431            is_tty: false,
432        };
433        assert!(decide_color_enabled(&cli, &env));
434    }
435
436    #[test]
437    fn decision_non_tty_default_off() {
438        let cli = test_cli(false);
439        let env = EnvProbe {
440            no_color: None,
441            clicolor_force: None,
442            is_tty: false,
443        };
444        assert!(!decide_color_enabled(&cli, &env));
445    }
446
447    #[test]
448    fn decision_tty_default_on() {
449        let cli = test_cli(false);
450        let env = EnvProbe {
451            no_color: None,
452            clicolor_force: None,
453            is_tty: true,
454        };
455        assert!(decide_color_enabled(&cli, &env));
456    }
457
458    /// Empty `NO_COLOR` is the documented opt-out — per
459    /// no-color.org, "the value of `NO_COLOR` is irrelevant if it's
460    /// non-empty"; an empty string is *not* a disable. We honour
461    /// that subtlety so users can `NO_COLOR= cargo run` to reset
462    /// without unsetting.
463    #[test]
464    fn decision_empty_no_color_is_not_disable() {
465        let cli = test_cli(false);
466        let env = EnvProbe {
467            no_color: Some(""),
468            clicolor_force: None,
469            is_tty: true,
470        };
471        assert!(decide_color_enabled(&cli, &env));
472    }
473
474    fn test_cli(no_color: bool) -> Cli {
475        // We can't easily construct `Cli` directly because it has a
476        // mandatory subcommand; route through clap's parser with a
477        // minimal valid argv. `--no-color` is a global flag so it
478        // lands regardless of which subcommand we pick.
479        use clap::Parser;
480        let mut argv = vec!["heddle".to_string()];
481        if no_color {
482            argv.push("--no-color".to_string());
483        }
484        argv.push("status".to_string());
485        Cli::try_parse_from(argv).expect("parse minimal cli")
486    }
487
488    /// Crucial: `principal()` with color off returns *exactly* the
489    /// same string the un-styled call site would have produced.
490    /// Render-site tests rely on this byte-for-byte equivalence.
491    #[test]
492    #[serial(color_state)]
493    fn principal_uncolored_is_identity() {
494        force_for_test(false);
495        let out = principal("Ada Lovelace", "ada@analytical.engine");
496        assert_eq!(out, "Ada Lovelace <ada@analytical.engine>");
497    }
498
499    #[test]
500    #[serial(color_state)]
501    fn state_id_uncolored_is_identity() {
502        force_for_test(false);
503        assert_eq!(state_id("hs-abc123"), "hs-abc123");
504    }
505}