cellix-render 0.6.4

Cell grid and pixel rasterizing for Cellix
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
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
use compact_str::CompactString;
use unicode_segmentation::UnicodeSegmentation;
use unicode_width::UnicodeWidthStr;

use crate::icon::{Icon, Piece};
use crate::measure::Font;
use crate::paint::{Bounds, Paint, UNIT, units};

/// Heights of a button, in rows.
/// Every button is one chip centred inside its row, so none stands past it and what's
/// painted is what takes a click: about 30 px of a 37 px row, the air the design's 24 of 32
/// gives its smaller text.
const BUTTON_HEIGHT: f32 = 0.8;
const LARGE_BUTTON_HEIGHT: f32 = BUTTON_HEIGHT;

#[derive(Clone, Copy, Debug, Default, PartialEq, Eq, Hash)]
pub struct Rgb(pub u8, pub u8, pub u8);

#[derive(Clone, Copy, Debug, Default, PartialEq, Eq, Hash)]
pub struct Style {
    pub fg: Rgb,
    pub bg: Rgb,
    pub bold: bool,
    pub italic: bool,
    pub underline: bool,
    /// The pixel tiers leave this cell's glyph out, because paint draws its text.
    pub painted: bool,
}

/// `text` with nothing a terminal would act on: a control character, as a file, an agent's
/// output or a transcript can hold, is shown by its picture (`␛` for escape), the way VS Code
/// shows one, so it can't move the cursor, set the title or write the clipboard. A tab is a
/// space.
fn visible(text: &str) -> std::borrow::Cow<'_, str> {
    let control = |c: char| c.is_control();
    if !text.contains(control) {
        return std::borrow::Cow::Borrowed(text);
    }
    let shown = text
        .chars()
        .map(|c| match c {
            '\t' => ' ',
            '\0'..='\u{1f}' => char::from_u32(0x2400 + c as u32).unwrap_or('\u{fffd}'),
            '\u{7f}' => '\u{2421}',
            c if c.is_control() => '\u{fffd}',
            c => c,
        })
        .collect();
    std::borrow::Cow::Owned(shown)
}

/// A screen cell. An empty symbol is the right half of a wide character.
#[derive(Clone, Debug, PartialEq, Eq, Hash)]
pub struct Cell {
    pub symbol: CompactString,
    pub style: Style,
}

/// Copies a run of cells within the grid's own cells. `Cell` isn't `Copy`, so this clones.
trait CopyCells {
    fn copy_within_cells(&mut self, from: std::ops::Range<usize>, to: usize);
}

impl CopyCells for Vec<Cell> {
    fn copy_within_cells(&mut self, from: std::ops::Range<usize>, to: usize) {
        for (n, at) in from.enumerate() {
            let cell = self[at].clone();
            self[to + n] = cell;
        }
    }
}

#[derive(Clone, Copy, Debug, Default, PartialEq, Eq)]
pub struct Rect {
    pub x: u16,
    pub y: u16,
    pub w: u16,
    pub h: u16,
}

impl Rect {
    pub fn new(x: u16, y: u16, w: u16, h: u16) -> Self {
        Self { x, y, w, h }
    }

    pub fn contains(&self, x: u16, y: u16) -> bool {
        x >= self.x && x < self.x + self.w && y >= self.y && y < self.y + self.h
    }

    pub fn right(&self) -> u16 {
        self.x + self.w
    }

    pub fn bottom(&self) -> u16 {
        self.y + self.h
    }
}

/// A span of changed cells on one row.
#[derive(Clone, Copy, Debug, PartialEq, Eq)]
pub struct Run {
    pub x: u16,
    pub y: u16,
    pub len: u16,
}

