rdom-tui 0.3.3

Terminal rendering layer for rdom-core — flexbox layout, TUI styles, key/mouse events. Use rdom-core directly for headless DOM manipulation.
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
//! SGR (Select Graphic Rendition) byte emission.
//!
//! Given a previous style and a new style, emit the minimal ANSI
//! escape sequence that transforms the terminal's current SGR state
//! into the new one. This is the heart of "don't re-emit what hasn't
//! changed" optimization.
//!
//! ## Encoding
//!
//! - Foreground: `\x1b[38;5;Nm` (indexed), `\x1b[38;2;R;G;Bm`
//!   (truecolor), `\x1b[39m` (reset). rdom is truecolor-only — every
//!   CSS named color expands to a 24-bit `Rgb` triple at parse time;
//!   no ANSI-16 short codes are emitted.
//! - Background: same with `48;5`, `48;2`, `49`.
//! - Modifier bits: `1` bold, `3` italic, `4` underline,
//!   `5` slow blink, `6` rapid blink, `7` reversed, `8` hidden,
//!   `9` crossed-out. Turn-off codes: `22` bold, `23` italic,
//!   `24` underline, `25` blink, `27` reversed, `28` hidden,
//!   `29` crossed-out.
//!
//! ## Style cache across frames
//!
//! `CrosstermBackend` keeps an `SgrState` across `draw()` calls. When
//! frame N ends, we leave the terminal with whatever SGR state the
//! last cell had; frame N+1 starts from that state and only emits
//! diffs. This saves ~30% of bytes in steady-state rendering.

use std::io::{self, Write};

use super::{Color, Modifier};

/// The SGR state we need to track across cells: fg, bg, and the
/// modifier bitmask.
#[derive(Debug, Clone, Copy, PartialEq, Eq, Default)]
pub struct SgrState {
    pub fg: Color,
    pub bg: Color,
    pub modifier: Modifier,
}

impl SgrState {
    pub const RESET: SgrState = SgrState {
        fg: Color::Reset,
        bg: Color::Reset,
        modifier: Modifier::empty(),
    };
}

/// Emit the minimal SGR sequence transitioning `w` from `prev` to `new`.
/// Returns the new state (equal to `new`; returned for chaining).
pub fn emit_sgr_transition<W: Write>(
    w: &mut W,
    prev: SgrState,
    new: SgrState,
) -> io::Result<SgrState> {
    if prev == new {
        return Ok(new);
    }

    // Modifier diff.
    let mod_to_remove = prev.modifier.difference(new.modifier);
    let mod_to_add = new.modifier.difference(prev.modifier);

    for code in modifier_off_codes(mod_to_remove) {
        write_sgr(w, code)?;
    }
    for code in modifier_on_codes(mod_to_add) {
        write_sgr(w, code)?;
    }

    // fg diff.
    if prev.fg != new.fg {
        emit_fg(w, new.fg)?;
    }
    // bg diff.
    if prev.bg != new.bg {
        emit_bg(w, new.bg)?;
    }

    Ok(new)
}

/// Emit a sequence that fully resets SGR state to defaults
/// (`\x1b[0m`). Used by `Terminal::clear` and on clean shutdown.
pub fn emit_reset<W: Write>(w: &mut W) -> io::Result<()> {
    w.write_all(b"\x1b[0m")
}

fn write_sgr<W: Write>(w: &mut W, code: u16) -> io::Result<()> {
    write!(w, "\x1b[{}m", code)
}

// ─── Modifier encoding ──────────────────────────────────────────────

fn modifier_on_codes(bits: Modifier) -> impl Iterator<Item = u16> {
    let mut out = Vec::new();
    if bits.contains(Modifier::BOLD) {
        out.push(1);
    }
    if bits.contains(Modifier::ITALIC) {
        out.push(3);
    }
    if bits.contains(Modifier::UNDERLINED) {
        out.push(4);
    }
    if bits.contains(Modifier::SLOW_BLINK) {
        out.push(5);
    }
    if bits.contains(Modifier::RAPID_BLINK) {
        out.push(6);
    }
    if bits.contains(Modifier::HIDDEN) {
        out.push(8);
    }
    if bits.contains(Modifier::CROSSED_OUT) {
        out.push(9);
    }
    out.into_iter()
}

