embers-cli 0.4.0

Interactive terminal UI and automation CLI for the Embers terminal multiplexer.
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
//! What a program in a pane writes must look the same on the host terminal
//! after Embers has drawn it as it would if written straight to a terminal.
//!
//! Both sides are read through `alacritty_terminal`, the emulator panes run
//! on: one parses the program's bytes directly, the other parses the frame
//! Embers draws from the pane's snapshot. Gaps in the emulation are the same
//! on both sides and cancel out, so a difference is Embers' own: the
//! snapshot, the client grid or the frame encoding.

use alacritty_terminal::event::VoidListener;
use alacritty_terminal::grid::Dimensions;
use alacritty_terminal::index::{Column, Line};
use alacritty_terminal::term::cell::Flags;
use alacritty_terminal::term::test::TermSize;
use alacritty_terminal::term::{Config, Term};
use alacritty_terminal::vte::ansi::{self, Color, NamedColor, Rgb};
use embers_client::{HostCapabilities, RenderGrid};
use embers_core::{PtySize, Size};
use embers_server::{AlacrittyTerminalBackend, TerminalBackend};
use proptest::prelude::*;

use super::frame_bytes;

const COLUMNS: u16 = 16;
const ROWS: u16 = 5;

/// Flags that change how a cell looks. Hidden is left out: Embers draws a
/// hidden cell as a blank instead of passing the attribute on.
const VISIBLE_FLAGS: Flags = Flags::BOLD
    .union(Flags::DIM)
    .union(Flags::ITALIC)
    .union(Flags::ALL_UNDERLINES)
    .union(Flags::INVERSE)
    .union(Flags::STRIKEOUT)
    .union(Flags::WIDE_CHAR);

/// One cell as the user sees it.
#[derive(Clone, Debug, PartialEq, Eq)]
struct Shown {
    text: String,
    fg: Shade,
    bg: Shade,
    underline_color: Option<Shade>,
    flags: Flags,
    link: Option<String>,
}

/// A color as the host resolves it. Embers sends the 16 named colors as
/// their palette indices, which the host paints the same.
#[derive(Clone, Copy, Debug, PartialEq, Eq)]
enum Shade {
    Default,
    Indexed(u8),
    Rgb(Rgb),
}

impl Shown {
    fn is_blank(&self) -> bool {
        self.text == " "
            && self.bg == Shade::Default
            && !self
                .flags
                .intersects(Flags::INVERSE | Flags::ALL_UNDERLINES | Flags::STRIKEOUT)
    }
}

fn shade(color: Color) -> Shade {
    match color {
        Color::Spec(rgb) => Shade::Rgb(rgb),
        Color::Indexed(index) => Shade::Indexed(index),
        Color::Named(named) => match named {
            NamedColor::Foreground
            | NamedColor::Background
            | NamedColor::Cursor
            | NamedColor::BrightForeground
            | NamedColor::DimForeground => Shade::Default,
            NamedColor::DimBlack => Shade::Indexed(0),
            NamedColor::DimRed => Shade::Indexed(1),
            NamedColor::DimGreen => Shade::Indexed(2),
            NamedColor::DimYellow => Shade::Indexed(3),
            NamedColor::DimBlue => Shade::Indexed(4),
            NamedColor::DimMagenta => Shade::Indexed(5),
            NamedColor::DimCyan => Shade::Indexed(6),
            NamedColor::DimWhite => Shade::Indexed(7),
            named => Shade::Indexed(named as u8),
        },
    }
}

fn terminal() -> (Term<VoidListener>, ansi::Processor) {
    let size = TermSize::new(usize::from(COLUMNS), usize::from(ROWS));
    (
        Term::new(Config::default(), &size, VoidListener),
        ansi::Processor::new(),
    )
}

