mach-tui 0.3.2

A terminal-first task manager for people who live in the shell and work with agents
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
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
//! A one-line editor. Every text field in mach is built from it —
//! the title, the due date, category names, the command line, the search
//! box, and each block of a task's body. Cursor and selection indices are
//! Unicode grapheme clusters; display geometry is still measured in cells.
//!
//! Selection follows macOS: Shift extends, Option jumps by word, and
//! Shift+Option+W selects the word under the cursor.

use unicode_segmentation::UnicodeSegmentation;
use unicode_width::UnicodeWidthStr;

#[derive(Debug, Default, Clone, PartialEq, Eq)]
pub struct TextInput {
    /// Extended grapheme clusters. A visible user-perceived character is
    /// never split by cursor movement, selection, or deletion.
    chars: Vec<String>,
    cursor: usize,
    /// Other end of the selection; `None` means a caret only.
    sel_anchor: Option<usize>,
    scroll: usize,
    max_len: usize,
    max_bytes: usize,
}

/// What [`TextInput::visible`] hands the UI: text, caret column, and an
/// optional selection span in display columns within that text.
#[derive(Debug, Clone)]
pub struct View {
    pub text: String,
    pub cursor_col: u16,
    /// Inclusive start, exclusive end, in display columns of `text`.
    pub sel_cols: Option<(u16, u16)>,
}

/// One visual row of a soft-wrapped field.
#[derive(Debug, Clone)]
pub struct WrappedLine {
    pub text: String,
    pub sel_cols: Option<(u16, u16)>,
    /// Grapheme range into the full buffer covered by this row.
    pub start: usize,
    pub end: usize,
}

/// Soft-wrapped layout for body / description painting.
#[derive(Debug, Clone)]
pub struct WrappedView {
    pub lines: Vec<WrappedLine>,
    /// Cursor row within `lines`, and column within that row.
    pub cursor_row: u16,
    pub cursor_col: u16,
}

/// Soft-wrap graphemes into `(start, end)` ranges of at most `width`
/// display columns. Prefers breaking after a space; otherwise hard-breaks.
pub fn wrap_breaks(chars: &[String], width: usize) -> Vec<(usize, usize)> {
    if chars.is_empty() {
        return vec![(0, 0)];
    }
    let width = width.max(1);
    let mut lines = Vec::new();
    let mut start = 0usize;
    let mut col = 0usize;
    let mut last_ws: Option<usize> = None;
    let mut i = 0usize;

    while i < chars.len() {
        let w = chars[i].width();
        if w > width {
            // Lone character wider than the field — own row.
            if i > start {
                lines.push((start, i));
            }
            lines.push((i, i + 1));
            i += 1;
            start = i;
            col = 0;
            last_ws = None;
            continue;
        }
        if col + w > width && i > start {
            // Prefer the last space so words stay intact when they fit.
            let end = last_ws
                .filter(|&ws| ws >= start)
                .map(|ws| ws + 1)
                .unwrap_or(i);
            let end = end.max(start + 1);
            lines.push((start, end));
            start = end;
            i = start;
            col = 0;
            last_ws = None;
            continue;
        }
        if is_whitespace(&chars[i]) {
            last_ws = Some(i);
        }
        col += w;
        i += 1;
    }
    if start <= chars.len() {
        lines.push((start, chars.len()));
    }
    if lines.is_empty() {
        lines.push((0, 0));
    }
    lines
}

fn is_whitespace(grapheme: &str) -> bool {
    !grapheme.is_empty() && grapheme.chars().all(char::is_whitespace)
}

impl TextInput {
    pub fn new(initial: &str, max_len: usize) -> Self {
        let max_bytes = crate::model::text_byte_limit(max_len);
        let chars = bounded_graphemes(initial, max_len, max_bytes);
        let cursor = chars.len();
        Self {
            chars,
            cursor,
            sel_anchor: None,
            scroll: 0,
            max_len,
            max_bytes,
        }
    }

    pub fn value(&self) -> String {
        self.chars.concat()
    }

    pub fn is_empty(&self) -> bool {
        self.chars.is_empty()
    }

