tinynote 0.2.0

Raycast Notes in your terminal: a tiny markdown notes TUI over plain files
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
//! A small line-based text buffer: the note being edited.
//!
//! Deliberately plain — no vim motions, no undo stack. Lines are `String`s and
//! positions are (line, char column); long lines are soft-wrapped by the view,
//! which owns the wrapping because it also owns the styling. Everything the
//! live-preview view needs is here.

use crossterm::event::{KeyCode, KeyEvent, KeyModifiers};

pub type Pos = (usize, usize);

#[derive(Default)]
pub struct Editor {
    lines: Vec<String>,
    pub cursor: Pos,
    /// Selection start; the cursor is the other end. `None` when nothing is selected.
    pub anchor: Option<Pos>,
    /// First visible source line.
    pub scroll: usize,
    /// Whether the view should chase the cursor on the next draw. Key handling
    /// sets it; the mouse wheel clears it, so a scrolled page stays put.
    follow_cursor: bool,
    /// The file ended with a newline, and should again after a save.
    trailing_newline: bool,
    /// The file used CRLF line endings, and should again after a save.
    crlf: bool,
}

impl Editor {
    pub fn new(content: &str) -> Editor {
        let mut lines: Vec<String> = content.lines().map(String::from).collect();
        if lines.is_empty() {
            lines.push(String::new());
        }
        Editor {
            lines,
            follow_cursor: true,
            trailing_newline: content.ends_with('\n'),
            crlf: content.contains("\r\n"),
            ..Default::default()
        }
    }

    /// Whether the view is still chasing the cursor. The draw needs to know:
    /// with images on screen a line is not always one row, so the vertical
    /// scroll gets a second, height-aware pass — but only when following.
    pub fn following(&self) -> bool {
        self.follow_cursor
    }

    pub fn lines(&self) -> &[String] {
        &self.lines
    }

    /// The buffer as it should be written back: the file's own line ending and
    /// its trailing newline are preserved, so notes shared with git or another
    /// editor aren't rewritten by merely opening them.
    pub fn text(&self) -> String {
        let sep = if self.crlf { "\r\n" } else { "\n" };
        let mut out = self.lines.join(sep);
        if self.trailing_newline {
            out.push_str(sep);
        }
        out
    }

    fn line_len(&self, row: usize) -> usize {
        self.lines.get(row).map_or(0, |l| l.chars().count())
    }

    /// Clamp an arbitrary (line, col) onto the buffer.
    pub fn clamp(&self, (row, col): Pos) -> Pos {
        let row = row.min(self.lines.len().saturating_sub(1));
        (row, col.min(self.line_len(row)))
    }

    fn byte_index(&self, (row, col): Pos) -> usize {
        self.lines[row]
            .char_indices()
            .nth(col)
            .map_or(self.lines[row].len(), |(b, _)| b)
    }

    /// Selection as an ordered (start, end) pair, if any text is selected.
    pub fn selection(&self) -> Option<(Pos, Pos)> {
        let a = self.anchor?;
        if a == self.cursor {
            return None;
        }
        Some(if a <= self.cursor {
            (a, self.cursor)
        } else {
            (self.cursor, a)
        })
    }

    /// Selected source columns on `row`, as a half-open range.
    pub fn selection_on(&self, row: usize) -> Option<(usize, usize)> {
        let ((sr, sc), (er, ec)) = self.selection()?;
        if row < sr || row > er {
            return None;
        }
        let start = if row == sr { sc } else { 0 };
        let end = if row == er {
            ec
        } else {
            self.line_len(row) + 1 // include the newline, so the row reads as full
        };
        Some((start, end))
    }

    pub fn selected_text(&self) -> Option<String> {
        let ((sr, sc), (er, ec)) = self.selection()?;
        if sr == er {
            let s: String = self.lines[sr].chars().skip(sc).take(ec - sc).collect();
            return Some(s);
        }
        let mut out: String = self.lines[sr].chars().skip(sc).collect();
        for line in &self.lines[sr + 1..er] {
            out.push('\n');
            out.push_str(line);
        }
        out.push('\n');
        out.extend(self.lines[er].chars().take(ec));
        Some(out)
    }

    /// Replace one line wholesale (the preview's checkbox toggle).
    pub fn set_line(&mut self, row: usize, text: String) {
        if let Some(line) = self.lines.get_mut(row) {
            *line = text;
            self.cursor = self.clamp(self.cursor);
        }
    }

