comb-tui 0.2.0

A tiny native terminal-UI engine — surfaces, layers, widgets.
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
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
//! The cell grid: a `width × height` array of [`Cell`]s that widgets draw into.
//! Two buffers (front = on screen, back = next frame) are diffed so the terminal
//! only receives the cells that actually changed.

use unicode_width::{UnicodeWidthChar, UnicodeWidthStr};

use crate::core::geom::{Align, Rect, Size};
use crate::core::style::Style;
use crate::core::text::Line;

/// Display columns for a label with `pad` blank cells on each side.
pub fn label_cols(text: &str, pad: u16) -> u16 {
    let w = UnicodeWidthStr::width(text) as u16;
    w.saturating_add(pad.saturating_mul(2))
}

/// Shared chip width that exactly centers the greatest number of labels.
///
/// A label can have equal left and right pads only when its display width has
/// the same parity as the chip. When the labels have mixed parity, this picks
/// the majority parity; ties keep the narrowest possible chip. This makes the
/// unavoidable one-cell imbalance affect the fewest labels.
pub fn chip_cols(labels: &[&str], pad: u16) -> u16 {
    let w = labels
        .iter()
        .map(|t| label_cols(t, pad))
        .max()
        .unwrap_or(pad.saturating_mul(2));

    let (even, odd) = labels.iter().fold((0usize, 0usize), |(even, odd), text| {
        if UnicodeWidthStr::width(*text) % 2 == 0 {
            (even + 1, odd)
        } else {
            (even, odd + 1)
        }
    });
    let preferred_parity = if odd > even { 1 } else { 0 };

    if w % 2 == preferred_parity {
        w
    } else {
        w.saturating_add(1)
    }
}

/// Pad `text` to exactly `width` display columns (centered).
///
/// When the gap is odd, the extra column goes on the **right**.
pub fn center_pad(text: &str, width: u16) -> String {
    pad_align(text, width, Align::Center)
}

fn clip_text(text: &str, width: u16) -> (String, u16) {
    let mut out = String::new();
    let mut tw = 0u16;
    for ch in text.chars() {
        let w = UnicodeWidthChar::width(ch).unwrap_or(0) as u16;
        if w == 0 {
            continue;
        }
        if tw + w > width {
            break;
        }
        out.push(ch);
        tw += w;
    }
    (out, tw)
}

fn pad_align(text: &str, width: u16, align: Align) -> String {
    let (core, tw) = clip_text(text, width);
    if tw >= width {
        return core;
    }
    let gap = width - tw;
    let (left, right) = match align {
        Align::Start => (0, gap),
        Align::End => (gap, 0),
        Align::Center => {
            let left = gap / 2;
            (left, gap - left)
        }
    };
    let mut s = String::with_capacity(width as usize);
    for _ in 0..left {
        s.push(' ');
    }
    s.push_str(&core);
    for _ in 0..right {
        s.push(' ');
    }
    s
}

/// Marker in the cell to the right of a double-width glyph. Skipped when
/// flushing ANSI so the terminal does not advance an extra column.
pub const WIDE_CONT: char = '\u{FFFE}';

/// One character cell. `'\0'` marks a *transparent* cell — it is skipped when a
/// surface is composited, letting lower layers show through.
#[derive(Clone, Copy, PartialEq, Eq, Debug)]
pub struct Cell {
    pub ch: char,
    pub style: Style,
}

impl Cell {
    pub const fn blank() -> Self {
        Cell {
            ch: ' ',
            style: Style::new(),
        }
    }

    pub const fn transparent() -> Self {
        Cell {
            ch: '\0',
            style: Style::new(),
        }
    }

    pub fn is_transparent(&self) -> bool {
        self.ch == '\0'
    }
}

impl Default for Cell {
    fn default() -> Self {
        Cell::blank()
    }
}

#[derive(Clone)]
pub struct Buffer {
    pub width: u16,
    pub height: u16,
    cells: Vec<Cell>,
}

impl Buffer {
    /// A buffer of blank (opaque space) cells — used for the screen root.
    pub fn blank(size: Size) -> Self {
        Buffer::filled(size, Cell::blank())
    }

    /// A buffer of transparent cells — used for overlay surfaces.
    pub fn transparent(size: Size) -> Self {
        Buffer::filled(size, Cell::transparent())
    }

    pub fn filled(size: Size, cell: Cell) -> Self {
        Buffer {
            width: size.width,
            height: size.height,
            cells: vec![cell; size.area() as usize],
        }
    }

    pub fn size(&self) -> Size {
        Size::new(self.width, self.height)
    }