/// The screen as the user sees it: each row's cells up to the last one that
/// shows anything.
///
/// A hidden glyph shows as a blank in each column it covers. Half a wide
/// character whose other half was overwritten or erased shows as a plain
/// blank: the emulator keeps that half, with its style, but a terminal shows
/// neither half, and Ghostty clears the half that is left to a blank cell.
fn screen(term: &Term<VoidListener>) -> Vec<Vec<Shown>> {
    let grid = term.grid();
    let columns = grid.columns();
    (0..grid.screen_lines())
        .map(|line| {
            let row = &grid[Line(line as i32)];
            let flags = |column: usize| row[Column(column)].flags;
            let mut cells = Vec::new();
            for column in 0..columns {
                let cell = &row[Column(column)];
                let shown = |text: &str, flags: Flags| Shown {
                    text: text.to_owned(),
                    fg: shade(cell.fg),
                    bg: shade(cell.bg),
                    underline_color: cell.underline_color().map(shade),
                    flags: flags & VISIBLE_FLAGS,
                    link: cell.hyperlink().map(|link| link.uri().to_owned()),
                };
                let blank_flags = cell.flags - Flags::WIDE_CHAR;
                let broken_half = || Shown {
                    text: " ".to_owned(),
                    fg: Shade::Default,
                    bg: Shade::Default,
                    underline_color: None,
                    flags: Flags::empty(),
                    link: None,
                };
                if cell.flags.contains(Flags::LEADING_WIDE_CHAR_SPACER) {
                    continue;
                }
                if cell.flags.contains(Flags::WIDE_CHAR_SPACER) {
                    if column == 0 || !flags(column - 1).contains(Flags::WIDE_CHAR) {
                        cells.push(broken_half());
                    }
                    continue;
                }
                if cell.flags.contains(Flags::WIDE_CHAR)
                    && (column + 1 == columns
                        || !flags(column + 1).contains(Flags::WIDE_CHAR_SPACER))
                {
                    cells.push(broken_half());
                    continue;
                }
                if cell.flags.contains(Flags::HIDDEN) {
                    let width = if cell.flags.contains(Flags::WIDE_CHAR) {
                        2
                    } else {
                        1
                    };
                    cells.extend((0..width).map(|_| shown(" ", blank_flags)));
                    continue;
                }
                let mut text = String::new();
                if cell.c == '\t' {
                    text.push(' ');
                } else {
                    text.push(cell.c);
                    // Alacritty gives each part of an emoji sequence a cell
                    // of its own, which Embers draws without the selector
                    // and joiner so no host draws the part wide.
                    text.extend(
                        cell.zerowidth()
                            .into_iter()
                            .flatten()
                            .filter(|&&ch| ch != '\u{FE0F}' && ch != '\u{200D}'),
                    );
                }
                // Hosts disagree on a regional indicator alone, half a flag,
                // which Embers draws as U+FFFD; whole flags it draws as they
                // are. Either way only the columns can be compared.
                if text
                    .chars()
                    .any(|ch| ('\u{1F1E6}'..='\u{1F1FF}').contains(&ch))
                {
                    text = "\u{FFFD}".to_owned();
                }
                cells.push(shown(&text, cell.flags));
            }
            while cells.last().is_some_and(Shown::is_blank) {
                cells.pop();
            }
            cells
        })
        .collect()
}

/// The screen a program writing `bytes` sees on a terminal of its own.
fn direct(bytes: &[u8]) -> Vec<Vec<Shown>> {
    let (mut term, mut parser) = terminal();
    parser.advance(&mut term, bytes);
    screen(&term)
}

/// The screen the host shows after Embers draws a pane the program wrote
/// `bytes` to, filling the whole terminal.
fn through_embers(bytes: &[u8]) -> Vec<Vec<Shown>> {
    let mut pane = AlacrittyTerminalBackend::new(PtySize::new(COLUMNS, ROWS), 0);
    pane.ingest_bytes(bytes);
    let snapshot = pane.visible_snapshot(0, PtySize::new(COLUMNS, ROWS), None);

    let mut grid = RenderGrid::new(COLUMNS, ROWS);
    for (row, line) in snapshot.lines.iter().enumerate() {
        grid.put_snapshot_line(0, row as u16, COLUMNS, line);
    }
    let frame = frame_bytes(
        &grid,
        Size {
            width: COLUMNS,
            height: ROWS,
        },
        None,
        None,
        HostCapabilities::ALL,
        b"",
    );

    let (mut host, mut parser) = terminal();
    parser.advance(&mut host, &frame);
    screen(&host)
}

/// Where the screen through Embers first differs from the direct one.
fn first_difference(bytes: &[u8]) -> Option<String> {
    let embers = through_embers(bytes);
    let direct = direct(bytes);
    let (row, (embers, direct)) = embers
        .iter()
        .zip(&direct)
        .enumerate()
        .find(|(_, (embers, direct))| embers != direct)?;
    let column = embers
        .iter()
        .zip(direct)
        .position(|(embers, direct)| embers != direct)
        .unwrap_or(embers.len().min(direct.len()));
    Some(format!(
        "bytes {:?} differ on row {row} at cell {column}:\n through Embers: {:?}\n direct:         {:?}",
        String::from_utf8_lossy(bytes),
        embers.get(column),
        direct.get(column),
    ))
}

fn assert_round_trip(bytes: &[u8]) {
    if let Some(difference) = first_difference(bytes) {
        panic!("{difference}");
    }
}

#[test]
fn plain_text_and_line_editing() {
    assert_round_trip(b"hello\r\nworld\x1b[1;3Hxy\x1b[2;2H\x1b[K\x1b[3;1Habc\tdef");
}