fn modifier_off_codes(bits: Modifier) -> impl Iterator<Item = u16> {
    let mut out = Vec::new();
    if bits.contains(Modifier::BOLD) {
        out.push(22);
    }
    if bits.contains(Modifier::ITALIC) {
        out.push(23);
    }
    if bits.contains(Modifier::UNDERLINED) {
        out.push(24);
    }
    if bits.intersects(Modifier::SLOW_BLINK | Modifier::RAPID_BLINK) {
        out.push(25);
    }
    if bits.contains(Modifier::HIDDEN) {
        out.push(28);
    }
    if bits.contains(Modifier::CROSSED_OUT) {
        out.push(29);
    }
    out.into_iter()
}

// ─── Color encoding ─────────────────────────────────────────────────

/// Emit an SGR sequence setting the foreground color. Truecolor-
/// only: `Rgb` emits `\x1b[38;2;r;g;b m`, `Indexed` emits
/// `\x1b[38;5;n m`, `Reset` emits `\x1b[39m`. No ANSI-16
/// quantization — every Rgb goes out as `38;2;r;g;b`.
fn emit_fg<W: Write>(w: &mut W, color: Color) -> io::Result<()> {
    match color {
        Color::Reset => write!(w, "\x1b[39m"),
        Color::Indexed(n) => write!(w, "\x1b[38;5;{}m", n),
        Color::Rgb(r, g, b) => write!(w, "\x1b[38;2;{};{};{}m", r, g, b),
    }
}

/// Mirror of [`emit_fg`] for background colors.
fn emit_bg<W: Write>(w: &mut W, color: Color) -> io::Result<()> {
    match color {
        Color::Reset => write!(w, "\x1b[49m"),
        Color::Indexed(n) => write!(w, "\x1b[48;5;{}m", n),
        Color::Rgb(r, g, b) => write!(w, "\x1b[48;2;{};{};{}m", r, g, b),
    }
}

// ─── Cursor positioning (CUP) ───────────────────────────────────────