    pub fn cursor(&self) -> usize {
        self.cursor
    }

    pub fn len(&self) -> usize {
        self.chars.len()
    }

    pub fn slice(&self, start: usize, end: usize) -> String {
        let start = start.min(self.chars.len());
        let end = end.max(start).min(self.chars.len());
        self.chars[start..end].concat()
    }

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

    pub fn has_selection(&self) -> bool {
        self.selection_range().is_some()
    }

    /// Inclusive start and exclusive end of the selection, if any.
    pub fn selection_range(&self) -> Option<(usize, usize)> {
        let a = self.sel_anchor?;
        let (lo, hi) = if a <= self.cursor {
            (a, self.cursor)
        } else {
            (self.cursor, a)
        };
        (lo < hi).then_some((lo, hi))
    }

    pub fn selected_text(&self) -> Option<String> {
        let (lo, hi) = self.selection_range()?;
        Some(self.chars[lo..hi].concat())
    }

    /// Removes the selected range. Returns true when something was deleted.
    pub fn delete_selection(&mut self) -> bool {
        let Some((lo, hi)) = self.selection_range() else {
            return false;
        };
        self.replace_range(lo, hi, "");
        true
    }

    /// Select the word under (or beside) the cursor — macOS Select Word.
    pub fn select_word(&mut self) {
        if self.chars.is_empty() {
            self.sel_anchor = None;
            return;
        }
        let n = self.chars.len();
        let i = self.cursor.min(n);
        // Prefer the word containing the caret; if on a boundary, the
        // word to the left; if mid-whitespace, the next word to the right.
        if i < n && !is_whitespace(&self.chars[i]) {
            let mut start = i;
            while start > 0 && !is_whitespace(&self.chars[start - 1]) {
                start -= 1;
            }
            let mut end = i;
            while end < n && !is_whitespace(&self.chars[end]) {
                end += 1;
            }
            self.sel_anchor = Some(start);
            self.cursor = end;
            return;
        }
        if i > 0 && !is_whitespace(&self.chars[i - 1]) {
            let mut start = i;
            while start > 0 && !is_whitespace(&self.chars[start - 1]) {
                start -= 1;
            }
            self.sel_anchor = Some(start);
            self.cursor = i;
            return;
        }
        // Whitespace: walk forward to the next word.
        let mut start = i;
        while start < n && is_whitespace(&self.chars[start]) {
            start += 1;
        }
        if start < n {
            let mut end = start;
            while end < n && !is_whitespace(&self.chars[end]) {
                end += 1;
            }
            self.sel_anchor = Some(start);
            self.cursor = end;
            return;
        }
        // Trailing whitespace only: word to the left.
        if i > 0 {
            let mut end = i;
            while end > 0 && is_whitespace(&self.chars[end - 1]) {
                end -= 1;
            }
            let mut start = end;
            while start > 0 && !is_whitespace(&self.chars[start - 1]) {
                start -= 1;
            }
            if start < end {
                self.sel_anchor = Some(start);
                self.cursor = end;
                return;
            }
        }
        self.sel_anchor = None;
    }

    pub fn set_cursor(&mut self, cursor: usize) {
        self.clear_selection();
        self.cursor = cursor.min(self.chars.len());
    }

    /// Places the cursor at a display column of the visible window, as
    /// produced by [`TextInput::visible`]. Past the end of the text the
    /// cursor lands at the end.
    pub fn set_cursor_from_col(&mut self, col: usize) {
        self.clear_selection();
        let mut used = 0;
        let mut cursor = self.scroll;
        for c in &self.chars[self.scroll.min(self.chars.len())..] {
            let w = c.width();
            if used + w > col {
                break;
            }
            used += w;
            cursor += 1;
        }
        self.cursor = cursor.min(self.chars.len());
    }

    pub fn at_start(&self) -> bool {
        self.cursor == 0
    }

    pub fn at_end(&self) -> bool {
        self.cursor == self.chars.len()
    }

