lune-tty 0.1.0

A GPU-accelerated Wayland terminal emulator for Linux
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
//! Unified incremental search over terminal scrollback and visible rows
//! for both normal mode (`Ctrl+Shift+F`) and Vi mode (`/` / `?` / `n` / `N`).

use std::ops::Range;

use lune_terminal::{CellPosition, CellText, Row, ScreenBuffer, ScreenView};
use regex::{Regex, RegexBuilder};

use super::vi::Direction;

const QUERY_LIMIT: usize = 512;
const REGEX_LIMIT: usize = 1 << 20;
const MAX_MATCHES: usize = 10_000;

/// Persisted search settings shared across normal search and Vi search (`n`/`N`).
#[derive(Clone, Debug, PartialEq, Eq)]
pub(super) struct SavedSearch {
    pub(super) query: String,
    pub(super) direction: Direction,
    pub(super) case_sensitive: bool,
    pub(super) regex_mode: bool,
}

impl Default for SavedSearch {
    fn default() -> Self {
        Self {
            query: String::new(),
            direction: Direction::Forward,
            case_sensitive: false,
            regex_mode: false,
        }
    }
}

impl SavedSearch {
    /// Finds the next match from `origin` in `self.direction` (or its opposite
    /// when `reverse` is true), wrapping across the screen view.
    pub(super) fn find_from(
        &self,
        view: ScreenView<'_>,
        columns: usize,
        origin: CellPosition,
        reverse: bool,
    ) -> Option<CellPosition> {
        if self.query.is_empty() || columns == 0 || view.rows() == 0 {
            return None;
        }
        let regex = compile_regex(&self.query, self.case_sensitive, self.regex_mode)?;
        let (matches, _) = collect_matches(view, columns, &regex, None);
        if matches.is_empty() {
            return None;
        }
        let direction = if reverse {
            opposite(self.direction)
        } else {
            self.direction
        };
        let index = select_from_origin(&matches, origin, direction, true);
        Some(matches[index].start)
    }
}

/// A matched cell span in chronological `ScreenView` coordinates.
/// `start` is inclusive; `end` is exclusive on `end.row`.
#[derive(Clone, Copy, Debug, PartialEq, Eq)]
pub(super) struct SearchMatch {
    pub(super) start: CellPosition,
    pub(super) end: CellPosition,
}

impl SearchMatch {
    /// Returns the half-open column span covered by this match on `row`.
    pub(super) fn columns_on_row(&self, row: usize, columns: usize) -> Option<Range<usize>> {
        if row < self.start.row || row > self.end.row || columns == 0 {
            return None;
        }
        let start = if row == self.start.row {
            self.start.column.min(columns)
        } else {
            0
        };
        let end = if row == self.end.row {
            self.end.column.min(columns)
        } else {
            columns
        };
        (start < end).then_some(start..end)
    }
}

pub(super) struct SearchBar {
    pub(super) query: String,
    pub(super) direction: Direction,
    pub(super) case_sensitive: bool,
    pub(super) regex_mode: bool,
    pub(super) vi_mode: bool,
    pub(super) vi_origin: Option<CellPosition>,
    pub(super) invalid_regex: bool,
    pub(super) matches: Vec<SearchMatch>,
    pub(super) current: Option<usize>,
    pub(super) buffer: ScreenBuffer,
    pub(super) row_scope: Option<Range<usize>>,
    truncated: bool,
}

impl SearchBar {
    /// Opens the unified search bar in normal mode, restoring previous settings.
    pub(super) fn open(
        saved: Option<SavedSearch>,
        buffer: ScreenBuffer,
        view: ScreenView<'_>,
        columns: usize,
        viewport: Range<usize>,
    ) -> Self {
        Self::open_with_scope(saved, buffer, view, columns, viewport, None)
    }

    /// Opens the unified search bar restricted to `row_scope` (e.g. a single command block).
    pub(super) fn open_scoped(
        saved: Option<SavedSearch>,
        buffer: ScreenBuffer,
        view: ScreenView<'_>,
        columns: usize,
        viewport: Range<usize>,
        row_scope: Range<usize>,
    ) -> Self {
        Self::open_with_scope(saved, buffer, view, columns, viewport, Some(row_scope))
    }