    pub fn area(&self) -> Rect {
        Rect::new(0, 0, self.width, self.height)
    }

    fn index(&self, x: u16, y: u16) -> Option<usize> {
        if x < self.width && y < self.height {
            Some(y as usize * self.width as usize + x as usize)
        } else {
            None
        }
    }

    pub fn get(&self, x: u16, y: u16) -> Option<&Cell> {
        self.index(x, y).map(|i| &self.cells[i])
    }

    pub fn cell_mut(&mut self, x: u16, y: u16) -> Option<&mut Cell> {
        self.index(x, y).map(|i| &mut self.cells[i])
    }

    pub fn set(&mut self, x: u16, y: u16, ch: char, style: Style) {
        if let Some(c) = self.cell_mut(x, y) {
            c.ch = ch;
            // Patch, don't replace: `None` colours inherit the cell underneath.
            // Critical for panels — `set_line` clears short-line tails with
            // `Style::new()`, which must not punch black holes through a painted bg.
            c.style = c.style.patch(style);
        }
    }

    /// Write one grapheme cell, honouring East-Asian / emoji display width.
    /// Returns columns advanced (0, 1, or 2).
    fn put_glyph(&mut self, x: u16, y: u16, ch: char, style: Style, limit: u16) -> u16 {
        if x >= limit {
            return 0;
        }
        let w = UnicodeWidthChar::width(ch).unwrap_or(0);
        if w == 0 {
            // Combining / zero-width: keep layout stable; skip.
            return 0;
        }
        if w >= 2 {
            // Need two cells; if only one remains, fall back to a space.
            if x + 1 >= limit {
                self.set(x, y, ' ', style);
                return 1;
            }
            self.set(x, y, ch, style);
            self.set(x + 1, y, WIDE_CONT, style);
            return 2;
        }
        self.set(x, y, ch, style);
        1
    }

    /// Write `s` starting at `(x, y)`, clipping at the buffer edge. Returns the
    /// column just past the last glyph written.
    pub fn set_str(&mut self, x: u16, y: u16, s: &str, style: Style) -> u16 {
        let mut cx = x;
        for ch in s.chars() {
            if cx >= self.width {
                break;
            }
            let adv = self.put_glyph(cx, y, ch, style, self.width);
            if adv == 0 && UnicodeWidthChar::width(ch).unwrap_or(0) == 0 {
                continue;
            }
            if adv == 0 {
                break;
            }
            cx = cx.saturating_add(adv);
        }
        cx
    }

    /// Draw a styled line at `(x, y)`, each span keeping its own style, clipped
    /// to `max_width` cells (and the buffer edge).
    pub fn set_line(&mut self, x: u16, y: u16, line: &Line, max_width: u16) {
        self.set_line_on(x, y, line, max_width, Style::new());
    }

    /// Like [`set_line`], but every span is patched with `base` (typically a
    /// panel background) so fg-only highlight styles don't wipe the row bg.
    /// Always clears the remainder of the row so scroll / short lines cannot
    /// leave stale glyphs behind.
    pub fn set_line_on(&mut self, x: u16, y: u16, line: &Line, max_width: u16, base: Style) {
        let mut cx = x;
        let limit = x.saturating_add(max_width).min(self.width);
        for span in &line.spans {
            let st = span.style.patch(base);
            for ch in span.content.chars() {
                if cx >= limit {
                    // Still clear the rest of the row below.
                    break;
                }
                let adv = self.put_glyph(cx, y, ch, st, limit);
                if adv == 0 {
                    if UnicodeWidthChar::width(ch).unwrap_or(0) == 0 {
                        continue;
                    }
                    break;
                }
                cx = cx.saturating_add(adv);
            }
            if cx >= limit {
                break;
            }
        }
        // Clear vacated cells — required for scroll artifacts and width changes.
        while cx < limit {
            self.set(cx, y, ' ', base);
            cx += 1;
        }
    }

    /// Draw `lines` top-down within `area`, starting at logical index `scroll`,
    /// clipped to the area's height and width. The Paragraph-with-scroll of comb.
    pub fn set_lines(&mut self, area: Rect, lines: &[Line], scroll: usize) {
        self.set_lines_on(area, lines, scroll, Style::new());
    }

    /// Like [`set_lines`], patching every cell with `base` (e.g. panel bg).
    /// Empty rows in the viewport are always cleared.
    pub fn set_lines_on(&mut self, area: Rect, lines: &[Line], scroll: usize, base: Style) {
        for row in 0..area.height {
            let idx = scroll + row as usize;
            let y = area.y + row;
            match lines.get(idx) {
                Some(line) => self.set_line_on(area.x, y, line, area.width, base),
                None => {
                    self.paint(Rect::new(area.x, y, area.width, 1), base);
                }
            }
        }
    }