    pub fn clear_selection(&mut self) {
        self.anchor = None;
    }

    pub fn set_cursor(&mut self, pos: Pos) {
        self.cursor = self.clamp(pos);
        self.follow_cursor = true;
    }

    /// Move the cursor, extending the selection when `select` is set. Public
    /// for the view's display-row Up/Down, which needs the wrapping to know
    /// where the next visual row is.
    pub fn move_cursor(&mut self, pos: Pos, select: bool) {
        self.follow_cursor = true;
        self.move_to(pos, select);
    }

    /// Move the cursor, extending the selection when `select` is set.
    fn move_to(&mut self, pos: Pos, select: bool) {
        if select {
            if self.anchor.is_none() {
                self.anchor = Some(self.cursor);
            }
        } else {
            self.anchor = None;
        }
        self.cursor = self.clamp(pos);
    }

    pub fn delete_selection(&mut self) -> bool {
        let Some(((sr, sc), (er, ec))) = self.selection() else {
            return false;
        };
        let head: String = self.lines[sr].chars().take(sc).collect();
        let tail: String = self.lines[er].chars().skip(ec).collect();
        self.lines.splice(sr..=er, [head + &tail]);
        self.cursor = (sr, sc);
        self.anchor = None;
        true
    }

    pub fn insert_char(&mut self, c: char) {
        self.delete_selection();
        let at = self.byte_index(self.cursor);
        self.lines[self.cursor.0].insert(at, c);
        self.cursor.1 += 1;
    }

    /// Insert arbitrary text at the cursor, newlines and all (a paste).
    pub fn insert_str(&mut self, text: &str) {
        self.delete_selection();
        for (i, part) in text.replace("\r\n", "\n").split('\n').enumerate() {
            if i > 0 {
                self.insert_newline();
            }
            for c in part.chars() {
                if c != '\r' {
                    self.insert_char(c);
                }
            }
        }
    }

    pub fn insert_newline(&mut self) {
        self.delete_selection();
        let (row, col) = self.cursor;
        let at = self.byte_index((row, col));
        let tail = self.lines[row].split_off(at);
        self.lines.insert(row + 1, tail);
        self.cursor = (row + 1, 0);
    }

    pub fn backspace(&mut self) {
        if self.delete_selection() {
            return;
        }
        let (row, col) = self.cursor;
        if col > 0 {
            let at = self.byte_index((row, col - 1));
            self.lines[row].remove(at);
            self.cursor.1 = col - 1;
        } else if row > 0 {
            let line = self.lines.remove(row);
            let prev = self.line_len(row - 1);
            self.lines[row - 1].push_str(&line);
            self.cursor = (row - 1, prev);
        }
    }

    pub fn delete_forward(&mut self) {
        if self.delete_selection() {
            return;
        }
        let (row, col) = self.cursor;
        if col < self.line_len(row) {
            let at = self.byte_index((row, col));
            self.lines[row].remove(at);
        } else if row + 1 < self.lines.len() {
            let next = self.lines.remove(row + 1);
            self.lines[row].push_str(&next);
        }
    }

    fn left(&self) -> Pos {
        let (row, col) = self.cursor;
        if col > 0 {
            (row, col - 1)
        } else if row > 0 {
            (row - 1, self.line_len(row - 1))
        } else {
            (0, 0)
        }
    }

    fn right(&self) -> Pos {
        let (row, col) = self.cursor;
        if col < self.line_len(row) {
            (row, col + 1)
        } else if row + 1 < self.lines.len() {
            (row + 1, 0)
        } else {
            self.cursor
        }
    }

    /// Delete from the cursor back to the start of the line (Cmd+Backspace).
    /// On an already-empty prefix this joins with the previous line, like a
    /// plain backspace, so the key is never a no-op mid-buffer.
    pub fn delete_to_line_start(&mut self) {
        if self.delete_selection() {
            return;
        }
        let (row, col) = self.cursor;
        if col == 0 {
            self.backspace();
            return;
        }
        let at = self.byte_index((row, col));
        let tail = self.lines[row][at..].to_string();
        self.lines[row] = tail;
        self.cursor = (row, 0);
    }

