ados-term 0.1.0

Full-screen terminal toolkit for installers and deploy wizards: amber-on-charcoal theme with color tiers, frame compositor, raw tty backends, live step-progress renderers and a streaming command runner.
Documentation
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
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
//! Color + glyph theme for the terminal UI, with capability tiers.
//!
//! The palette is amber-on-charcoal: a charcoal background, near-white
//! headings, muted gray body text, and a single amber accent for what is
//! selected or acted on. Destructive is a salmon red, success a soft green.
//!
//! Color is emitted at the tier the terminal can show: truecolor (24-bit RGB),
//! a 256-color fallback (nearest xterm index), a basic 16-color fallback, or
//! plain text when there is no color at all. Every styled span resets only the
//! foreground (`ESC[39m`) or the bold attribute (`ESC[22m`), never the whole
//! SGR state, so the charcoal background painted once per frame is preserved
//! across padding and borders. Only the selection bar changes the background,
//! and it restores the charcoal base itself.
//!
//! Glyphs (marks, spinner, box drawing) are keyed on an ASCII tier for
//! `--ascii`, non-UTF-8 locales, and basic terminals. Width math elsewhere
//! counts `chars()`, so every glyph here is one display column.

/// An RGB color for the truecolor tier.
type Rgb = (u8, u8, u8);

// --- palette (generated from the brand design tokens) ------------------------

use crate::palette_generated::{
    AMBER, AMBER_256, AMBER_DEEP, AMBER_LIGHT, CHARCOAL, CHARCOAL_256, DANGER, DANGER_256, HEADING,
    HEADING_256, MUTED, MUTED_256, ON_AMBER, ON_AMBER_256, SUCCESS, SUCCESS_256, WARNING,
    WARNING_256,
};

/// The braille spinner frames (the modern gold standard).
const SPIN_UNICODE: &[&str] = &["⠋", "⠙", "⠹", "⠸", "⠼", "⠴", "⠦", "⠧", "⠇", "⠏"];
/// ASCII spinner fallback.
const SPIN_ASCII: &[&str] = &["-", "\\", "|", "/"];

/// How much color the terminal can render.
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
pub enum ColorTier {
    /// 24-bit RGB (`COLORTERM=truecolor`).
    Truecolor,
    /// 256-color indexed (`TERM=*-256color`).
    Ansi256,
    /// Basic 16-color (bold + named foreground).
    Basic,
    /// No color: `NO_COLOR`, `--no-color`, `TERM=dumb`, or not a terminal.
    None,
}

/// Box-drawing glyphs (rounded) and their ASCII fallbacks.
pub struct BoxChars {
    /// Top-left corner.
    pub tl: &'static str,
    /// Top-right corner.
    pub tr: &'static str,
    /// Bottom-left corner.
    pub bl: &'static str,
    /// Bottom-right corner.
    pub br: &'static str,
    /// Horizontal edge.
    pub h: &'static str,
    /// Vertical edge.
    pub v: &'static str,
}

/// Resolved color + glyph capability for this run.
#[derive(Debug, Clone, Copy)]
pub struct Theme {
    /// Use the ASCII glyph tier (no Unicode box/spinner/marks).
    pub ascii: bool,
    /// The color depth to emit at.
    pub tier: ColorTier,
}

impl Theme {
    /// Resolve from the flags + environment. `NO_COLOR` (any value) or
    /// `--no-color` disables color; `--ascii` or a non-UTF-8 locale selects the
    /// ASCII glyph tier. The color depth comes from `COLORTERM` + `TERM`.
    pub fn detect(no_color_flag: bool, ascii_flag: bool) -> Theme {
        let no_color = std::env::var_os("NO_COLOR").is_some() || no_color_flag;
        Theme {
            ascii: ascii_flag || !locale_is_utf8(),
            tier: detect_tier(no_color),
        }
    }

    /// Whether the tier paints a real background (truecolor / 256). Basic and
    /// plain tiers use the terminal's own background.
    pub fn paints_background(&self) -> bool {
        matches!(self.tier, ColorTier::Truecolor | ColorTier::Ansi256)
    }