    /// Fill a rectangle with `ch`/`style` (intersected with the buffer).
    pub fn fill(&mut self, rect: Rect, ch: char, style: Style) {
        let r = rect.intersection(self.area());
        for y in r.y..r.bottom() {
            for x in r.x..r.right() {
                self.set(x, y, ch, style);
            }
        }
    }

    /// Paint a background over a rectangle, leaving spaces. Handy for panels.
    pub fn paint(&mut self, rect: Rect, style: Style) {
        self.fill(rect, ' ', style);
    }

    /// Fill a horizontal band with `style`, then draw `text` inside it.
    ///
    /// The whole band is painted first, then one padded line is written so chip
    /// backgrounds stay solid. [`Align::Center`] uses [`center_pad`].
    pub fn put_str_aligned(
        &mut self,
        x: u16,
        y: u16,
        width: u16,
        text: &str,
        style: Style,
        align: Align,
    ) {
        if width == 0 || y >= self.height {
            return;
        }
        let x = x.min(self.width);
        let width = width.min(self.width.saturating_sub(x));
        if width == 0 {
            return;
        }
        self.paint(Rect::new(x, y, width, 1), style);
        let padded = match align {
            Align::Center => center_pad(text, width),
            Align::Start => pad_align(text, width, Align::Start),
            Align::End => pad_align(text, width, Align::End),
        };
        self.set_line_on(
            x,
            y,
            &Line::from(crate::core::text::Span::styled(padded, style)),
            width,
            style,
        );
    }

    /// Fill `area` with `style` and center `text` horizontally + vertically.
    ///
    /// Use [`chip_cols`] to choose a shared width that centers as many labels
    /// as possible when their display widths have mixed parity.
    pub fn put_label(&mut self, area: Rect, text: &str, style: Style) {
        let area = area.intersection(self.area());
        if area.is_empty() {
            return;
        }
        self.paint(area, style);
        let y = area.y + area.height / 2;
        self.put_str_aligned(area.x, y, area.width, text, style, Align::Center);
    }

    /// Copy `src` onto this buffer at `(ox, oy)`. When `skip_transparent`, cells
    /// marked transparent in `src` don't overwrite what's underneath.
    pub fn blit(&mut self, ox: u16, oy: u16, src: &Buffer, skip_transparent: bool) {
        for sy in 0..src.height {
            for sx in 0..src.width {
                let cell = src.cells[sy as usize * src.width as usize + sx as usize];
                if skip_transparent && cell.is_transparent() {
                    continue;
                }
                self.set(ox + sx, oy + sy, cell.ch, cell.style);
            }
        }
    }

    /// The cells that differ from `prev` (same dimensions assumed), as
    /// `(x, y, cell)`, in row-major order — ready to emit to the terminal.
    pub fn diff<'a>(&'a self, prev: &Buffer) -> Vec<(u16, u16, &'a Cell)> {
        let mut out = Vec::new();
        if self.width != prev.width || self.height != prev.height {
            // Different geometry: treat everything as changed.
            for y in 0..self.height {
                for x in 0..self.width {
                    out.push((x, y, &self.cells[self.index(x, y).unwrap()]));
                }
            }
            return out;
        }
        for (i, (a, b)) in self.cells.iter().zip(prev.cells.iter()).enumerate() {
            if a != b {
                let x = (i % self.width as usize) as u16;
                let y = (i / self.width as usize) as u16;
                out.push((x, y, a));
            }
        }
        out
    }

    /// The whole buffer as text, rows joined by `\n` (transparent / wide-cont
    /// cells become spaces). Handy for snapshot-style assertions in tests.
    pub fn text(&self) -> String {
        let mut s = String::with_capacity((self.width as usize + 1) * self.height as usize);
        for y in 0..self.height {
            for x in 0..self.width {
                let ch = self.cells[y as usize * self.width as usize + x as usize].ch;
                s.push(if ch == '\0' || ch == WIDE_CONT {
                    ' '
                } else {
                    ch
                });
            }
            if y + 1 < self.height {
                s.push('\n');
            }
        }
        s
    }
}

#[cfg(test)]
mod tests {
    use super::*;
    use crate::core::style::{Color, Style};
    use crate::core::text::Span;