    fn open_with_scope(
        saved: Option<SavedSearch>,
        buffer: ScreenBuffer,
        view: ScreenView<'_>,
        columns: usize,
        viewport: Range<usize>,
        row_scope: Option<Range<usize>>,
    ) -> Self {
        let saved = saved.unwrap_or_default();
        let mut bar = Self {
            query: saved.query,
            direction: Direction::Forward,
            case_sensitive: saved.case_sensitive,
            regex_mode: saved.regex_mode,
            vi_mode: false,
            vi_origin: None,
            invalid_regex: false,
            matches: Vec::new(),
            current: None,
            buffer,
            row_scope,
            truncated: false,
        };
        bar.recompute(view, columns, viewport);
        bar
    }

    /// Opens the unified search bar from Vi mode (`/` or `?`) with a fresh query
    /// anchored at `origin`.
    pub(super) fn open_vi(
        saved: Option<&SavedSearch>,
        direction: Direction,
        origin: CellPosition,
        buffer: ScreenBuffer,
    ) -> Self {
        let case_sensitive = saved.is_some_and(|saved| saved.case_sensitive);
        Self {
            query: String::new(),
            direction,
            case_sensitive,
            regex_mode: true,
            vi_mode: true,
            vi_origin: Some(origin),
            invalid_regex: false,
            matches: Vec::new(),
            current: None,
            buffer,
            row_scope: None,
            truncated: false,
        }
    }

    pub(super) fn into_saved(self, fallback: Option<SavedSearch>) -> SavedSearch {
        if self.query.is_empty()
            && let Some(mut previous) = fallback
        {
            previous.direction = self.direction;
            return previous;
        }
        SavedSearch {
            query: self.query,
            direction: self.direction,
            case_sensitive: self.case_sensitive,
            regex_mode: self.regex_mode,
        }
    }

    pub(super) fn push(&mut self, text: &str) -> bool {
        let before = self.query.len();
        for ch in text.chars().filter(|ch| !ch.is_control()) {
            if self.query.len() + ch.len_utf8() > QUERY_LIMIT {
                break;
            }
            self.query.push(ch);
        }
        self.query.len() != before
    }

    pub(super) fn backspace(&mut self) -> bool {
        self.query.pop().is_some()
    }

    pub(super) fn delete_word(&mut self) -> bool {
        let before = self.query.len();
        while self
            .query
            .chars()
            .next_back()
            .is_some_and(char::is_whitespace)
        {
            self.query.pop();
        }
        while self
            .query
            .chars()
            .next_back()
            .is_some_and(|ch| !ch.is_whitespace())
        {
            self.query.pop();
        }
        self.query.len() != before
    }

    pub(super) fn clear(&mut self) -> bool {
        if self.query.is_empty() {
            return false;
        }
        self.query.clear();
        true
    }

    pub(super) fn toggle_case(&mut self) {
        self.case_sensitive = !self.case_sensitive;
    }

    pub(super) fn toggle_regex(&mut self) {
        self.regex_mode = !self.regex_mode;
    }

    pub(super) fn recompute(
        &mut self,
        view: ScreenView<'_>,
        columns: usize,
        viewport: Range<usize>,
    ) {
        let previous_anchor = self
            .current
            .and_then(|idx| self.matches.get(idx))
            .map(|m| m.start);
        self.matches.clear();
        self.current = None;
        self.invalid_regex = false;
        self.truncated = false;

        if self.query.is_empty() || columns == 0 || view.rows() == 0 {
            return;
        }
        let Some(regex) = compile_regex(&self.query, self.case_sensitive, self.regex_mode) else {
            self.invalid_regex = true;
            return;
        };

        let (matches, truncated) = collect_matches(view, columns, &regex, self.row_scope.as_ref());
        self.matches = matches;
        self.truncated = truncated;
        if self.matches.is_empty() {
            return;
        }
        self.current = Some(if let Some(origin) = self.vi_origin {
            select_from_origin(&self.matches, origin, self.direction, true)
        } else {
            select_initial(&self.matches, previous_anchor, viewport)
        });
    }