    /// Cuts everything after the cursor into a new input.
    pub fn split_off_at_cursor(&mut self) -> Self {
        self.clear_selection();
        let tail: Vec<String> = self.chars.split_off(self.cursor);
        Self {
            chars: tail,
            cursor: 0,
            sel_anchor: None,
            scroll: 0,
            max_len: self.max_len,
            max_bytes: self.max_bytes,
        }
    }

    /// Appends another input's text, leaving the cursor at the join. Returns
    /// `false` without changing either input when the full join would exceed
    /// this field's grapheme or byte limit.
    pub fn append(&mut self, other: &Self) -> bool {
        let left = self.value();
        let cursor = left.graphemes(true).count();
        let right = other.value();
        if left.len().saturating_add(right.len()) > self.max_bytes {
            return false;
        }
        let joined = left + &right;
        let chars: Vec<String> = joined.graphemes(true).map(str::to_string).collect();
        if chars.len() > self.max_len {
            return false;
        }
        self.clear_selection();
        self.chars = chars;
        self.cursor = cursor.min(self.chars.len());
        self.scroll = self.scroll.min(self.cursor);
        true
    }

    fn replace_range(&mut self, lo: usize, hi: usize, replacement: &str) {
        let lo = lo.min(self.chars.len());
        let hi = hi.max(lo).min(self.chars.len());
        let prefix = self.chars[..lo].concat();
        let suffix = self.chars[hi..].concat();
        let before_cursor = format!("{prefix}{replacement}");
        let cursor = before_cursor.graphemes(true).count();
        let joined = before_cursor + &suffix;
        self.chars = joined.graphemes(true).map(str::to_string).collect();
        self.cursor = cursor.min(self.chars.len());
        self.sel_anchor = None;
        self.scroll = self.scroll.min(self.cursor);
    }

    pub fn insert(&mut self, c: char) {
        self.insert_str(&c.to_string());
    }

    /// Inserts a run of text at the cursor. Tab/newline separators are
    /// flattened, while terminal control bytes are discarded at the input
    /// boundary so they can never reach the renderer.
    pub fn insert_str(&mut self, text: &str) {
        let (lo, hi) = self.selection_range().unwrap_or((self.cursor, self.cursor));
        let prefix = self.chars[..lo].concat();
        let suffix = self.chars[hi..].concat();
        let available = self
            .max_bytes
            .saturating_sub(prefix.len().saturating_add(suffix.len()));
        let mut flattened = String::with_capacity(text.len().min(available));
        for character in text.chars().filter_map(|character| match character {
            '\t' | '\n' | '\r' => Some(' '),
            character if character.is_control() => None,
            _ => Some(character),
        }) {
            if flattened.len().saturating_add(character.len_utf8()) > available {
                break;
            }
            flattened.push(character);
        }
        if flattened.is_empty() {
            return;
        }
        let mut accepted = String::new();
        for grapheme in flattened.graphemes(true) {
            let candidate = format!("{prefix}{accepted}{grapheme}{suffix}");
            if candidate.len() > self.max_bytes || candidate.graphemes(true).count() > self.max_len
            {
                break;
            }
            accepted.push_str(grapheme);
        }
        if !accepted.is_empty() {
            self.replace_range(lo, hi, &accepted);
        }
    }

    pub fn backspace(&mut self) {
        if self.delete_selection() {
            return;
        }
        if self.cursor > 0 {
            self.replace_range(self.cursor - 1, self.cursor, "");
        }
    }

    pub fn delete(&mut self) {
        if self.delete_selection() {
            return;
        }
        if self.cursor < self.chars.len() {
            self.replace_range(self.cursor, self.cursor + 1, "");
        }
    }

    pub fn delete_to_start(&mut self) {
        if self.delete_selection() {
            return;
        }
        self.replace_range(0, self.cursor, "");
    }

    pub fn delete_to_end(&mut self) {
        if self.delete_selection() {
            return;
        }
        self.replace_range(self.cursor, self.chars.len(), "");
    }

    pub fn delete_word_left(&mut self) {
        if self.delete_selection() {
            return;
        }
        let target = self.word_left_index();
        self.replace_range(target, self.cursor, "");
    }

