Skip to main content

vela_protocol/objects/
cli_style.rs

1//! CLI output discipline for Vela.
2//!
3//! The rules:
4//!   - Signal blue (#3B5BDB) is reserved for live state — the current step of a
5//!     running audit or the active cursor. It is never used for success.
6//!   - State chips use a muted engraved palette derived from ink, not a
7//!     traffic-light green/red: moss for ok, brass for contested, dust for
8//!     stale, madder for lost.
9//!   - `·` (middle dot) is the only decorative separator.
10//!   - Banners are a dim tick row and a mono eyebrow, never `===` or `---`.
11//!   - All ANSI is gated on a TTY stdout and NO_COLOR being unset.
12//!
13//! Every styled string in `cli.rs` should route through a helper here so the
14//! discipline is enforced in one place.
15
16use colored::{ColoredString, Colorize};
17use std::io::IsTerminal;
18use std::sync::Once;
19
20// --- palette -----------------------------------------------------------------
21
22pub const MOSS: (u8, u8, u8) = (0x3F, 0x6B, 0x4E);
23pub const BRASS: (u8, u8, u8) = (0x8A, 0x6A, 0x1F);
24pub const DUST: (u8, u8, u8) = (0x7A, 0x6F, 0x5C);
25pub const MADDER: (u8, u8, u8) = (0x8A, 0x3A, 0x3A);
26pub const SIGNAL: (u8, u8, u8) = (0x3B, 0x5B, 0xDB);
27
28// --- init --------------------------------------------------------------------
29
30static INIT: Once = Once::new();
31
32/// Initialize styling. Call once near the CLI entry point.
33///
34/// Disables all ANSI output when stdout is not a terminal or `NO_COLOR` is set.
35/// Safe to call multiple times; only the first call takes effect.
36pub fn init() {
37    INIT.call_once(|| {
38        let no_color = std::env::var_os("NO_COLOR").is_some();
39        let is_tty = std::io::stdout().is_terminal();
40        if no_color || !is_tty {
41            colored::control::set_override(false);
42        }
43    });
44}
45
46// --- primitives --------------------------------------------------------------
47
48#[must_use]
49pub fn dim(s: &str) -> ColoredString {
50    s.dimmed()
51}
52
53/// A tick row — dim `·` characters of a given visual width.
54#[must_use]
55pub fn tick_row(width: usize) -> String {
56    let row = "·".repeat(width);
57    format!("{}", row.dimmed())
58}
59
60// --- ink-derived color wrappers ---------------------------------------------
61// These exist so `cli.rs` and friends can migrate from `.green()`/`.red()` to
62// a muted engraved palette without loss of semantic pairing (added / removed,
63// ok / lost, current / stale).
64
65#[must_use]
66pub fn moss(s: impl AsRef<str>) -> ColoredString {
67    let (r, g, b) = MOSS;
68    s.as_ref().truecolor(r, g, b)
69}
70
71#[must_use]
72pub fn madder(s: impl AsRef<str>) -> ColoredString {
73    let (r, g, b) = MADDER;
74    s.as_ref().truecolor(r, g, b)
75}
76
77#[must_use]
78pub fn brass(s: impl AsRef<str>) -> ColoredString {
79    let (r, g, b) = BRASS;
80    s.as_ref().truecolor(r, g, b)
81}
82
83#[must_use]
84pub fn signal(s: impl AsRef<str>) -> ColoredString {
85    let (r, g, b) = SIGNAL;
86    s.as_ref().truecolor(r, g, b)
87}
88
89// --- state chips -------------------------------------------------------------
90
91/// Engraved state chip: `· label` in the state color. Lowercase, mono-ish.
92#[must_use]
93pub fn chip(label: &str, rgb: (u8, u8, u8)) -> String {
94    let dot = "·".truecolor(rgb.0, rgb.1, rgb.2);
95    let text = label.truecolor(rgb.0, rgb.1, rgb.2);
96    format!("{dot} {text}")
97}
98
99#[must_use]
100pub fn ok(label: &str) -> String {
101    chip(label, MOSS)
102}
103
104#[must_use]
105pub fn warn(label: &str) -> String {
106    chip(label, BRASS)
107}
108
109#[must_use]
110pub fn stale(label: &str) -> String {
111    chip(label, DUST)
112}
113
114#[must_use]
115pub fn lost(label: &str) -> String {
116    chip(label, MADDER)
117}
118
119/// Signal-blue chip — reserved for live state only.
120#[must_use]
121pub fn live(label: &str) -> String {
122    chip(label, SIGNAL)
123}
124
125// --- headers -----------------------------------------------------------------
126
127/// A header block: dim mono eyebrow, bold title, dim tick row.
128///
129/// Prints three lines to stdout.
130pub fn header(eyebrow: &str, title: &str) {
131    println!("  {}", eyebrow.dimmed());
132    println!("  {}", title.bold());
133    println!("  {}", tick_row(60));
134}
135
136/// An error prefix — `err ·` in madder, lowercase.
137#[must_use]
138pub fn err_prefix() -> String {
139    let (r, g, b) = MADDER;
140    format!("{}", "err ·".truecolor(r, g, b))
141}