    /// Refreshes matches after terminal content changes.
    pub(super) fn refresh(
        &mut self,
        buffer: ScreenBuffer,
        view: ScreenView<'_>,
        columns: usize,
        viewport: Range<usize>,
    ) {
        self.buffer = buffer;
        self.recompute(view, columns, viewport);
    }

    /// Moves to the next (`forward == true`) or previous match and returns its
    /// chronological start position.
    pub(super) fn step(&mut self, forward: bool) -> Option<CellPosition> {
        let len = self.matches.len();
        if len == 0 {
            return None;
        }
        let next = match self.current {
            Some(index) if forward => (index + 1) % len,
            Some(index) => index.checked_sub(1).unwrap_or(len - 1),
            None if forward => 0,
            None => len - 1,
        };
        self.current = Some(next);
        Some(self.matches[next].start)
    }

    pub(super) fn current_match(&self) -> Option<CellPosition> {
        self.current
            .and_then(|idx| self.matches.get(idx))
            .map(|m| m.start)
    }

    /// Iterates matches overlapping `rows`, yielding `(is_current, match)`.
    pub(super) fn visible_matches(
        &self,
        rows: Range<usize>,
    ) -> impl Iterator<Item = (bool, SearchMatch)> + '_ {
        let first = self.matches.partition_point(|m| m.end.row < rows.start);
        let current = self.current;
        self.matches[first..]
            .iter()
            .enumerate()
            .take_while(move |(_, m)| m.start.row < rows.end)
            .map(move |(offset, &m)| (current == Some(first + offset), m))
    }

    pub(super) fn status_label(&self) -> String {
        if self.invalid_regex {
            return "invalid".to_owned();
        }
        if self.query.is_empty() {
            return String::new();
        }
        let total = self.matches.len();
        if total == 0 {
            return "0/0".to_owned();
        }
        let index = self.current.map_or(0, |idx| idx + 1);
        if self.truncated {
            format!("{index}/{total}+")
        } else {
            format!("{index}/{total}")
        }
    }
}

fn opposite(direction: Direction) -> Direction {
    match direction {
        Direction::Forward => Direction::Backward,
        Direction::Backward => Direction::Forward,
    }
}

fn compile_regex(query: &str, case_sensitive: bool, regex_mode: bool) -> Option<Regex> {
    let pattern = if regex_mode {
        std::borrow::Cow::Borrowed(query)
    } else {
        std::borrow::Cow::Owned(regex::escape(query))
    };
    RegexBuilder::new(&pattern)
        .case_insensitive(!case_sensitive)
        .size_limit(REGEX_LIMIT)
        .dfa_size_limit(REGEX_LIMIT)
        .build()
        .ok()
}

fn collect_matches(
    view: ScreenView<'_>,
    columns: usize,
    regex: &Regex,
    row_scope: Option<&Range<usize>>,
) -> (Vec<SearchMatch>, bool) {
    let mut matches = Vec::new();
    let mut truncated = false;
    let mut line = LogicalText::default();
    for logical in view.logical_lines() {
        let rows = logical.range();
        if let Some(scope) = row_scope {
            if rows.end <= scope.start {
                continue;
            }
            if rows.start >= scope.end {
                break;
            }
        }
        line.read(view, rows, columns);
        if line.text.is_empty() {
            continue;
        }
        for matched in regex.find_iter(&line.text) {
            if matched.start() == matched.end() {
                continue;
            }
            if let Some(span) = line.span(view, matched.start(), matched.end(), columns) {
                matches.push(span);
                if matches.len() >= MAX_MATCHES {
                    truncated = true;
                    break;
                }
            }
        }
        if truncated {
            break;
        }
    }
    (matches, truncated)
}

fn pos_key(pos: CellPosition) -> (usize, usize) {
    (pos.row, pos.column)
}