    /// The escape that sets the charcoal background for a frame, or empty when
    /// the tier does not paint a background.
    pub fn background_prefix(&self) -> String {
        match self.tier {
            ColorTier::Truecolor => bg_rgb(CHARCOAL),
            ColorTier::Ansi256 => format!("\x1b[48;5;{CHARCOAL_256}m"),
            _ => String::new(),
        }
    }

    // --- foreground color helpers (foreground-only reset) ------------------

    fn fg(&self, rgb: Rgb, idx: u8, basic: u8, s: &str) -> String {
        match self.tier {
            ColorTier::Truecolor => format!("\x1b[38;2;{};{};{}m{s}\x1b[39m", rgb.0, rgb.1, rgb.2),
            ColorTier::Ansi256 => format!("\x1b[38;5;{idx}m{s}\x1b[39m"),
            ColorTier::Basic => format!("\x1b[{basic}m{s}\x1b[39m"),
            ColorTier::None => s.to_string(),
        }
    }

    /// Success green.
    pub fn ok(&self, s: &str) -> String {
        self.fg(SUCCESS, SUCCESS_256, 32, s)
    }
    /// Destructive salmon red.
    pub fn fail(&self, s: &str) -> String {
        self.fg(DANGER, DANGER_256, 31, s)
    }
    /// The amber accent (selected / acted-on).
    pub fn accent(&self, s: &str) -> String {
        self.fg(AMBER, AMBER_256, 33, s)
    }
    /// Warning yellow.
    pub fn warn(&self, s: &str) -> String {
        self.fg(WARNING, WARNING_256, 33, s)
    }
    /// Muted gray body text.
    pub fn dim(&self, s: &str) -> String {
        self.fg(MUTED, MUTED_256, 90, s)
    }
    /// A bold near-white heading.
    pub fn heading(&self, s: &str) -> String {
        self.bold(&self.fg(HEADING, HEADING_256, 97, s))
    }
    /// Bold, resetting only the bold attribute.
    pub fn bold(&self, s: &str) -> String {
        if self.tier == ColorTier::None {
            s.to_string()
        } else {
            format!("\x1b[1m{s}\x1b[22m")
        }
    }

    /// Color one row of the block wordmark with an amber shade, lighter at the
    /// top and deeper at the bottom (a subtle vertical gradient). The 256 and
    /// basic tiers use the single amber accent; plain is untouched.
    pub fn amber_gradient(&self, row: usize, s: &str) -> String {
        match self.tier {
            ColorTier::Truecolor => {
                let rgb = match row {
                    0 => AMBER_LIGHT,
                    1 => AMBER,
                    _ => AMBER_DEEP,
                };
                self.bold(&format!(
                    "\x1b[38;2;{};{};{}m{s}\x1b[39m",
                    rgb.0, rgb.1, rgb.2
                ))
            }
            _ => self.bold(&self.accent(s)),
        }
    }

    /// A full-width solid amber selection bar with dark text, `width` display
    /// columns wide. In the basic and plain tiers there is no background to
    /// draw, so the row degrades to an accented marker plus a bold label (the
    /// caller pads it to width). `content` is plain text (no embedded escapes).
    pub fn selection_bar(&self, content: &str, width: usize) -> String {
        let body = pad_or_clip(content, width);
        match self.tier {
            ColorTier::Truecolor => format!(
                "{}{}{body}{}",
                bg_rgb(AMBER),
                fg_rgb(ON_AMBER),
                self.restore_base()
            ),
            ColorTier::Ansi256 => format!(
                "\x1b[48;5;{AMBER_256}m\x1b[38;5;{ON_AMBER_256}m{body}{}",
                self.restore_base()
            ),
            // No background tier: keep the label readable without a bar.
            ColorTier::Basic => self.bold(&self.accent(content)),
            ColorTier::None => content.to_string(),
        }
    }