    fn move_to(&mut self, pos: usize, extend: bool) {
        if extend {
            if self.sel_anchor.is_none() {
                self.sel_anchor = Some(self.cursor);
            }
        } else {
            self.sel_anchor = None;
        }
        self.cursor = pos.min(self.chars.len());
    }

    pub fn left(&mut self) {
        self.move_to(self.cursor.saturating_sub(1), false);
    }

    pub fn right(&mut self) {
        self.move_to((self.cursor + 1).min(self.chars.len()), false);
    }

    pub fn select_left(&mut self) {
        self.move_to(self.cursor.saturating_sub(1), true);
    }

    pub fn select_right(&mut self) {
        self.move_to((self.cursor + 1).min(self.chars.len()), true);
    }

    pub fn word_left(&mut self) {
        self.move_to(self.word_left_index(), false);
    }

    pub fn word_right(&mut self) {
        self.move_to(self.word_right_index(), false);
    }

    pub fn select_word_left(&mut self) {
        self.move_to(self.word_left_index(), true);
    }

    pub fn select_word_right(&mut self) {
        self.move_to(self.word_right_index(), true);
    }

    pub fn home(&mut self) {
        self.move_to(0, false);
    }

    pub fn end(&mut self) {
        self.move_to(self.chars.len(), false);
    }

    pub fn select_home(&mut self) {
        self.move_to(0, true);
    }

    pub fn select_end(&mut self) {
        self.move_to(self.chars.len(), true);
    }

    /// Where the caret lands moving one word left; the body editor uses
    /// this to extend a selection across block boundaries.
    pub fn word_left_index(&self) -> usize {
        let mut i = self.cursor;
        while i > 0 && is_whitespace(&self.chars[i - 1]) {
            i -= 1;
        }
        while i > 0 && !is_whitespace(&self.chars[i - 1]) {
            i -= 1;
        }
        i
    }

    /// Where the caret lands moving one word right.
    pub fn word_right_index(&self) -> usize {
        let mut i = self.cursor;
        let n = self.chars.len();
        while i < n && is_whitespace(&self.chars[i]) {
            i += 1;
        }
        while i < n && !is_whitespace(&self.chars[i]) {
            i += 1;
        }
        i
    }

    /// Soft-wrap into visual rows of at most `width` columns. Prefers
    /// breaking after whitespace; hard-breaks when a single word is wider
    /// than the field. Empty text yields one empty row.
    pub fn wrap_breaks(&self, width: usize) -> Vec<(usize, usize)> {
        wrap_breaks(&self.chars, width)
    }

    /// How many visual rows the text needs at `width` (at least 1).
    pub fn wrap_height(&self, width: usize) -> usize {
        self.wrap_breaks(width).len().max(1)
    }

    /// Cursor as `(visual_row, column)` from precomputed wrap breaks.
    pub fn wrap_cursor_from_breaks(&self, breaks: &[(usize, usize)]) -> (usize, u16) {
        for (row, &(start, end)) in breaks.iter().enumerate() {
            if self.cursor < end || (self.cursor == end && row + 1 == breaks.len()) {
                let col: usize = self.chars[start..self.cursor.min(end)]
                    .iter()
                    .map(|c| c.width())
                    .sum();
                // When the caret sits exactly at a soft-break and another
                // row follows, show it at the start of the next row.
                if self.cursor == end && row + 1 < breaks.len() && end < self.chars.len() {
                    return (row + 1, 0);
                }
                return (row, col as u16);
            }
        }
        let last = breaks.len().saturating_sub(1);
        (last, 0)
    }

    /// Cursor as `(visual_row, column)` for a wrapped layout.
    pub fn wrap_cursor(&self, width: usize) -> (usize, u16) {
        self.wrap_cursor_from_breaks(&self.wrap_breaks(width))
    }