fn select_from_origin(
    matches: &[SearchMatch],
    origin: CellPosition,
    direction: Direction,
    exclusive: bool,
) -> usize {
    let key = pos_key(origin);
    match direction {
        Direction::Forward => {
            let idx = if exclusive {
                matches.partition_point(|m| pos_key(m.start) <= key)
            } else {
                matches.partition_point(|m| pos_key(m.start) < key)
            };
            if idx < matches.len() { idx } else { 0 }
        }
        Direction::Backward => {
            let idx = if exclusive {
                matches.partition_point(|m| pos_key(m.start) < key)
            } else {
                matches.partition_point(|m| pos_key(m.start) <= key)
            };
            idx.checked_sub(1).unwrap_or(matches.len() - 1)
        }
    }
}

fn select_initial(
    matches: &[SearchMatch],
    previous: Option<CellPosition>,
    viewport: Range<usize>,
) -> usize {
    if let Some(anchor) = previous
        && let Ok(index) = matches.binary_search_by_key(&anchor, |m| m.start)
    {
        return index;
    }
    let after_viewport = matches.partition_point(|m| m.start.row < viewport.end);
    if after_viewport > 0 {
        after_viewport - 1
    } else {
        0
    }
}

#[derive(Default)]
struct LogicalText {
    text: String,
    first_row: usize,
    row_starts: Vec<usize>,
    row_ascii_1to1: Vec<bool>,
}

impl LogicalText {
    fn read(&mut self, view: ScreenView<'_>, rows: Range<usize>, columns: usize) {
        self.text.clear();
        self.first_row = rows.start;
        self.row_starts.clear();
        self.row_ascii_1to1.clear();
        for index in rows {
            let row_start = self.text.len();
            self.row_starts.push(row_start);
            let Some(row) = view.row(index) else {
                self.row_ascii_1to1.push(true);
                continue;
            };
            if self.try_read_ascii_row(row, columns) {
                self.row_ascii_1to1.push(true);
            } else {
                self.text.truncate(row_start);
                self.row_ascii_1to1.push(false);
                visit_row(row, columns, |_, _, text| match text {
                    CellText::Scalar(character) => self.text.push(character),
                    CellText::Extended(text) => self.text.push_str(text),
                    CellText::Continuation => unreachable!(),
                });
            }
        }
    }

    /// Fast path for ordinary single-width ASCII rows without sized text or
    /// extended grapheme clusters: maps `byte - row_start == column` 1:1.
    fn try_read_ascii_row(&mut self, row: &Row, columns: usize) -> bool {
        if row.sized_text_rows().next().is_some() {
            return false;
        }
        let limit = row.cells().len().min(columns);
        let end = if let Some(wrap) = row.wrap_end() {
            wrap.min(limit)
        } else {
            row.cells()[..limit]
                .iter()
                .rposition(|cell| !cell.is_erased())
                .map_or(0, |last| last + 1)
        };
        if end == 0 {
            return true;
        }
        let cells = &row.cells()[..end];
        let mut buf = [0u8; 256];
        let mut filled = 0usize;
        for cell in cells {
            if cell.has_extended_text() || cell.is_continuation() || cell.width() != 1 {
                return false;
            }
            let byte = if cell.is_erased() {
                b' '
            } else {
                let Some(ch) = cell.character() else {
                    return false;
                };
                if !ch.is_ascii() {
                    return false;
                }
                ch as u8
            };
            buf[filled] = byte;
            filled += 1;
            if filled == buf.len() {
                // Every byte written to `buf` is verified ASCII (< 0x80), so UTF-8 is guaranteed.
                if let Ok(chunk) = std::str::from_utf8(&buf[..filled]) {
                    self.text.push_str(chunk);
                }
                filled = 0;
            }
        }
        if filled > 0
            && let Ok(chunk) = std::str::from_utf8(&buf[..filled])
        {
            self.text.push_str(chunk);
        }
        true
    }

    fn span(
        &self,
        view: ScreenView<'_>,
        start_byte: usize,
        end_byte: usize,
        columns: usize,
    ) -> Option<SearchMatch> {
        let start = self.position(view, start_byte, columns, false)?;
        let last_byte = end_byte.checked_sub(1)?;
        let end = self.position(view, last_byte, columns, true)?;
        Some(SearchMatch { start, end })
    }