    /// Delete the word before the cursor (Option+Backspace).
    pub fn delete_prev_word(&mut self) {
        if self.delete_selection() {
            return;
        }
        let (row, col) = self.cursor;
        if col == 0 {
            self.backspace();
            return;
        }
        let start = self.word_left(row, col);
        let from = self.byte_index((row, start));
        let to = self.byte_index((row, col));
        self.lines[row].replace_range(from..to, "");
        self.cursor = (row, start);
    }

    /// The last position in the buffer.
    fn doc_end(&self) -> Pos {
        let last = self.lines.len().saturating_sub(1);
        (last, self.line_len(last))
    }

    /// Handle one key. Returns true if the buffer changed.
    pub fn on_key(&mut self, key: KeyEvent) -> bool {
        self.follow_cursor = true;
        let m = key.modifiers;
        let select = m.contains(KeyModifiers::SHIFT);
        // Cmd, when the terminal reports it (kitty keyboard protocol).
        let line_wise = m.contains(KeyModifiers::SUPER);
        // Option/Alt — and Ctrl, which many terminals send for word motion.
        let word =
            !line_wise && (m.contains(KeyModifiers::ALT) || m.contains(KeyModifiers::CONTROL));
        let (row, col) = self.cursor;

        // The legacy bytes a terminal sends for the Mac editing keys when it
        // does *not* report Cmd/Option on the key itself. Ghostty's default
        // macOS keybinds rewrite them this way before the kitty keyboard
        // protocol ever gets a look in, so ⌘← arrives as a plain Ctrl-A and
        // this is the only path that sees it. They are the classic readline
        // chords besides, so binding them costs nothing.
        if let Some(changed) = self.legacy_chord(key, select) {
            return changed;
        }

        match key.code {
            // ctrl-chords belong to the app, not the buffer
            KeyCode::Char(_) if key.modifiers.contains(KeyModifiers::CONTROL) => {}
            KeyCode::Char(c) => {
                self.insert_char(c);
                return true;
            }
            KeyCode::Enter => {
                self.insert_newline();
                return true;
            }
            KeyCode::Backspace => {
                if line_wise {
                    self.delete_to_line_start();
                } else if word {
                    self.delete_prev_word();
                } else {
                    self.backspace();
                }
                return true;
            }
            KeyCode::Delete => {
                self.delete_forward();
                return true;
            }
            KeyCode::Tab => {
                for _ in 0..2 {
                    self.insert_char(' ');
                }
                return true;
            }
            KeyCode::Left => {
                let to = if line_wise {
                    (row, 0)
                } else if word && col > 0 {
                    (row, self.word_left(row, col))
                } else {
                    self.left()
                };
                self.move_to(to, select);
            }
            KeyCode::Right => {
                let to = if line_wise {
                    (row, self.line_len(row))
                } else if word && col < self.line_len(row) {
                    (row, self.word_right(row, col))
                } else {
                    self.right()
                };
                self.move_to(to, select);
            }
            KeyCode::Up if line_wise => self.move_to((0, 0), select),
            KeyCode::Down if line_wise => {
                let end = self.doc_end();
                self.move_to(end, select);
            }
            KeyCode::Up => self.move_to((row.saturating_sub(1), col), select),
            KeyCode::Down => self.move_to((row + 1, col), select),
            KeyCode::Home => self.move_to((row, 0), select),
            KeyCode::End => self.move_to((row, self.line_len(row)), select),
            KeyCode::PageUp => self.move_to((row.saturating_sub(10), col), select),
            KeyCode::PageDown => self.move_to((row + 10, col), select),
            KeyCode::Esc => self.clear_selection(),
            _ => {}
        }
        false
    }