/// Emit `\x1b[y;xH` — Cursor Position (1-indexed). Writer must have
/// already tracked that it doesn't know where the cursor is (or that
/// it's not at the target).
pub fn emit_cup<W: Write>(w: &mut W, x: u16, y: u16) -> io::Result<()> {
    write!(w, "\x1b[{};{}H", y.saturating_add(1), x.saturating_add(1))
}

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

    fn emit_diff(prev: SgrState, new: SgrState) -> Vec<u8> {
        let mut buf = Vec::new();
        emit_sgr_transition(&mut buf, prev, new).unwrap();
        buf
    }

    // ── fg colors ────────────────────────────────────────────────────

    #[test]
    fn fg_rgb_pure_red_is_truecolor() {
        // Truecolor-only: every Rgb goes out as `38;2;r;g;b`,
        // regardless of whether it happens to match a legacy ANSI
        // named-color RGB.
        let buf = emit_diff(
            SgrState::default(),
            SgrState {
                fg: Color::Rgb(255, 0, 0),
                ..SgrState::default()
            },
        );
        assert_eq!(buf, b"\x1b[38;2;255;0;0m");
    }

    #[test]
    fn fg_rgb_lightcoral_is_truecolor() {
        // CSS `lightcoral` — plain truecolor, no ANSI-16 shortcut.
        let buf = emit_diff(
            SgrState::default(),
            SgrState {
                fg: Color::Rgb(240, 128, 128),
                ..SgrState::default()
            },
        );
        assert_eq!(buf, b"\x1b[38;2;240;128;128m");
    }

    #[test]
    fn fg_indexed() {
        let buf = emit_diff(
            SgrState::default(),
            SgrState {
                fg: Color::Indexed(204),
                ..SgrState::default()
            },
        );
        assert_eq!(buf, b"\x1b[38;5;204m");
    }

    #[test]
    fn fg_rgb_truecolor() {
        let buf = emit_diff(
            SgrState::default(),
            SgrState {
                fg: Color::Rgb(61, 144, 206),
                ..SgrState::default()
            },
        );
        assert_eq!(buf, b"\x1b[38;2;61;144;206m");
    }

    #[test]
    fn fg_reset() {
        let buf = emit_diff(
            SgrState {
                fg: Color::Rgb(255, 0, 0),
                ..SgrState::default()
            },
            SgrState::default(),
        );
        assert_eq!(buf, b"\x1b[39m");
    }

    // ── bg colors ────────────────────────────────────────────────────

    #[test]
    fn bg_rgb_pure_blue_is_truecolor() {
        // Same as fg_rgb_pure_red_is_truecolor for the bg slot.
        let buf = emit_diff(
            SgrState::default(),
            SgrState {
                bg: Color::Rgb(0, 0, 255),
                ..SgrState::default()
            },
        );
        assert_eq!(buf, b"\x1b[48;2;0;0;255m");
    }

    #[test]
    fn bg_rgb_truecolor() {
        let buf = emit_diff(
            SgrState::default(),
            SgrState {
                bg: Color::Rgb(10, 20, 30),
                ..SgrState::default()
            },
        );
        assert_eq!(buf, b"\x1b[48;2;10;20;30m");
    }

    // ── modifiers ────────────────────────────────────────────────────

    #[test]
    fn bold_on() {
        let buf = emit_diff(
            SgrState::default(),
            SgrState {
                modifier: Modifier::BOLD,
                ..SgrState::default()
            },
        );
        assert_eq!(buf, b"\x1b[1m");
    }

    #[test]
    fn multiple_modifiers() {
        let buf = emit_diff(
            SgrState::default(),
            SgrState {
                modifier: Modifier::BOLD | Modifier::ITALIC | Modifier::UNDERLINED,
                ..SgrState::default()
            },
        );
        // Codes emitted in order: 1, 3, 4
        assert_eq!(buf, b"\x1b[1m\x1b[3m\x1b[4m");
    }

    #[test]
    fn bold_off_uses_sgr22() {
        let buf = emit_diff(
            SgrState {
                modifier: Modifier::BOLD,
                ..SgrState::default()
            },
            SgrState::default(),
        );
        assert_eq!(buf, b"\x1b[22m");
    }

    #[test]
    fn italic_off_uses_sgr23() {
        let buf = emit_diff(
            SgrState {
                modifier: Modifier::ITALIC,
                ..SgrState::default()
            },
            SgrState::default(),
        );
        assert_eq!(buf, b"\x1b[23m");
    }

    #[test]
    fn underline_off_uses_sgr24() {
        let buf = emit_diff(
            SgrState {
                modifier: Modifier::UNDERLINED,
                ..SgrState::default()
            },
            SgrState::default(),
        );
        assert_eq!(buf, b"\x1b[24m");
    }

    // ── no-op transitions ────────────────────────────────────────────

    #[test]
    fn same_state_emits_nothing() {
        let state = SgrState {
            fg: Color::Rgb(255, 0, 0),
            bg: Color::Rgb(0, 0, 0),
            modifier: Modifier::BOLD,
        };
        assert_eq!(emit_diff(state, state), b"");
    }

    #[test]
    fn only_fg_changed() {
        let prev = SgrState {
            fg: Color::Rgb(255, 0, 0),
            bg: Color::Rgb(0, 0, 0),
            modifier: Modifier::BOLD,
        };
        let new = SgrState {
            fg: Color::Rgb(0, 128, 0),
            ..prev
        };
        assert_eq!(emit_diff(prev, new), b"\x1b[38;2;0;128;0m");
    }

    // ── combined transitions ─────────────────────────────────────────

    #[test]
    fn add_bold_change_fg_and_bg() {
        let prev = SgrState::default();
        let new = SgrState {
            fg: Color::Rgb(255, 0, 0),
            bg: Color::Rgb(0, 0, 0),
            modifier: Modifier::BOLD,
        };
        let buf = emit_diff(prev, new);
        // Modifiers first (add bold), then fg, then bg — all truecolor.
        assert_eq!(buf, b"\x1b[1m\x1b[38;2;255;0;0m\x1b[48;2;0;0;0m");
    }

    #[test]
    fn remove_one_bit_add_another() {
        let prev = SgrState {
            modifier: Modifier::BOLD,
            ..SgrState::default()
        };
        let new = SgrState {
            modifier: Modifier::ITALIC,
            ..SgrState::default()
        };
        // Remove bold (22), add italic (3).
        assert_eq!(emit_diff(prev, new), b"\x1b[22m\x1b[3m");
    }

    // ── CUP (cursor position) ───────────────────────────────────────

    #[test]
    fn cup_emits_one_indexed_row_col() {
        let mut buf = Vec::new();
        emit_cup(&mut buf, 5, 10).unwrap();
        assert_eq!(buf, b"\x1b[11;6H");
    }

    #[test]
    fn cup_origin_is_1_1() {
        let mut buf = Vec::new();
        emit_cup(&mut buf, 0, 0).unwrap();
        assert_eq!(buf, b"\x1b[1;1H");
    }

    // ── Reset ───────────────────────────────────────────────────────

    #[test]
    fn emit_reset_produces_sgr0() {
        let mut buf = Vec::new();
        emit_reset(&mut buf).unwrap();
        assert_eq!(buf, b"\x1b[0m");
    }
}