#[derive(Clone, Debug, PartialEq, Eq)]
pub struct Grid {
    width: u16,
    height: u16,
    cells: Vec<Cell>,
    pub cursor: Option<(u16, u16)>,
    /// The caret's exact place on the pixel tiers, when text isn't on whole cells.
    pub caret: Option<Bounds>,
    /// Drawn over the cells on the pixel tiers, in order.
    pub paint: Vec<Paint>,
    /// Columns where a new band starts, in order. Each band of a row is its own tile, so text
    /// that scrolls isn't tied to whatever the sidebar or minimap shows at that height.
    pub bands: Vec<u16>,
}

impl Grid {
    /// Moves rows `top..=bottom` up by `by` rows, or down if negative, across the whole width,
    /// as a terminal's scroll region does. The rows left behind hold nothing any real cell
    /// matches, so the next diff draws them.
    pub fn scroll_rows(&mut self, top: u16, bottom: u16, by: i32) {
        let width = self.width as usize;
        let (top, bottom) = (
            top as usize,
            (bottom as usize).min(self.height as usize - 1),
        );
        if top > bottom || by == 0 {
            return;
        }
        let rows = bottom + 1 - top;
        let n = (by.unsigned_abs() as usize).min(rows);
        let unknown = Cell {
            symbol: CompactString::const_new("\u{fffe}"),
            style: Style::default(),
        };
        let row = |y: usize| y * width..(y + 1) * width;
        if by > 0 {
            for y in top..bottom + 1 - n {
                self.cells.copy_within_cells(row(y + n), row(y).start);
            }
            for y in bottom + 1 - n..=bottom {
                self.cells[row(y)].fill(unknown.clone());
            }
        } else {
            for y in (top + n..=bottom).rev() {
                self.cells.copy_within_cells(row(y - n), row(y).start);
            }
            for y in top..top + n {
                self.cells[row(y)].fill(unknown.clone());
            }
        }
    }

    pub fn new(width: u16, height: u16, style: Style) -> Self {
        let blank = Cell {
            symbol: " ".into(),
            style,
        };
        Self {
            width,
            height,
            cells: vec![blank; width as usize * height as usize],
            cursor: None,
            caret: None,
            paint: Vec::new(),
            bands: Vec::new(),
        }
    }

    /// Text in the interface font on the pixel tiers, and ordinary cells on the cell tier.
    pub fn label(&mut self, x: u16, y: u16, text: &str, style: Style, max_x: u16) -> u16 {
        self.label_at(x as f32, x, y, text, style, max_x, 100)
    }

    /// How much has been painted so far, to `lift_since` later.
    pub fn painted_so_far(&self) -> usize {
        self.paint.len()
    }

    /// Moves what was painted since `from` up by `rows`, which only the pixel tiers can show:
    /// the cells under it stay in their rows, and so does where a click lands.
    pub fn lift_since(&mut self, from: usize, rows: f32) {
        let by = units(rows);
        for paint in self.paint.iter_mut().skip(from) {
            paint.lift(by);
        }
    }

    /// A label starting between cells, at a size relative to the interface font.
    #[allow(clippy::too_many_arguments)]
    pub fn label_at(
        &mut self,
        x_px: f32,
        x: u16,
        y: u16,
        text: &str,
        style: Style,
        max_x: u16,
        size: u8,
    ) -> u16 {
        let end = self.put_str(
            x,
            y,
            text,
            Style {
                painted: true,
                ..style
            },
            max_x,
        );
        if y < self.height {
            self.paint.push(Paint::Text {
                x: units(x_px),
                y: y as i32 * UNIT,
                max_width: units(max_x.min(self.width) as f32 - x_px),
                text: text.to_string(),
                color: style.fg,
                font: Font {
                    bold: style.bold,
                    size,
                    ..Font::default()
                },
                align_right: false,
                centred: false,
                underline: false,
            });
        }
        end
    }

    /// Paints a run of text with no cells behind it, at a place in cells and rows.
    pub fn text_run(
        &mut self,
        (x, y): (f32, f32),
        max_x: f32,
        text: &str,
        (color, underline): (Rgb, bool),
        font: Font,
    ) {
        self.paint.push(Paint::Text {
            x: units(x),
            y: units(y),
            max_width: units(max_x - x),
            text: text.to_string(),
            color,
            font,
            align_right: false,
            centred: false,
            underline,
        });
    }