    /// Place the caret from a click on a wrapped row.
    pub fn set_cursor_from_wrap(&mut self, width: usize, row: usize, col: usize) {
        self.clear_selection();
        let breaks = self.wrap_breaks(width);
        if breaks.is_empty() {
            self.cursor = 0;
            return;
        }
        let row = row.min(breaks.len() - 1);
        let (start, end) = breaks[row];
        let mut used = 0usize;
        let mut cursor = start;
        for c in &self.chars[start..end] {
            let w = c.width();
            if used + w > col {
                break;
            }
            used += w;
            cursor += 1;
        }
        self.cursor = cursor.min(self.chars.len());
    }

    /// Move the caret up one visual row. Returns false when already on
    /// the first row (caller may leave the block).
    pub fn wrap_up(&mut self, width: usize, prefer_col: u16) -> bool {
        let (row, col) = self.wrap_cursor(width);
        let prefer = if prefer_col == u16::MAX {
            col
        } else {
            prefer_col
        };
        if row == 0 {
            return false;
        }
        self.set_cursor_from_wrap(width, row - 1, prefer as usize);
        true
    }

    /// Move the caret down one visual row. Returns false when already on
    /// the last row (caller may leave the block).
    pub fn wrap_down(&mut self, width: usize, prefer_col: u16) -> bool {
        let (row, col) = self.wrap_cursor(width);
        let prefer = if prefer_col == u16::MAX {
            col
        } else {
            prefer_col
        };
        let height = self.wrap_height(width);
        if row + 1 >= height {
            return false;
        }
        self.set_cursor_from_wrap(width, row + 1, prefer as usize);
        true
    }

    /// Full soft-wrapped view for painting a multi-line field.
    pub fn wrapped(&self, width: usize) -> WrappedView {
        self.wrapped_with_sel(width, self.selection_range())
    }

    /// Like [`Self::wrapped`], but uses an explicit grapheme-range selection
    /// (for body-level multi-line selections).
    pub fn wrapped_with_sel(&self, width: usize, sel: Option<(usize, usize)>) -> WrappedView {
        self.wrapped_from_breaks(&self.wrap_breaks(width), sel)
    }

    /// Soft-wrapped view from precomputed breaks (one wrap pass per paint).
    pub fn wrapped_from_breaks(
        &self,
        breaks: &[(usize, usize)],
        sel: Option<(usize, usize)>,
    ) -> WrappedView {
        let (cursor_row, cursor_col) = self.wrap_cursor_from_breaks(breaks);
        let lines = breaks
            .iter()
            .map(|&(start, end)| {
                let text = self.chars[start..end].concat();
                let sel_cols = sel.and_then(|(lo, hi)| {
                    let vis_lo = lo.max(start);
                    let vis_hi = hi.min(end);
                    if vis_lo >= vis_hi {
                        return None;
                    }
                    let col = |idx: usize| -> u16 {
                        self.chars[start..idx]
                            .iter()
                            .map(|c| c.width())
                            .sum::<usize>() as u16
                    };
                    Some((col(vis_lo), col(vis_hi)))
                });
                WrappedLine {
                    text,
                    sel_cols,
                    start,
                    end,
                }
            })
            .collect();
        WrappedView {
            lines,
            cursor_row: cursor_row as u16,
            cursor_col,
        }
    }

    /// Move the caret and drop this line's own selection: when the body
    /// editor drives the caret, the multi-line selection is its to own.
    pub fn place_cursor(&mut self, cursor: usize) {
        self.cursor = cursor.min(self.chars.len());
        self.sel_anchor = None;
    }