    fn position(
        &self,
        view: ScreenView<'_>,
        offset: usize,
        columns: usize,
        exclusive_end: bool,
    ) -> Option<CellPosition> {
        let byte = offset.min(self.text.len().checked_sub(1)?);
        let index = if self.row_starts.len() == 1 {
            0
        } else {
            self.row_starts
                .partition_point(|&start| start <= byte)
                .checked_sub(1)?
        };
        let row_index = self.first_row + index;
        let row_start = self.row_starts[index];
        if self.row_ascii_1to1[index] {
            let col = byte - row_start;
            return Some(CellPosition {
                row: row_index,
                column: if exclusive_end {
                    (col + 1).min(columns)
                } else {
                    col
                },
            });
        }
        let mut start = row_start;
        let mut position = None;
        visit_row_until(view.row(row_index)?, columns, |column, width, text| {
            if start <= byte {
                let col = if exclusive_end {
                    (column + width).min(columns)
                } else {
                    column
                };
                position = Some(CellPosition {
                    row: row_index,
                    column: col,
                });
                start += utf8_len(text);
                start <= byte
            } else {
                false
            }
        });
        position
    }
}

fn utf8_len(text: CellText<'_>) -> usize {
    match text {
        CellText::Scalar(character) => character.len_utf8(),
        CellText::Extended(text) => text.len(),
        CellText::Continuation => unreachable!(),
    }
}