    /// Puts the caret at a place between cells, `height` rows tall and centred on `y`.
    pub fn set_caret(&mut self, x: f32, y: u16, height: f32) {
        self.cursor = Some((x as u16, y));
        let top = y as f32 + (1.0 - height) / 2.0;
        self.caret = Some(Bounds::cells(x, top, 0.0, height));
    }

    /// A label whose right edge sits at `right`, since proportional text is narrower than
    /// its cells and would otherwise leave a gap at the end.
    pub fn label_right(&mut self, right: u16, y: u16, text: &str, style: Style, size: u8) {
        let cells = text.chars().count() as u16;
        let x = right.saturating_sub(cells);
        self.put_str(
            x,
            y,
            text,
            Style {
                painted: true,
                ..style
            },
            right,
        );
        self.paint.push(Paint::Text {
            x: units(right as f32),
            y: y as i32 * UNIT,
            max_width: units(right as f32),
            text: text.to_string(),
            color: style.fg,
            font: Font {
                bold: style.bold,
                size,
                ..Font::default()
            },
            align_right: true,
            centred: false,
            underline: false,
        });
    }

    /// A rounded box inside `rect`, with square corners cleaned up behind it.
    pub fn field(&mut self, rect: Rect, behind: Rgb, fill: Rgb, border: Rgb) {
        self.panel(Bounds::inset(rect, 0.0, 0.0), 0, Some(behind), None);
        // Inset a little from its row, so fields stacked in rows next to each other keep
        // their air.
        self.panel(
            Bounds::inset(rect, 0.15, 0.11),
            22,
            Some(fill),
            Some(border),
        );
    }

    /// A rounded button in a row of its own. The cell tier shows `cells`, which may be
    /// wider than the label.
    pub fn button(&mut self, x: u16, y: u16, cells: &str, label: &str, fill: Rgb, style: Style) {
        self.button_of((BUTTON_HEIGHT, 0.0), (x, y), cells, label, fill, style);
    }

    /// A button that stands past its row, for where there's air around it.
    pub fn large_button(
        &mut self,
        x: u16,
        y: u16,
        cells: &str,
        label: &str,
        fill: Rgb,
        style: Style,
    ) {
        self.button_of(
            (LARGE_BUTTON_HEIGHT, 0.0),
            (x, y),
            cells,
            label,
            fill,
            style,
        );
    }

    /// A large button centred between `y` and the row below it.
    pub fn large_button_between(
        &mut self,
        x: u16,
        y: u16,
        cells: &str,
        label: &str,
        fill: Rgb,
        style: Style,
    ) {
        self.button_of(
            (LARGE_BUTTON_HEIGHT, 0.5),
            (x, y),
            cells,
            label,
            fill,
            style,
        );
    }

    fn button_of(
        &mut self,
        (height, drop): (f32, f32),
        (x, y): (u16, u16),
        cells: &str,
        label: &str,
        fill: Rgb,
        style: Style,
    ) {
        let width = cells.chars().count() as u16;
        let (left, top, right, bottom) = button_span((height, drop), (x, y), width);
        self.put_str(
            x,
            y,
            cells,
            Style {
                painted: true,
                ..style
            },
            self.width,
        );
        let bounds = Bounds::cells(left, top, right - left, bottom - top);
        self.panel(bounds, 22, Some(fill), None);
        let middle = x as f32 + width as f32 / 2.0;
        self.centred_text(middle, y as f32 + drop, label, style.fg, 90);
    }

    /// Moves what was painted last down by part of a row.
    pub fn drop_last(&mut self, by: f32) {
        match self.paint.last_mut() {
            Some(Paint::Text { y, .. }) => *y += units(by),
            Some(Paint::Icon { bounds, .. }) => bounds.y += units(by),
            _ => {}
        }
    }