    /// The legacy Mac-editing chords, or `None` if this key isn't one.
    /// `Some(changed)` mirrors [`Editor::on_key`]'s own return.
    fn legacy_chord(&mut self, key: KeyEvent, select: bool) -> Option<bool> {
        let m = key.modifiers;
        let ctrl = m.contains(KeyModifiers::CONTROL);
        let alt = m.contains(KeyModifiers::ALT);
        let (row, col) = self.cursor;
        match key.code {
            // ⌘← / ⌘→ reach us as Ctrl-A / Ctrl-E
            KeyCode::Char('a') if ctrl => self.move_to((row, 0), select),
            KeyCode::Char('e') if ctrl => self.move_to((row, self.line_len(row)), select),
            // ⌘⌫ as Ctrl-U, ⌥⌫ as Ctrl-W
            KeyCode::Char('u') if ctrl => {
                self.delete_to_line_start();
                return Some(true);
            }
            KeyCode::Char('w') if ctrl => {
                self.delete_prev_word();
                return Some(true);
            }
            // ⌥← / ⌥→ as ESC b / ESC f
            KeyCode::Char('b') if alt && !ctrl => {
                let to = if col > 0 {
                    (row, self.word_left(row, col))
                } else {
                    self.left()
                };
                self.move_to(to, select);
            }
            KeyCode::Char('f') if alt && !ctrl => {
                let to = if col < self.line_len(row) {
                    (row, self.word_right(row, col))
                } else {
                    self.right()
                };
                self.move_to(to, select);
            }
            // ⌘↑ / ⌘↓ have no legacy encoding at all — Ghostty keeps them for
            // its own scrollback jumping — so Ctrl-Home / Ctrl-End stand in,
            // which is what most terminals send for them anyway.
            KeyCode::Home if ctrl => self.move_to((0, 0), select),
            KeyCode::End if ctrl => {
                let end = self.doc_end();
                self.move_to(end, select);
            }
            _ => return None,
        }
        Some(false)
    }

    fn word_left(&self, row: usize, col: usize) -> usize {
        let chars: Vec<char> = self.lines[row].chars().collect();
        let mut i = col;
        while i > 0 && !chars[i - 1].is_alphanumeric() {
            i -= 1;
        }
        while i > 0 && chars[i - 1].is_alphanumeric() {
            i -= 1;
        }
        i
    }

    fn word_right(&self, row: usize, col: usize) -> usize {
        let chars: Vec<char> = self.lines[row].chars().collect();
        let mut i = col;
        while i < chars.len() && !chars[i].is_alphanumeric() {
            i += 1;
        }
        while i < chars.len() && chars[i].is_alphanumeric() {
            i += 1;
        }
        i
    }

    /// Scroll the page without moving the cursor (the mouse wheel). The view
    /// stops chasing the cursor until the next keystroke.
    pub fn scroll_by(&mut self, delta: isize) {
        let max = self.lines.len().saturating_sub(1);
        self.scroll = (self.scroll as isize + delta).clamp(0, max as isize) as usize;
        self.follow_cursor = false;
    }