    /// Text to draw in a field `width` columns wide, the cursor's column
    /// within that field, and the selection's column span when it overlaps.
    /// Single-row fields (title, status) use this; body uses [`Self::wrapped`].
    pub fn visible(&mut self, width: usize) -> View {
        if width == 0 {
            return View {
                text: String::new(),
                cursor_col: 0,
                sel_cols: None,
            };
        }
        if self.cursor < self.scroll {
            self.scroll = self.cursor;
        }
        // Scroll forward until the cursor fits; it may rest on the column
        // just past the field, right after the last character typed.
        loop {
            let used: usize = self.chars[self.scroll..self.cursor]
                .iter()
                .map(|c| c.width())
                .sum();
            if used <= width || self.scroll >= self.cursor {
                break;
            }
            self.scroll += 1;
        }
        let mut text = String::new();
        let mut used = 0usize;
        let mut end_idx = self.scroll;
        for c in &self.chars[self.scroll..] {
            let w = c.width();
            if used + w > width {
                break;
            }
            used += w;
            text.push_str(c);
            end_idx += 1;
        }
        let cursor_col: usize = self.chars[self.scroll..self.cursor]
            .iter()
            .map(|c| c.width())
            .sum();

        let sel_cols = self.selection_range().and_then(|(lo, hi)| {
            let vis_lo = lo.max(self.scroll);
            let vis_hi = hi.min(end_idx);
            if vis_lo >= vis_hi {
                return None;
            }
            let col = |idx: usize| -> u16 {
                self.chars[self.scroll..idx]
                    .iter()
                    .map(|c| c.width())
                    .sum::<usize>() as u16
            };
            Some((col(vis_lo), col(vis_hi)))
        });

        View {
            text,
            cursor_col: cursor_col.min(width) as u16,
            sel_cols,
        }
    }
}