    /// Paints text centred on `x`, at a place in rows.
    pub fn centred_text(&mut self, x: f32, y: f32, text: &str, color: Rgb, size: u8) {
        self.paint.push(Paint::Text {
            x: units(x),
            y: units(y),
            max_width: units(self.width as f32),
            text: text.to_string(),
            color,
            font: Font {
                size,
                ..Font::default()
            },
            align_right: false,
            centred: true,
            underline: false,
        });
    }

    pub fn panel(&mut self, bounds: Bounds, radius: u8, fill: Option<Rgb>, border: Option<Rgb>) {
        self.paint.push(Paint::Panel {
            bounds,
            radius,
            fill,
            border,
            shadow: false,
            dashed: false,
        });
    }

    /// A panel with its border in dashes, for something offered rather than settled.
    pub fn dashed_panel(&mut self, bounds: Bounds, radius: u8, fill: Option<Rgb>, border: Rgb) {
        self.paint.push(Paint::Panel {
            bounds,
            radius,
            fill,
            border: Some(border),
            shadow: false,
            dashed: true,
        });
    }

    /// A panel floating above the page, with a soft shadow.
    pub fn popup(&mut self, bounds: Bounds, radius: u8, fill: Rgb, border: Rgb) {
        self.paint.push(Paint::Panel {
            bounds,
            radius,
            fill: Some(fill),
            border: Some(border),
            shadow: true,
            dashed: false,
        });
    }

    /// A see-through wash of `color` over `bounds`, `alpha` out of 255, with a border.
    pub fn wash(
        &mut self,
        bounds: Bounds,
        radius: u8,
        (color, alpha): (Rgb, u8),
        border: Option<Rgb>,
    ) {
        self.paint.push(Paint::Wash {
            bounds,
            radius,
            color,
            alpha,
            border,
        });
    }

    pub fn hairline(&mut self, bounds: Bounds, color: Rgb) {
        self.paint.push(Paint::Hairline { bounds, color });
    }

    /// A small icon, one row tall, with its left edge at `x`. `fallback` is what the cell
    /// tier shows there.
    pub fn glyph(&mut self, x: f32, y: u16, icon: Icon, color: Rgb, fallback: &str, style: Style) {
        let cell = x.floor() as u16;
        if cell < self.width && y < self.height {
            self.put_str(
                cell,
                y,
                fallback,
                Style {
                    painted: true,
                    ..style
                },
                self.width,
            );
        }
        self.paint.push(Paint::Icon {
            bounds: Bounds::cells(x, y as f32 + 0.14, 2.0, 0.72),
            icon,
            color,
        });
    }

    /// Paint that touches row `y`, in drawing order.
    pub fn corners(&mut self, bounds: Bounds, radius: u8, color: Rgb, which: u8) {
        self.paint.push(Paint::Corners {
            bounds,
            radius,
            color,
            which,
        });
    }

    pub fn paint_on_row(&self, y: u16) -> impl Iterator<Item = &Paint> {
        let (top, bottom) = (y as i32 * UNIT, (y as i32 + 1) * UNIT);
        self.paint.iter().filter(move |p| {
            let (from, to) = p.span();
            from < bottom && to > top
        })
    }

    pub fn width(&self) -> u16 {
        self.width
    }

    pub fn height(&self) -> u16 {
        self.height
    }

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

    fn index(&self, x: u16, y: u16) -> usize {
        y as usize * self.width as usize + x as usize
    }

    pub fn cell(&self, x: u16, y: u16) -> &Cell {
        &self.cells[self.index(x, y)]
    }

    pub fn row(&self, y: u16) -> &[Cell] {
        let start = self.index(0, y);
        &self.cells[start..start + self.width as usize]
    }

    pub fn fill(&mut self, rect: Rect, style: Style) {
        for y in rect.y..rect.bottom().min(self.height) {
            for x in rect.x..rect.right().min(self.width) {
                self.put_cell(x, y, " ", style);
            }
        }
    }