#[test]
fn colors_and_attributes() {
    assert_round_trip(
        b"\x1b[1;31mred\x1b[0m \x1b[2;3;9mdim\x1b[0m\r\n\
          \x1b[38;5;200;48;2;1;2;3mx\x1b[7my\x1b[0m\r\n\
          \x1b[92;44mbright\x1b[39;49m",
    );
}

#[test]
fn underline_styles_and_colors() {
    assert_round_trip(b"\x1b[4mone\x1b[4:3;58:2::255:0:0mtwo\x1b[4:2;58:5:4mthree\x1b[0m");
}

#[test]
fn trailing_painted_cells() {
    assert_round_trip(b"a\x1b[41m   \x1b[0m\r\nb\x1b[7m  \x1b[0m\r\nc\x1b[4m  \x1b[0m");
}

#[test]
fn hidden_text() {
    assert_round_trip(b"see \x1b[8msecret\x1b[0m done");
}

#[test]
fn hyperlinks() {
    assert_round_trip(b"go \x1b]8;;https://example.com\x1b\\here\x1b]8;;\x1b\\ now");
}

#[test]
fn wide_and_combining_characters() {
    assert_round_trip("漢字 e\u{301} 👍\r\n0123456789abcde漢".as_bytes());
}

/// Transgender flag: white flag, VS16, ZWJ, transgender symbol, VS16.
const FLAG: &str = "\u{1F3F3}\u{FE0F}\u{200D}\u{26A7}\u{FE0F}";

#[test]
fn emoji_sequences_keep_the_columns_the_emulator_gave_them() {
    // In the middle of a row, wrapped at its last column, overwritten from
    // the start, and a symbol made wide by VS16 on its own.
    assert_round_trip(format!("ab {FLAG} cd").as_bytes());
    assert_round_trip(format!("0123456789abcde{FLAG}xy").as_bytes());
    assert_round_trip(format!("\x1b[1;6H{FLAG}x\x1b[1;6Ha").as_bytes());
    assert_round_trip("a \u{26A7}\u{FE0F} b \u{2764}\u{FE0F} c #\u{FE0F}\u{20E3}".as_bytes());
}

#[test]
fn erase_with_background_color() {
    assert_round_trip(b"\x1b[44m\x1b[2J\x1b[Htext\x1b[0m");
}

#[test]
fn screen_alignment_pattern() {
    assert_round_trip(b"\x1b#8");
}

/// One step of what a program might write.
fn step() -> impl Strategy<Value = Vec<u8>> {
    let text = prop::collection::vec(
        prop::sample::select(vec![
            "a",
            "Z",
            " ",
            "~",
            "漢",
            "e\u{301}",
            "👍",
            "\t",
            "\r",
            "\n",
            FLAG,
            "\u{26A7}\u{FE0F}",
            "\u{1F1F8}",
        ]),
        1..8,
    )
    .prop_map(|parts| parts.concat().into_bytes());
    let sgr = prop::sample::select(vec![
        "0",
        "1",
        "2",
        "3",
        "4",
        "4:2",
        "4:3",
        "4:4",
        "4:5",
        "7",
        "8",
        "9",
        "22",
        "24",
        "27",
        "31",
        "42",
        "95",
        "106",
        "38;5;123",
        "48;5;7",
        "38;2;10;20;30",
        "48;2;200;0;100",
        "58:2::1:2:3",
        "58:5:9",
        "39",
        "49",
        "59",
    ])
    .prop_map(|sgr| format!("\x1b[{sgr}m").into_bytes());
    let cursor = (1..=ROWS, 1..=COLUMNS)
        .prop_map(|(row, column)| format!("\x1b[{row};{column}H").into_bytes());
    let edit = prop::sample::select(vec![
        "\x1b[K",
        "\x1b[1K",
        "\x1b[2K",
        "\x1b[J",
        "\x1b[@",
        "\x1b[2@",
        "\x1b[P",
        "\x1b[2P",
        "\x1b[X",
        "\x1b[2X",
        "\x1b[L",
        "\x1b[M",
        "\x1b[S",
        "\x1b[T",
        "\x1b[2;4r",
        "\x1b[r",
        "\x1bM",
        "\x1b#8",
        "\x1b]8;;https://a.example\x1b\\",
        "\x1b]8;;\x1b\\",
    ])
    .prop_map(|edit| edit.as_bytes().to_vec());
    prop_oneof![3 => text, 2 => sgr, 1 => cursor, 1 => edit]
}

proptest! {
    #[test]
    fn any_output_looks_the_same_through_embers(
        steps in prop::collection::vec(step(), 1..24),
    ) {
        let bytes = steps.concat();
        if let Some(difference) = first_difference(&bytes) {
            return Err(TestCaseError::fail(difference));
        }
    }
}