    #[test]
    fn set_str_clips_at_edge() {
        let mut b = Buffer::blank(Size::new(4, 1));
        let next = b.set_str(2, 0, "hello", Style::new());
        assert_eq!(next, 4); // stopped at the right edge
        let row: String = (0..4).map(|x| b.get(x, 0).unwrap().ch).collect();
        assert_eq!(row, "  he");
    }

    #[test]
    fn diff_reports_only_changes() {
        let a = Buffer::blank(Size::new(3, 1));
        let mut b = a.clone();
        b.set(1, 0, 'x', Style::new().fg(Color::rgb(9, 9, 9)));
        let d = b.diff(&a);
        assert_eq!(d.len(), 1);
        assert_eq!((d[0].0, d[0].1, d[0].2.ch), (1, 0, 'x'));
    }

    #[test]
    fn set_line_clears_remainder_of_row() {
        let mut b = Buffer::blank(Size::new(8, 1));
        b.set_str(0, 0, "abcdefgh", Style::new());
        b.set_line(0, 0, &Line::from("hi"), 8);
        let row: String = (0..8).map(|x| b.get(x, 0).unwrap().ch).collect();
        assert_eq!(row, "hi      ");
    }

    #[test]
    fn set_line_preserves_painted_panel_bg_on_tail() {
        let panel = Color::rgb(0x22, 0x22, 0x22);
        let mut b = Buffer::blank(Size::new(10, 1));
        b.paint(Rect::new(0, 0, 10, 1), Style::new().bg(panel));
        // Short line + clear remainder (Style::new base) must keep panel bg.
        b.set_line(
            0,
            0,
            &Line::from(Span::styled(
                "Settings",
                Style::new().fg(Color::rgb(0xe0, 0xe0, 0xe0)),
            )),
            10,
        );
        for x in 0..10 {
            assert_eq!(
                b.get(x, 0).unwrap().style.bg,
                Some(panel),
                "col {x} lost panel bg"
            );
        }
        assert_eq!(b.get(0, 0).unwrap().ch, 'S');
        assert_eq!(b.get(8, 0).unwrap().ch, ' ');
    }

    #[test]
    fn set_lines_clears_empty_viewport_rows() {
        let mut b = Buffer::blank(Size::new(4, 3));
        b.set_str(0, 0, "aaaa", Style::new());
        b.set_str(0, 1, "bbbb", Style::new());
        b.set_str(0, 2, "cccc", Style::new());
        let lines = vec![Line::from("x")];
        b.set_lines(Rect::new(0, 0, 4, 3), &lines, 0);
        assert_eq!(b.get(0, 0).unwrap().ch, 'x');
        assert_eq!(b.get(1, 0).unwrap().ch, ' ');
        assert_eq!(b.get(0, 1).unwrap().ch, ' ');
        assert_eq!(b.get(0, 2).unwrap().ch, ' ');
    }

    #[test]
    fn wide_glyph_reserves_two_cells() {
        let mut b = Buffer::blank(Size::new(4, 1));
        b.set_str(0, 0, "あ", Style::new());
        assert_eq!(b.get(0, 0).unwrap().ch, 'あ');
        assert_eq!(b.get(1, 0).unwrap().ch, WIDE_CONT);
        assert_eq!(b.get(2, 0).unwrap().ch, ' ');
    }

    #[test]
    fn chip_cols_prefers_the_majority_label_parity() {
        assert_eq!(chip_cols(&["MAKE", "MARK", "SEND"], 1), 6);
        assert_eq!(center_pad("MAKE", 6), " MAKE ");
        assert_eq!(center_pad("MARK", 6), " MARK ");
        assert_eq!(center_pad("SEND", 6), " SEND ");
    }

    #[test]
    fn put_label_preserves_shared_action_width() {
        let bg = Color::rgb(0x5a, 0x8f, 0xb0);
        let style = Style::new().bg(bg).fg(Color::rgb(0x11, 0x11, 0x11));
        let w = chip_cols(&["MAKE", "MARK", "SEND"], 1);
        assert_eq!(w, 6);
        for (word, expect) in [("MAKE", " MAKE "), ("MARK", " MARK "), ("SEND", " SEND ")] {
            let mut b = Buffer::blank(Size::new(w, 3));
            b.put_label(Rect::new(0, 0, w, 3), word, style);
            let mid: String = (0..w).map(|x| b.get(x, 1).unwrap().ch).collect();
            assert_eq!(mid, expect, "{word}");
            let text_width = UnicodeWidthStr::width(word);
            let pad = (w as usize - text_width) / 2;
            assert_eq!(&mid[..pad], " ".repeat(pad));
            assert_eq!(&mid[pad + text_width..], " ".repeat(pad));
        }
    }
}