    fn put_cell(&mut self, x: u16, y: u16, symbol: &str, style: Style) {
        // Overwriting half of a wide character blanks its other half.
        let i = self.index(x, y);
        if self.cells[i].symbol.is_empty() && x > 0 {
            self.cells[i - 1].symbol = " ".into();
        }
        if x + 1 < self.width && self.cells[i + 1].symbol.is_empty() {
            self.cells[i + 1].symbol = " ".into();
        }
        self.cells[i] = Cell {
            symbol: symbol.into(),
            style,
        };
    }

    /// Writes text stopping before `max_x`, and returns the column after it. A wide
    /// character that doesn't fit becomes a space.
    pub fn put_str(&mut self, x: u16, y: u16, text: &str, style: Style, max_x: u16) -> u16 {
        if y >= self.height {
            return x;
        }
        let max_x = max_x.min(self.width);
        let mut at = x;
        let text = visible(text);
        for g in text.graphemes(true) {
            if at >= max_x {
                break;
            }
            let width = g.width() as u16;
            if width == 0 {
                continue;
            }
            if at + width > max_x {
                self.put_cell(at, y, " ", style);
                return at + 1;
            }
            self.put_cell(at, y, g, style);
            if width == 2 {
                self.put_cell(at + 1, y, "", style);
            }
            at += width;
        }
        at
    }

    /// Places a multi-cell icon with its top-left corner at `x`, `y`.
    pub fn put_icon(&mut self, x: u16, y: u16, icon: Icon, size: (u8, u8), style: Style) {
        for row in 0..size.1 {
            for column in 0..size.0 {
                let (cx, cy) = (x + u16::from(column), y + u16::from(row));
                if cx < self.width && cy < self.height {
                    let piece = Piece {
                        icon,
                        width: size.0,
                        height: size.1,
                        column,
                        row,
                    };
                    self.put_cell(cx, cy, piece.symbol().encode_utf8(&mut [0; 4]), style);
                }
            }
        }
    }

    /// Overrides the style of existing cells, keeping their text.
    pub fn set_style(&mut self, rect: Rect, f: impl Fn(Style) -> Style) {
        for y in rect.y..rect.bottom().min(self.height) {
            for x in rect.x..rect.right().min(self.width) {
                let i = self.index(x, y);
                self.cells[i].style = f(self.cells[i].style);
            }
        }
    }

    /// Spans that differ from `prev`. A size change makes the whole grid differ.
    pub fn diff(&self, prev: &Grid) -> Vec<Run> {
        let same_size = self.width == prev.width && self.height == prev.height;
        let mut runs = Vec::new();
        for y in 0..self.height {
            if same_size && self.row(y) == prev.row(y) {
                continue;
            }
            let mut x = 0;
            while x < self.width {
                if same_size && self.cell(x, y) == prev.cell(x, y) {
                    x += 1;
                    continue;
                }
                let start = x;
                while x < self.width && !(same_size && self.cell(x, y) == prev.cell(x, y)) {
                    x += 1;
                }
                // A run must never begin on the right half of a wide character.
                let start = if self.cell(start, y).symbol.is_empty() && start > 0 {
                    start - 1
                } else {
                    start
                };
                runs.push(Run {
                    x: start,
                    y,
                    len: x - start,
                });
            }
        }
        runs
    }

    /// The text of a row, for tests and copy.
    pub fn row_text(&self, y: u16) -> String {
        self.row(y).iter().map(|c| c.symbol.as_str()).collect()
    }

    /// Everything on screen, one line per row with trailing spaces removed.
    pub fn text(&self) -> String {
        (0..self.height)
            .map(|y| self.row_text(y).trim_end().to_string())
            .collect::<Vec<_>>()
            .join("\n")
    }
}

/// Where a button is painted, as (left, top, right, bottom) in columns and rows.
fn button_span((height, drop): (f32, f32), (x, y): (u16, u16), width: u16) -> (f32, f32, f32, f32) {
    let top = y as f32 + drop + (1.0 - height) / 2.0;
    (x as f32, top, (x + width) as f32, top + height)
}