fn bounded_graphemes(value: &str, max_len: usize, max_bytes: usize) -> Vec<String> {
    let mut result = Vec::with_capacity(value.len().min(max_len));
    let mut bytes = 0usize;
    for grapheme in value.graphemes(true).take(max_len) {
        if bytes.saturating_add(grapheme.len()) > max_bytes {
            break;
        }
        bytes += grapheme.len();
        result.push(grapheme.to_string());
    }
    result
}

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

    fn input(text: &str) -> TextInput {
        TextInput::new(text, 256)
    }

    #[test]
    fn edits_at_the_cursor() {
        let mut i = input("hello");
        i.left();
        i.insert('!');
        assert_eq!(i.value(), "hell!o");
        i.backspace();
        assert_eq!(i.value(), "hello");
        i.delete();
        assert_eq!(i.value(), "hell");
    }

    #[test]
    fn moves_and_deletes_by_word() {
        let mut i = input("one two three");
        i.word_left();
        assert_eq!(i.cursor, 8);
        i.word_left();
        assert_eq!(i.cursor, 4);
        i.delete_word_left();
        assert_eq!(i.value(), "two three");
        i.end();
        i.delete_to_start();
        assert!(i.is_empty());
    }

    #[test]
    fn honours_the_length_limit() {
        let mut i = TextInput::new("ab", 2);
        i.insert('c');
        assert_eq!(i.value(), "ab");
    }

    #[test]
    fn combining_sequences_cannot_bypass_the_byte_limit() {
        let mut typed = TextInput::new("", 1);
        typed.insert('e');
        for _ in 0..1_000 {
            typed.insert('\u{301}');
        }
        assert_eq!(typed.len(), 1);
        assert!(typed.value().len() <= crate::model::text_byte_limit(1));

        let pasted = format!("e{}", "\u{301}".repeat(1_000));
        let pasted = TextInput::new(&pasted, 1);
        assert!(pasted.value().len() <= crate::model::text_byte_limit(1));
    }

    #[test]
    fn appending_inputs_preserves_the_same_limits_as_typing() {
        let mut input = TextInput::new("a", 1);
        assert!(!input.append(&TextInput::new("b", 1)));
        assert_eq!(input.value(), "a");
    }

    #[test]
    fn scrolls_to_keep_the_cursor_in_view() {
        let mut i = input("abcdefghij");
        let view = i.visible(4);
        assert_eq!(view.text, "ghij");
        assert_eq!(view.cursor_col, 4);
        i.home();
        let view = i.visible(4);
        assert_eq!(view.text, "abcd");
        assert_eq!(view.cursor_col, 0);
    }

    #[test]
    fn measures_wide_characters_in_columns() {
        let mut i = input("买菜");
        let view = i.visible(4);
        assert_eq!(view.text, "买菜");
        assert_eq!(view.cursor_col, 4);
    }

    #[test]
    fn cursor_and_deletion_treat_combining_sequences_as_one_grapheme() {
        let mut i = input("e\u{301}x");
        assert_eq!(i.len(), 2);
        i.left();
        i.backspace();
        assert_eq!(i.value(), "x");
        assert_eq!(i.cursor(), 0);
    }

    #[test]
    fn cursor_and_selection_do_not_split_zwj_emoji() {
        let family = "👨‍👩‍👧‍👦";
        let mut i = input(&format!("{family}!"));
        assert_eq!(i.len(), 2);
        i.home();
        i.select_right();
        assert_eq!(i.selected_text().as_deref(), Some(family));
        i.delete_selection();
        assert_eq!(i.value(), "!");
    }

    #[test]
    fn paste_keeps_graphemes_but_strips_terminal_controls() {
        let family = "👨‍👩‍👧‍👦";
        let mut i = TextInput::new("", 64);
        i.insert_str(&format!("e\u{301}\t{family}\u{1b}\u{7f}\u{85}!"));

        assert_eq!(i.value(), format!("e\u{301} {family}!"));
        assert_eq!(i.len(), 4);
        assert!(!i.value().chars().any(char::is_control));
    }

    #[test]
    fn separately_typed_combining_marks_merge_with_the_previous_grapheme() {
        let mut i = TextInput::new("", 1);
        i.insert('e');
        i.insert('\u{301}');

        assert_eq!(i.value(), "e\u{301}");
        assert_eq!(i.len(), 1);
    }

    #[test]
    fn select_word_picks_the_word_under_the_cursor() {
        let mut i = input("one two three");
        i.home();
        i.word_right(); // after "one "
        i.right(); // on 't' of two
        i.select_word();
        assert_eq!(i.selected_text().as_deref(), Some("two"));
        assert_eq!(i.selection_range(), Some((4, 7)));
    }

    #[test]
    fn shift_arrows_extend_the_selection() {
        let mut i = input("hello");
        i.home();
        i.select_right();
        i.select_right();
        assert_eq!(i.selected_text().as_deref(), Some("he"));
        i.delete_selection();
        assert_eq!(i.value(), "llo");
    }

    #[test]
    fn typing_replaces_the_selection() {
        let mut i = input("hello");
        i.home();
        i.select_word();
        i.insert('x');
        assert_eq!(i.value(), "x");
    }

    #[test]
    fn wraps_on_spaces_then_hard_breaks() {
        let s: Vec<String> = "one two three"
            .graphemes(true)
            .map(str::to_string)
            .collect();
        let breaks = wrap_breaks(&s, 8);
        let parts: Vec<String> = breaks.iter().map(|&(a, b)| s[a..b].concat()).collect();
        // width 8 fits "one two " then "three"
        assert!(parts.len() >= 2);
        assert!(parts[0].starts_with("one"));
        assert!(parts.iter().any(|p| p.contains("three")));
    }

    #[test]
    fn wrap_ranges_preserve_every_space_and_accept_every_caret_position() {
        let mut input = input("word   next");
        let breaks = input.wrap_breaks(5);
        assert_eq!(breaks.first().map(|range| range.0), Some(0));
        assert_eq!(breaks.last().map(|range| range.1), Some(input.len()));
        assert!(breaks.windows(2).all(|pair| pair[0].1 == pair[1].0));
        assert_eq!(
            breaks
                .iter()
                .map(|&(start, end)| input.slice(start, end))
                .collect::<String>(),
            input.value()
        );

        for cursor in 0..=input.len() {
            input.set_cursor(cursor);
            let _ = input.wrap_cursor_from_breaks(&breaks);
        }
    }

    #[test]
    fn wrap_height_is_at_least_one() {
        let i = input("");
        assert_eq!(i.wrap_height(10), 1);
        let i = input("hello world again");
        assert!(i.wrap_height(6) >= 2);
    }

    #[test]
    fn wrap_cursor_tracks_rows() {
        let mut i = input("aaaa bbbb cccc");
        i.home();
        // width 5 → "aaaa " then "bbbb " then "cccc"
        let (row, col) = i.wrap_cursor(5);
        assert_eq!((row, col), (0, 0));
        i.end();
        let (row, _) = i.wrap_cursor(5);
        assert!(row >= 1);
    }
}