    /// The escape that restores the base background + default foreground after a
    /// span that changed the background (the selection bar). Truecolor and 256
    /// tiers restore charcoal; other tiers only reset the foreground.
    fn restore_base(&self) -> String {
        match self.tier {
            ColorTier::Truecolor => format!("{}\x1b[39m", bg_rgb(CHARCOAL)),
            ColorTier::Ansi256 => format!("\x1b[48;5;{CHARCOAL_256}m\x1b[39m"),
            _ => "\x1b[39m".to_string(),
        }
    }

    // --- glyphs ------------------------------------------------------------

    /// The current spinner frame glyph.
    pub fn spinner(&self, frame: usize) -> &'static str {
        if self.ascii {
            SPIN_ASCII[frame % SPIN_ASCII.len()]
        } else {
            SPIN_UNICODE[frame % SPIN_UNICODE.len()]
        }
    }
    /// The success mark (`✓` / `+`).
    pub fn glyph_ok(&self) -> &'static str {
        if self.ascii {
            "+"
        } else {
            "✓"
        }
    }
    /// The failure mark (`✗` / `x`).
    pub fn glyph_fail(&self) -> &'static str {
        if self.ascii {
            "x"
        } else {
            "✗"
        }
    }
    /// The not-started mark (`•` / `.`).
    pub fn glyph_pending(&self) -> &'static str {
        if self.ascii {
            "."
        } else {
            "•"
        }
    }
    /// A filled radio / progress dot.
    pub fn dot_filled(&self) -> &'static str {
        if self.ascii {
            "#"
        } else {
            "●"
        }
    }
    /// An empty radio / progress dot.
    pub fn dot_empty(&self) -> &'static str {
        if self.ascii {
            "."
        } else {
            "○"
        }
    }
    /// A filled checkbox mark (checklist on).
    pub fn box_checked(&self) -> &'static str {
        if self.ascii {
            "#"
        } else {
            "■"
        }
    }
    /// An empty checkbox mark (checklist off).
    pub fn box_unchecked(&self) -> &'static str {
        if self.ascii {
            "."
        } else {
            "□"
        }
    }

    /// The box-drawing set (rounded Unicode, or ASCII).
    pub fn box_chars(&self) -> BoxChars {
        if self.ascii {
            BoxChars {
                tl: "+",
                tr: "+",
                bl: "+",
                br: "+",
                h: "-",
                v: "|",
            }
        } else {
            BoxChars {
                tl: "╭",
                tr: "╮",
                bl: "╰",
                br: "╯",
                h: "─",
                v: "│",
            }
        }
    }
}

/// A truecolor foreground set.
fn fg_rgb(rgb: Rgb) -> String {
    format!("\x1b[38;2;{};{};{}m", rgb.0, rgb.1, rgb.2)
}

/// A truecolor background set.
fn bg_rgb(rgb: Rgb) -> String {
    format!("\x1b[48;2;{};{};{}m", rgb.0, rgb.1, rgb.2)
}

/// Pad `content` to `width` display columns with spaces, or clip it (with an
/// ellipsis) when it overflows. `content` must be plain text.
fn pad_or_clip(content: &str, width: usize) -> String {
    let n = content.chars().count();
    if n == width {
        content.to_string()
    } else if n < width {
        format!("{content}{}", " ".repeat(width - n))
    } else if width == 0 {
        String::new()
    } else {
        let mut out: String = content.chars().take(width.saturating_sub(1)).collect();
        out.push('…');
        out
    }
}

/// Resolve the color depth from the environment. `NO_COLOR` / `--no-color` and
/// `TERM=dumb` force the plain tier; `COLORTERM=truecolor|24bit` selects
/// truecolor; a `*-256color` `TERM` selects 256; anything else is basic.
fn detect_tier(no_color: bool) -> ColorTier {
    if no_color {
        return ColorTier::None;
    }
    let term = std::env::var("TERM").unwrap_or_default();
    if term == "dumb" {
        return ColorTier::None;
    }
    let colorterm = std::env::var("COLORTERM")
        .unwrap_or_default()
        .to_lowercase();
    if colorterm.contains("truecolor") || colorterm.contains("24bit") {
        return ColorTier::Truecolor;
    }
    if term.contains("256color") {
        return ColorTier::Ansi256;
    }
    ColorTier::Basic
}