/// Whether a click at `at` (columns and rows, with their fractions) lands on the large
/// button `Grid::large_button` paints at `(x, y)`, `width` cells wide. It takes the whole
/// of what's painted, which stands a little past its row above and below. A terminal
/// without pixel reports gives the middle of a cell, so there it's the button's row.
pub fn on_large_button((x, y): (u16, u16), width: u16, at: (f32, f32)) -> bool {
    let (left, top, right, bottom) = button_span((LARGE_BUTTON_HEIGHT, 0.0), (x, y), width);
    at.0 >= left && at.0 < right && at.1 >= top && at.1 < bottom
}

#[cfg(test)]
mod tests {

    #[test]
    fn control_characters_never_reach_a_cell() {
        let style = Style::default();
        let mut grid = Grid::new(40, 2, style);
        let text = "a\x1b]52;c;aGk=\x07b\r\n\x7f\u{9b}2Jc\td";
        let end = grid.put_str(0, 0, text, style, 40);
        let row: String = (0..end)
            .map(|x| grid.cell(x, 0).symbol.to_string())
            .collect();
        assert_eq!(row, "a␛]52;c;aGk=␇b␍␊␡\u{fffd}2Jc d");
        assert!(row.chars().all(|c| !c.is_control()), "{row:?}");
    }

    use super::*;

    fn grid() -> Grid {
        Grid::new(10, 3, Style::default())
    }

    #[test]
    fn diff_emits_only_changed_cells() {
        let before = grid();
        let mut after = grid();
        after.put_str(2, 1, "ab", Style::default(), 10);
        after.put_str(7, 1, "c", Style::default(), 10);
        assert_eq!(
            after.diff(&before),
            vec![Run { x: 2, y: 1, len: 2 }, Run { x: 7, y: 1, len: 1 }]
        );
        assert!(before.diff(&before.clone()).is_empty());
    }

    #[test]
    fn resize_redraws_everything() {
        let runs = grid().diff(&Grid::new(4, 4, Style::default()));
        assert_eq!(runs.len(), 3);
        assert!(runs.iter().all(|r| r.len == 10));
    }

    #[test]
    fn wide_characters_take_two_cells() {
        let mut g = grid();
        let end = g.put_str(0, 0, "日本", Style::default(), 10);
        assert_eq!(end, 4);
        assert_eq!(g.cell(0, 0).symbol, "日");
        assert_eq!(g.cell(1, 0).symbol, "");
        assert_eq!(g.row_text(0).trim_end(), "日本");
    }

    #[test]
    fn overwriting_half_a_wide_character_blanks_the_other_half() {
        let mut g = grid();
        g.put_str(0, 0, "日", Style::default(), 10);
        g.put_str(1, 0, "x", Style::default(), 10);
        assert_eq!(g.cell(0, 0).symbol, " ");
        assert_eq!(g.cell(1, 0).symbol, "x");
    }

    #[test]
    fn wide_character_at_the_edge_becomes_a_space() {
        let mut g = grid();
        g.put_str(9, 0, "日", Style::default(), 10);
        assert_eq!(g.cell(9, 0).symbol, " ");
    }

    #[test]
    fn combining_marks_stay_in_one_cell() {
        let mut g = grid();
        let end = g.put_str(0, 0, "e\u{301}x", Style::default(), 10);
        assert_eq!(end, 2);
        assert_eq!(g.cell(0, 0).symbol, "e\u{301}");
    }

    #[test]
    fn diff_never_starts_on_a_continuation_cell() {
        let mut before = grid();
        before.put_str(0, 0, "日", Style::default(), 10);
        let mut after = grid();
        after.put_str(
            0,
            0,
            "日",
            Style {
                bold: true,
                ..Style::default()
            },
            10,
        );
        let mut only_right = before.clone();
        only_right.cells[1].style.bold = true;
        assert_eq!(only_right.diff(&before)[0].x, 0);
        assert_eq!(after.diff(&before)[0].x, 0);
    }
}