fn visit_row(row: &Row, columns: usize, mut visit: impl FnMut(usize, usize, CellText<'_>)) {
    visit_row_until(row, columns, |col, width, text| {
        visit(col, width, text);
        true
    });
}

fn visit_row_until(
    row: &Row,
    columns: usize,
    mut visit: impl FnMut(usize, usize, CellText<'_>) -> bool,
) {
    let end = if let Some(wrap) = row.wrap_end() {
        wrap.min(columns)
    } else {
        let limit = row.cells().len().min(columns);
        let cells = row.cells()[..limit]
            .iter()
            .rposition(|cell| !cell.is_erased())
            .map_or(0, |last| last + 1);
        row.sized_text_rows()
            .take_while(|part| part.column < limit)
            .last()
            .map_or(cells, |part| cells.max(part.columns().end.min(limit)))
    };
    let mut column = 0;
    for part in row.sized_text_rows().take_while(|part| part.column < end) {
        for unit in row.display_units(column..part.column) {
            if !visit(unit.column, unit.cell.width(), unit.text) {
                return;
            }
        }
        if part.row_offset == 0 {
            if !visit(
                part.column,
                part.columns().len(),
                CellText::Extended(part.text()),
            ) {
                return;
            }
        } else {
            for col in part.column..part.columns().end.min(end) {
                if !visit(col, 1, CellText::Scalar(' ')) {
                    return;
                }
            }
        }
        column = part.columns().end;
    }
    for unit in row.display_units(column..end) {
        if !visit(unit.column, unit.cell.width(), unit.text) {
            return;
        }
    }
}

#[cfg(test)]
mod tests {
    use lune_terminal::{MAX_STYLES, Processor, Screen, ScreenBuffer, UnicodeWriter};

    use super::*;

    fn feed_screen(columns: usize, rows: usize, text: &str) -> Screen {
        let (palette, _) = super::super::theme::palette(winit::window::Theme::Dark);
        let mut processor = Processor::new(MAX_STYLES, palette).unwrap();
        let mut screen = Screen::new(columns, rows).unwrap();
        let mut writer = UnicodeWriter::default();
        processor
            .advance(text.as_bytes(), &mut screen, &mut writer)
            .unwrap();
        screen
    }

    #[test]
    fn literal_case_insensitive_and_case_sensitive_search() {
        let screen = feed_screen(40, 5, "Alpha alpha ALPHA\r\nfoo.bar fooXbar");
        let view = screen.view();
        let mut bar = SearchBar::open(None, ScreenBuffer::Primary, view, 40, 0..5);

        bar.push("alpha");
        bar.recompute(view, 40, 0..5);
        assert_eq!(bar.matches.len(), 3);
        assert_eq!(bar.status_label(), "3/3");

        bar.toggle_case();
        bar.recompute(view, 40, 0..5);
        assert_eq!(bar.matches.len(), 1);
        assert_eq!(bar.matches[0].start, CellPosition { row: 0, column: 6 });
        assert_eq!(bar.matches[0].end, CellPosition { row: 0, column: 11 });

        // Literal mode treats `.` as a literal dot rather than wildcard.
        bar.clear();
        bar.push("foo.bar");
        bar.recompute(view, 40, 0..5);
        assert_eq!(bar.matches.len(), 1);
        assert_eq!(bar.matches[0].start, CellPosition { row: 1, column: 0 });

        // Regex mode matches both `foo.bar` and `fooXbar`.
        bar.toggle_regex();
        bar.recompute(view, 40, 0..5);
        assert_eq!(bar.matches.len(), 2);
    }

    #[test]
    fn wrapped_lines_and_wide_characters_map_exact_columns() {
        let screen = feed_screen(6, 4, "ab中文cd");
        let view = screen.view();
        let mut bar = SearchBar::open(None, ScreenBuffer::Primary, view, 6, 0..4);

        bar.push("中文c");
        bar.recompute(view, 6, 0..4);
        assert_eq!(bar.matches.len(), 1);
        let matched = bar.matches[0];
        assert_eq!(matched.start, CellPosition { row: 0, column: 2 });
        assert_eq!(matched.end, CellPosition { row: 1, column: 1 });
        assert_eq!(matched.columns_on_row(0, 6), Some(2..6));
        assert_eq!(matched.columns_on_row(1, 6), Some(0..1));
        assert_eq!(matched.columns_on_row(2, 6), None);
    }

    #[test]
    fn invalid_regex_and_stepping_wrap_cleanly() {
        let screen = feed_screen(20, 4, "one\r\ntwo\r\none");
        let view = screen.view();
        let mut bar = SearchBar::open(None, ScreenBuffer::Primary, view, 20, 0..4);

        bar.toggle_regex();
        bar.push("[unclosed");
        bar.recompute(view, 20, 0..4);
        assert!(bar.invalid_regex);
        assert_eq!(bar.status_label(), "invalid");
        assert!(bar.matches.is_empty());

        bar.clear();
        bar.push("one");
        bar.recompute(view, 20, 0..4);
        assert_eq!(bar.matches.len(), 2);
        assert_eq!(bar.current, Some(1));
        assert_eq!(bar.step(true), Some(CellPosition { row: 0, column: 0 }));
        assert_eq!(bar.step(true), Some(CellPosition { row: 2, column: 0 }));
        assert_eq!(bar.step(false), Some(CellPosition { row: 0, column: 0 }));
    }

    #[test]
    fn vi_regex_search_crosses_soft_wraps_and_repeats_from_origin() {
        let screen = feed_screen(8, 4, "abcdef界ij\r\nnext 界ij");
        let view = screen.view();
        let origin = CellPosition { row: 0, column: 0 };

        let mut bar = SearchBar::open_vi(None, Direction::Forward, origin, ScreenBuffer::Primary);
        bar.push("f界i.");
        bar.recompute(view, 8, 0..4);
        let first = bar.current_match().unwrap();
        assert_eq!(first, CellPosition { row: 0, column: 5 });

        let mut bar = SearchBar::open_vi(None, Direction::Forward, first, ScreenBuffer::Primary);
        bar.push("界i.");
        bar.recompute(view, 8, 0..4);
        let second = bar.current_match().unwrap();
        assert_eq!(second, CellPosition { row: 0, column: 6 });

        let saved = bar.into_saved(None);
        assert_eq!(
            saved.find_from(view, 8, second, false),
            Some(CellPosition { row: 2, column: 5 })
        );
        assert_eq!(
            saved.find_from(view, 8, CellPosition { row: 2, column: 5 }, true),
            Some(CellPosition { row: 0, column: 6 })
        );
    }
}