/// True when the locale env vars indicate UTF-8 (or are unset — the modern
/// default). A `C`/`POSIX` locale selects the ASCII tier.
fn locale_is_utf8() -> bool {
    // POSIX precedence: LC_ALL wins, then LC_CTYPE, then LANG. The first set,
    // non-empty value decides; nothing set → assume a modern UTF-8 terminal.
    for key in ["LC_ALL", "LC_CTYPE", "LANG"] {
        if let Ok(v) = std::env::var(key) {
            if v.is_empty() {
                continue;
            }
            let v = v.to_ascii_lowercase();
            return v.contains("utf-8") || v.contains("utf8");
        }
    }
    true
}

#[cfg(test)]
mod tests {
    use super::*;

    fn plain() -> Theme {
        Theme {
            ascii: false,
            tier: ColorTier::None,
        }
    }

    #[test]
    fn no_color_tier_emits_plain_text() {
        let t = plain();
        assert_eq!(t.ok("✓"), "✓");
        assert_eq!(t.dim("x"), "x");
        assert_eq!(t.accent("a"), "a");
        assert_eq!(t.bold("b"), "b");
        assert_eq!(t.heading("h"), "h");
        assert_eq!(t.selection_bar("row", 3), "row");
    }

    #[test]
    fn truecolor_amber_accent_uses_rgb_sgr() {
        let t = Theme {
            ascii: false,
            tier: ColorTier::Truecolor,
        };
        let a = t.accent("x");
        assert!(
            a.contains("\x1b[38;2;235;193;87m"),
            "amber rgb missing: {a:?}"
        );
        assert!(a.ends_with("\x1b[39m"), "fg not reset: {a:?}");
        // Never a full reset that would clear the charcoal background.
        assert!(!a.contains("\x1b[0m"), "must not full-reset: {a:?}");
    }

    #[test]
    fn selection_bar_is_full_width_amber_with_dark_text() {
        let t = Theme {
            ascii: false,
            tier: ColorTier::Truecolor,
        };
        let bar = t.selection_bar("● Drone", 20);
        // Amber background + dark foreground, and the visible content padded.
        assert!(
            bar.contains("\x1b[48;2;235;193;87m"),
            "amber bg missing: {bar:?}"
        );
        assert!(
            bar.contains("\x1b[38;2;26;26;26m"),
            "dark fg missing: {bar:?}"
        );
        // Restores the charcoal base so the row after it is not amber.
        assert!(
            bar.contains("\x1b[48;2;31;31;31m"),
            "charcoal restore missing: {bar:?}"
        );
        assert_eq!(super::pad_or_clip("● Drone", 20).chars().count(), 20);
    }

    #[test]
    fn background_prefix_only_on_painting_tiers() {
        let tc = Theme {
            ascii: false,
            tier: ColorTier::Truecolor,
        };
        assert_eq!(tc.background_prefix(), "\x1b[48;2;31;31;31m");
        assert!(tc.paints_background());
        let basic = Theme {
            ascii: false,
            tier: ColorTier::Basic,
        };
        assert!(basic.background_prefix().is_empty());
        assert!(!basic.paints_background());
        assert!(plain().background_prefix().is_empty());
    }

    #[test]
    fn ascii_tier_uses_ascii_glyphs() {
        let t = Theme {
            ascii: true,
            tier: ColorTier::None,
        };
        assert_eq!(t.glyph_ok(), "+");
        assert_eq!(t.glyph_fail(), "x");
        assert_eq!(t.spinner(0), "-");
        assert_eq!(t.box_chars().h, "-");
        assert_eq!(t.dot_filled(), "#");
        assert_eq!(t.box_checked(), "#");
    }

    #[test]
    fn unicode_glyphs_are_single_column() {
        let t = plain();
        for g in [
            t.glyph_ok(),
            t.glyph_fail(),
            t.glyph_pending(),
            t.spinner(2),
            t.dot_filled(),
            t.dot_empty(),
            t.box_checked(),
            t.box_unchecked(),
        ] {
            assert_eq!(g.chars().count(), 1, "glyph {g:?} must be one column");
        }
    }
}