    /// Keep the cursor's line on screen for a viewport of `height` rows. This
    /// is the coarse, one-row-per-line pass; the draw refines it in display
    /// rows once it knows how many rows each line wrapped to. Does nothing
    /// while the page is being scrolled by hand.
    pub fn scroll_into_view(&mut self, height: usize) {
        if !self.follow_cursor {
            return;
        }
        let height = height.max(1);
        if self.cursor.0 < self.scroll {
            self.scroll = self.cursor.0;
        } else if self.cursor.0 >= self.scroll + height {
            self.scroll = self.cursor.0 + 1 - height;
        }
    }
}

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

    fn ed() -> Editor {
        Editor::new("one\ntwo\nthree")
    }

    #[test]
    fn insert_and_backspace() {
        let mut e = Editor::new("ab");
        e.set_cursor((0, 1));
        e.insert_char('X');
        assert_eq!(e.text(), "aXb");
        e.backspace();
        assert_eq!(e.text(), "ab");
    }

    #[test]
    fn newline_splits_and_backspace_joins() {
        let mut e = Editor::new("hello");
        e.set_cursor((0, 2));
        e.insert_newline();
        assert_eq!(e.text(), "he\nllo");
        assert_eq!(e.cursor, (1, 0));
        e.backspace();
        assert_eq!(e.text(), "hello");
        assert_eq!(e.cursor, (0, 2));
    }

    #[test]
    fn multiline_selection_text_and_delete() {
        let mut e = ed();
        e.anchor = Some((0, 1));
        e.cursor = (2, 2);
        assert_eq!(e.selected_text().unwrap(), "ne\ntwo\nth");
        assert_eq!(e.selection_on(1), Some((0, 4)));
        e.delete_selection();
        assert_eq!(e.text(), "oree");
        assert_eq!(e.cursor, (0, 1));
    }

    #[test]
    fn paste_inserts_multiple_lines() {
        let mut e = Editor::new("ab");
        e.set_cursor((0, 1));
        e.insert_str("X\r\nY");
        assert_eq!(e.text(), "aX\nYb");
        assert_eq!(e.cursor, (1, 1));
    }

    #[test]
    fn clamp_keeps_positions_inside() {
        let e = ed();
        assert_eq!(e.clamp((9, 9)), (2, 5));
        assert_eq!(e.clamp((1, 99)), (1, 3));
    }

    #[test]
    fn saving_keeps_the_file_s_trailing_newline_and_line_endings() {
        assert_eq!(Editor::new("a\nb\n").text(), "a\nb\n");
        assert_eq!(Editor::new("a\nb").text(), "a\nb");
        assert_eq!(Editor::new("a\r\nb\r\n").text(), "a\r\nb\r\n");
    }

    #[test]
    fn wheel_scrolling_is_not_snapped_back_to_the_cursor() {
        let mut e = Editor::new(&"x\n".repeat(50));
        e.scroll_by(10);
        e.scroll_into_view(5);
        assert_eq!(
            e.scroll, 10,
            "the wheel's scroll must survive the next frame"
        );
        // a keystroke puts the view back on the cursor
        e.on_key(KeyEvent::new(KeyCode::Down, KeyModifiers::NONE));
        e.scroll_into_view(5);
        assert_eq!(e.scroll, e.cursor.0);
    }

    fn key(code: KeyCode, mods: KeyModifiers) -> KeyEvent {
        KeyEvent::new(code, mods)
    }

    const SUPER: KeyModifiers = KeyModifiers::SUPER;
    const ALT: KeyModifiers = KeyModifiers::ALT;
    const SHIFT: KeyModifiers = KeyModifiers::SHIFT;

    #[test]
    fn cmd_arrows_go_to_line_and_document_ends() {
        let mut e = Editor::new("one two\nthree four\nfive");
        e.set_cursor((1, 4));
        e.on_key(key(KeyCode::Left, SUPER));
        assert_eq!(e.cursor, (1, 0));
        e.on_key(key(KeyCode::Right, SUPER));
        assert_eq!(e.cursor, (1, 10));
        e.on_key(key(KeyCode::Up, SUPER));
        assert_eq!(e.cursor, (0, 0));
        e.on_key(key(KeyCode::Down, SUPER));
        assert_eq!(e.cursor, (2, 4));
        // Home/End still work for terminals without the protocol
        e.on_key(key(KeyCode::Home, KeyModifiers::NONE));
        assert_eq!(e.cursor, (2, 0));
        e.on_key(key(KeyCode::End, KeyModifiers::NONE));
        assert_eq!(e.cursor, (2, 4));
    }

    #[test]
    fn option_arrows_move_by_word_and_cross_lines_at_the_edges() {
        let mut e = Editor::new("alpha beta\ngamma");
        e.set_cursor((0, 10));
        e.on_key(key(KeyCode::Left, ALT));
        assert_eq!(e.cursor, (0, 6));
        e.on_key(key(KeyCode::Left, ALT));
        assert_eq!(e.cursor, (0, 0));
        // at the very start of a line, word-left falls back to joining motion
        e.on_key(key(KeyCode::Right, ALT));
        assert_eq!(e.cursor, (0, 5));
        e.set_cursor((0, 10));
        e.on_key(key(KeyCode::Right, ALT));
        assert_eq!(e.cursor, (1, 0), "past the end of a line, wrap to the next");
    }

    const CTRL: KeyModifiers = KeyModifiers::CONTROL;

    /// Exactly what Ghostty's default macOS keybinds put on the wire for the
    /// Mac editing keys — the kitty protocol never sees them.
    #[test]
    fn the_legacy_mac_editing_chords_move_and_delete() {
        let mut e = Editor::new("alpha beta\ngamma delta\nlast");
        e.set_cursor((1, 6));
        e.on_key(key(KeyCode::Char('a'), CTRL)); // ⌘←
        assert_eq!(e.cursor, (1, 0));
        e.on_key(key(KeyCode::Char('e'), CTRL)); // ⌘→
        assert_eq!(e.cursor, (1, 11));
        e.on_key(key(KeyCode::Char('b'), ALT)); // ⌥←
        assert_eq!(e.cursor, (1, 6));
        e.on_key(key(KeyCode::Char('f'), ALT)); // ⌥→
        assert_eq!(e.cursor, (1, 11));
        // ⌘↑ / ⌘↓ stand-ins
        e.on_key(key(KeyCode::Home, CTRL));
        assert_eq!(e.cursor, (0, 0));
        e.on_key(key(KeyCode::End, CTRL));
        assert_eq!(e.cursor, (2, 4));

        // ⌘⌫ and ⌥⌫
        let mut e = Editor::new("alpha beta");
        e.set_cursor((0, 10));
        assert!(e.on_key(key(KeyCode::Char('w'), CTRL)));
        assert_eq!(e.text(), "alpha ");
        assert!(e.on_key(key(KeyCode::Char('u'), CTRL)));
        assert_eq!(e.text(), "");
        // ⌥⌫ also arrives as ESC DEL, which crossterm reports as alt-backspace
        let mut e = Editor::new("alpha beta");
        e.set_cursor((0, 10));
        assert!(e.on_key(key(KeyCode::Backspace, ALT)));
        assert_eq!(e.text(), "alpha ");
    }

    #[test]
    fn the_legacy_chords_extend_a_selection_and_leave_plain_letters_alone() {
        let mut e = Editor::new("alpha beta");
        e.set_cursor((0, 10));
        e.on_key(key(KeyCode::Char('a'), CTRL | SHIFT));
        assert_eq!(e.selected_text().unwrap(), "alpha beta");
        // an unmodified letter is still just typed, chord letters included
        let mut e = Editor::new("");
        for c in "abefuw".chars() {
            assert!(e.on_key(key(KeyCode::Char(c), KeyModifiers::NONE)));
        }
        assert_eq!(e.text(), "abefuw");
    }

    #[test]
    fn shift_extends_the_selection_for_the_new_movements() {
        let mut e = Editor::new("alpha beta\ngamma");
        e.set_cursor((0, 10));
        e.on_key(key(KeyCode::Left, SUPER | SHIFT));
        assert_eq!(e.selected_text().unwrap(), "alpha beta");
        e.set_cursor((0, 10));
        e.clear_selection();
        e.on_key(key(KeyCode::Left, ALT | SHIFT));
        assert_eq!(e.selected_text().unwrap(), "beta");
        e.set_cursor((0, 0));
        e.clear_selection();
        e.on_key(key(KeyCode::Down, SUPER | SHIFT));
        assert_eq!(e.selected_text().unwrap(), "alpha beta\ngamma");
    }

    #[test]
    fn cmd_backspace_kills_to_the_line_start() {
        let mut e = Editor::new("one\nhello there");
        e.set_cursor((1, 6));
        e.on_key(key(KeyCode::Backspace, SUPER));
        assert_eq!(e.text(), "one\nthere");
        assert_eq!(e.cursor, (1, 0));
        // already at column 0: joins with the line above, like a plain backspace
        e.on_key(key(KeyCode::Backspace, SUPER));
        assert_eq!(e.text(), "onethere");
        assert_eq!(e.cursor, (0, 3));
    }

    #[test]
    fn option_backspace_kills_the_previous_word() {
        let mut e = Editor::new("alpha beta gamma");
        e.set_cursor((0, 16));
        e.on_key(key(KeyCode::Backspace, ALT));
        assert_eq!(e.text(), "alpha beta ");
        e.on_key(key(KeyCode::Backspace, ALT));
        assert_eq!(e.text(), "alpha ");
        assert_eq!(e.cursor, (0, 6));
    }

    #[test]
    fn word_deletion_is_char_wise_over_unicode() {
        let mut e = Editor::new("café crème");
        e.set_cursor((0, 10));
        e.on_key(key(KeyCode::Backspace, ALT));
        assert_eq!(e.text(), "café ");
    }

    #[test]
    fn a_modified_backspace_deletes_the_selection_first() {
        let mut e = Editor::new("alpha beta");
        e.anchor = Some((0, 0));
        e.cursor = (0, 6);
        e.on_key(key(KeyCode::Backspace, ALT));
        assert_eq!(e.text(), "beta");
        assert_eq!(e.anchor, None);
    }

    #[test]
    fn unicode_columns_are_chars_not_bytes() {
        let mut e = Editor::new("café");
        e.set_cursor((0, 4));
        e.backspace();
        assert_eq!(e.text(), "caf");
    }
}