logana 0.1.0

A TUI log analyzer/viewer built for speed - handles files with millions of lines with instant filtering and VIM like navigation.
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
//! Regex-based search over visible log lines with wrapping navigation.
//!
//! [`Search`] operates on `visible_indices` only (respects active filters).
//! Builds a [`Vec<SearchResult>`] with byte-position match spans, then
//! provides wrapping [`Search::next_match`] / [`Search::previous_match`].
//!
//! - `search(pattern, impl Iterator<Item=usize>, &FileReader)`: accepts an
//!   iterator so both `VisibleLines::All` and `VisibleLines::Filtered` can be
//!   passed without materialising a `Vec`.
//! - `set_pattern(regex, forward)`: pre-sets the active regex without replacing
//!   results; used during background search so highlights appear immediately.
//! - `set_results(results, regex)`: called when the background task delivers
//!   results; replaces the result set and updates the pattern atomically.
//! - `set_case_sensitive`: toggles case sensitivity.
//! - `results` is always sorted by `line_idx`; render uses
//!   `binary_search_by_key` for O(log N) per-line lookup with zero allocation
//!   per frame.

use crate::types::SearchResult;
use regex::Regex;

#[derive(Debug, Clone)]
pub struct Search {
    pattern: Option<Regex>,
    results: Vec<SearchResult>,
    /// Index into `results` (which line).
    current_result_index: usize,
    /// Index into `results[current_result_index].matches` (which occurrence on that line).
    current_occurrence_index: usize,
    case_sensitive: bool,
    /// Direction of the last confirmed search. `true` = forward (`/`), `false` = backward (`?`).
    /// Determines whether `n` continues forward or backward (vim semantics).
    forward: bool,
}

impl Default for Search {
    fn default() -> Self {
        Self::new()
    }
}

impl Search {
    pub fn new() -> Self {
        Search {
            pattern: None,
            results: Vec::new(),
            current_result_index: 0,
            current_occurrence_index: 0,
            case_sensitive: true,
            forward: true,
        }
    }

    /// Search `visible_indices` lines for `pattern_str`.
    ///
    /// `get_text` maps a `line_idx` to the string that should be searched —
    /// callers should supply the *displayed* text (after field filtering) so
    /// that hidden fields are never counted as matches.
    pub fn search(
        &mut self,
        pattern_str: &str,
        visible_indices: impl Iterator<Item = usize>,
        get_text: impl Fn(usize) -> Option<String>,
    ) -> anyhow::Result<()> {
        let pattern = if self.case_sensitive {
            Regex::new(pattern_str)?
        } else {
            Regex::new(&format!("(?i){}", pattern_str))?
        };
        self.pattern = Some(pattern.clone());
        self.results.clear();
        self.current_result_index = 0;
        self.current_occurrence_index = 0;

        for line_idx in visible_indices {
            let text = match get_text(line_idx) {
                Some(t) => t,
                None => continue,
            };
            let matches: Vec<(usize, usize)> = pattern
                .find_iter(&text)
                .map(|m| (m.start(), m.end()))
                .collect();
            if !matches.is_empty() {
                self.results.push(SearchResult { line_idx, matches });
            }
        }
        Ok(())
    }

    /// Advance to the next occurrence. Stays on the same line if it has more
    /// occurrences; wraps to the first occurrence of the next line otherwise.
    pub fn next_match(&mut self) -> Option<&SearchResult> {
        if self.results.is_empty() {
            return None;
        }
        let current_line_matches = self.results[self.current_result_index].matches.len();
        if self.current_occurrence_index + 1 < current_line_matches {
            self.current_occurrence_index += 1;
        } else {
            self.current_result_index = (self.current_result_index + 1) % self.results.len();
            self.current_occurrence_index = 0;
        }
        Some(&self.results[self.current_result_index])
    }

    /// Go back to the previous occurrence. Stays on the same line if not at the
    /// first occurrence; wraps to the last occurrence of the previous line otherwise.
    pub fn previous_match(&mut self) -> Option<&SearchResult> {
        if self.results.is_empty() {
            return None;
        }
        if self.current_occurrence_index > 0 {
            self.current_occurrence_index -= 1;
        } else {
            if self.current_result_index == 0 {
                self.current_result_index = self.results.len() - 1;
            } else {
                self.current_result_index -= 1;
            }
            self.current_occurrence_index = self.results[self.current_result_index]
                .matches
                .len()
                .saturating_sub(1);
        }
        Some(&self.results[self.current_result_index])
    }

    pub fn get_current_match(&self) -> Option<&SearchResult> {
        if self.results.is_empty() {
            None
        } else {
            Some(&self.results[self.current_result_index])
        }
    }

    pub fn get_results(&self) -> &[SearchResult] {
        &self.results
    }

    pub fn set_case_sensitive(&mut self, case_sensitive: bool) {
        self.case_sensitive = case_sensitive;
    }

    pub fn is_case_sensitive(&self) -> bool {
        self.case_sensitive
    }

    /// Pre-set the compiled pattern without replacing results.
    ///
    /// Used by background search to enable highlighting immediately while the
    /// scan is still in progress (stale results remain visible).
    pub fn set_pattern(&mut self, pattern: Regex, forward: bool) {
        self.pattern = Some(pattern);
        self.forward = forward;
    }

    /// Replace results once a background search completes.
    pub fn set_results(&mut self, results: Vec<crate::types::SearchResult>, pattern: Regex) {
        self.results = results;
        self.pattern = Some(pattern);
        self.current_result_index = 0;
        self.current_occurrence_index = 0;
    }

    /// Store the search direction so that `go_next` / `go_prev` respect vim semantics:
    /// `n` repeats in the original direction, `N` reverses it.
    pub fn set_forward(&mut self, forward: bool) {
        self.forward = forward;
    }

    pub fn is_forward(&self) -> bool {
        self.forward
    }

    /// Move to the next occurrence in the **search direction** (`n` in vim).
    pub fn go_next(&mut self) -> Option<&SearchResult> {
        if self.forward {
            self.next_match()
        } else {
            self.previous_match()
        }
    }

    /// Move to the previous occurrence in the **search direction** (`N` in vim).
    pub fn go_prev(&mut self) -> Option<&SearchResult> {
        if self.forward {
            self.previous_match()
        } else {
            self.next_match()
        }
    }

    /// Total number of individual match occurrences across all lines.
    pub fn get_total_match_count(&self) -> usize {
        self.results.iter().map(|r| r.matches.len()).sum()
    }

    /// 1-based global occurrence number across all lines.
    pub fn get_current_occurrence_number(&self) -> usize {
        if self.results.is_empty() {
            return 0;
        }
        self.results[..self.current_result_index]
            .iter()
            .map(|r| r.matches.len())
            .sum::<usize>()
            + self.current_occurrence_index
            + 1
    }

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

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

    /// Position the cursor so that the next call to [`Self::next_match`] / [`Self::previous_match`]
    /// lands on the first occurrence strictly after (`forward=true`) or strictly before
    /// (`forward=false`) `line_idx`, wrapping around when necessary.
    pub fn set_position_for_search(&mut self, line_idx: usize, forward: bool) {
        if self.results.is_empty() {
            return;
        }
        let last = self.results.len() - 1;
        if forward {
            // pos = index of first result with line_idx >= current line (inclusive).
            // Position cursor at pos-1 so next_match() lands on pos (which may be
            // the current line itself, so matches on the current line are included).
            let pos = self.results.partition_point(|r| r.line_idx < line_idx);
            if pos == 0 {
                // All results are at/after line_idx; wrap by seating at last result.
                self.current_result_index = last;
            } else {
                self.current_result_index = pos - 1;
            }
            self.current_occurrence_index = self.results[self.current_result_index]
                .matches
                .len()
                .saturating_sub(1);
        } else {
            // pos = index of first result with line_idx >= current line.
            // Position cursor at pos so previous_match() retreats to pos-1.
            let pos = self.results.partition_point(|r| r.line_idx < line_idx);
            if pos == 0 {
                // All results are at/after line_idx; wrap by seating at first result.
                self.current_result_index = 0;
                self.current_occurrence_index = 0;
            } else {
                self.current_result_index = pos;
                self.current_occurrence_index = 0;
            }
        }
    }

    /// If `line_idx` is the line that holds the current match, returns the
    /// index of the current occurrence within that line's match list.
    pub fn get_current_occurrence_for_line(&self, line_idx: usize) -> Option<usize> {
        if self.results.is_empty() {
            return None;
        }
        let current = &self.results[self.current_result_index];
        if current.line_idx == line_idx {
            Some(self.current_occurrence_index)
        } else {
            None
        }
    }

    pub fn clear(&mut self) {
        self.pattern = None;
        self.results.clear();
        self.current_result_index = 0;
        self.current_occurrence_index = 0;
        self.forward = true;
    }

    pub fn get_pattern(&self) -> Option<&str> {
        self.pattern.as_ref().map(|p| p.as_str())
    }

    pub fn get_compiled_pattern(&self) -> Option<&Regex> {
        self.pattern.as_ref()
    }
}

#[cfg(test)]
mod tests {
    use super::*;
    use crate::file_reader::FileReader;
    use std::io::Write;
    use tempfile::NamedTempFile;

    fn make_reader(lines: &[&str]) -> (NamedTempFile, FileReader) {
        let mut f = NamedTempFile::new().unwrap();
        for line in lines {
            writeln!(f, "{}", line).unwrap();
        }
        let path = f.path().to_str().unwrap().to_string();
        let reader = FileReader::new(&path).unwrap();
        (f, reader)
    }

    /// Helper: build a `get_text` closure from a `FileReader` (raw bytes).
    fn raw_text(reader: &FileReader) -> impl Fn(usize) -> Option<String> + '_ {
        |line_idx| Some(String::from_utf8_lossy(reader.get_line(line_idx)).into_owned())
    }

    #[test]
    fn test_search_basic() -> anyhow::Result<()> {
        let (_f, reader) = make_reader(&[
            "This is a test line",
            "Another line with Test",
            "No match here",
            "test test test",
        ]);
        let all: Vec<usize> = (0..reader.line_count()).collect();
        let mut search = Search::new();
        search.search("test", all.iter().copied(), raw_text(&reader))?;

        let results = search.get_results();
        assert_eq!(results.len(), 2); // lines 0 and 3
        assert_eq!(results[0].line_idx, 0);
        assert_eq!(results[0].matches, vec![(10, 14)]);
        assert_eq!(results[1].line_idx, 3);
        assert_eq!(results[1].matches, vec![(0, 4), (5, 9), (10, 14)]);
        Ok(())
    }

    #[test]
    fn test_search_case_insensitive() -> anyhow::Result<()> {
        let (_f, reader) = make_reader(&[
            "This is a test line",
            "Another line with Test",
            "No match here",
            "test test test",
        ]);
        let all: Vec<usize> = (0..reader.line_count()).collect();
        let mut search = Search::new();
        search.set_case_sensitive(false);
        search.search("test", all.iter().copied(), raw_text(&reader))?;

        let results = search.get_results();
        assert_eq!(results.len(), 3); // lines 0, 1, 3
        assert_eq!(results[0].line_idx, 0);
        assert_eq!(results[1].line_idx, 1);
        assert_eq!(results[2].line_idx, 3);
        Ok(())
    }

    #[test]
    fn test_search_no_match() -> anyhow::Result<()> {
        let (_f, reader) = make_reader(&["This is a line"]);
        let all: Vec<usize> = (0..reader.line_count()).collect();
        let mut search = Search::new();
        search.search("nomatch", all.iter().copied(), raw_text(&reader))?;
        assert!(search.get_results().is_empty());
        Ok(())
    }

    #[test]
    fn test_next_previous_match() -> anyhow::Result<()> {
        let (_f, reader) = make_reader(&["test line", "nothing", "another test"]);
        let all: Vec<usize> = (0..reader.line_count()).collect();
        let mut search = Search::new();
        search.search("test", all.iter().copied(), raw_text(&reader))?;

        // starts at index 0 → line_idx 0
        assert_eq!(search.get_current_match().unwrap().line_idx, 0);

        search.next_match();
        assert_eq!(search.get_current_match().unwrap().line_idx, 2);

        // wrap around
        search.next_match();
        assert_eq!(search.get_current_match().unwrap().line_idx, 0);

        search.previous_match();
        assert_eq!(search.get_current_match().unwrap().line_idx, 2);
        Ok(())
    }

    #[test]
    fn test_search_only_visible() -> anyhow::Result<()> {
        let (_f, reader) = make_reader(&["ERROR: bad", "INFO: good", "ERROR: also bad"]);
        // Only search visible lines (0 and 2 pass the filter)
        let visible = vec![0usize, 2];
        let mut search = Search::new();
        search.search("bad", visible.iter().copied(), raw_text(&reader))?;

        let results = search.get_results();
        assert_eq!(results.len(), 2);
        assert_eq!(results[0].line_idx, 0);
        assert_eq!(results[1].line_idx, 2);
        Ok(())
    }

    #[test]
    fn test_empty_search() {
        let mut search = Search::new();
        assert!(search.next_match().is_none());
        assert!(search.previous_match().is_none());
        assert!(search.get_current_match().is_none());
    }

    #[test]
    fn test_next_match_stays_on_same_line_for_multiple_occurrences() -> anyhow::Result<()> {
        // line 0 has 3 occurrences, line 1 has 1
        let (_f, reader) = make_reader(&["foo foo foo", "foo"]);
        let all: Vec<usize> = (0..reader.line_count()).collect();
        let mut search = Search::new();
        search.search("foo", all.iter().copied(), raw_text(&reader))?;

        assert_eq!(search.get_current_match().unwrap().line_idx, 0); // occ 0
        search.next_match();
        assert_eq!(search.get_current_match().unwrap().line_idx, 0); // occ 1 – same line
        search.next_match();
        assert_eq!(search.get_current_match().unwrap().line_idx, 0); // occ 2 – still same line
        search.next_match();
        assert_eq!(search.get_current_match().unwrap().line_idx, 1); // exhausted → line 1
        Ok(())
    }

    #[test]
    fn test_previous_match_stays_on_same_line_for_multiple_occurrences() -> anyhow::Result<()> {
        let (_f, reader) = make_reader(&["foo foo", "foo"]);
        let all: Vec<usize> = (0..reader.line_count()).collect();
        let mut search = Search::new();
        search.search("foo", all.iter().copied(), raw_text(&reader))?;
        // advance to line 1
        search.next_match();
        search.next_match();
        assert_eq!(search.get_current_match().unwrap().line_idx, 1);
        search.previous_match();
        assert_eq!(search.get_current_match().unwrap().line_idx, 0); // last occ of line 0
        search.previous_match();
        assert_eq!(search.get_current_match().unwrap().line_idx, 0); // first occ of line 0
        Ok(())
    }

    #[test]
    fn test_get_total_match_count() -> anyhow::Result<()> {
        let (_f, reader) = make_reader(&["test test", "nothing", "test"]);
        let all: Vec<usize> = (0..reader.line_count()).collect();
        let mut search = Search::new();
        search.search("test", all.iter().copied(), raw_text(&reader))?;
        // line 0: 2 occurrences, line 2: 1 occurrence → total 3
        assert_eq!(search.get_total_match_count(), 3);
        Ok(())
    }

    #[test]
    fn test_get_total_match_count_empty() {
        let search = Search::new();
        assert_eq!(search.get_total_match_count(), 0);
    }

    #[test]
    fn test_get_current_occurrence_number() -> anyhow::Result<()> {
        // line 0: 2 occurrences ("test test"), line 1: 1 occurrence ("test")
        let (_f, reader) = make_reader(&["test test", "test"]);
        let all: Vec<usize> = (0..reader.line_count()).collect();
        let mut search = Search::new();
        search.search("test", all.iter().copied(), raw_text(&reader))?;
        assert_eq!(search.get_current_occurrence_number(), 1); // result=0, occ=0
        search.next_match(); // stays on line 0, advances to occ=1
        assert_eq!(search.get_current_occurrence_number(), 2);
        search.next_match(); // line 0 exhausted → advances to line 1, occ=0
        assert_eq!(search.get_current_occurrence_number(), 3);
        Ok(())
    }

    #[test]
    fn test_get_current_occurrence_number_empty() {
        let search = Search::new();
        assert_eq!(search.get_current_occurrence_number(), 0);
    }

    #[test]
    fn test_get_current_match_index() -> anyhow::Result<()> {
        let (_f, reader) = make_reader(&["test a", "test b", "test c"]);
        let all: Vec<usize> = (0..reader.line_count()).collect();
        let mut search = Search::new();
        search.search("test", all.iter().copied(), raw_text(&reader))?;
        assert_eq!(search.get_current_match_index(), 0);
        search.next_match();
        assert_eq!(search.get_current_match_index(), 1);
        search.next_match();
        assert_eq!(search.get_current_match_index(), 2);
        Ok(())
    }

    #[test]
    fn test_clear_resets_state() -> anyhow::Result<()> {
        let (_f, reader) = make_reader(&["test line"]);
        let all: Vec<usize> = (0..reader.line_count()).collect();
        let mut search = Search::new();
        search.search("test", all.iter().copied(), raw_text(&reader))?;
        assert!(!search.get_results().is_empty());
        assert!(search.get_pattern().is_some());
        search.clear();
        assert!(search.get_results().is_empty());
        assert!(search.get_pattern().is_none());
        assert_eq!(search.get_current_match_index(), 0);
        Ok(())
    }

    #[test]
    fn test_default() {
        let search = Search::default();
        assert!(search.pattern.is_none());
        assert!(search.results.is_empty());
        assert_eq!(search.current_result_index, 0);
        assert_eq!(search.current_occurrence_index, 0);
        assert!(search.case_sensitive);
        assert!(search.forward);
    }

    #[test]
    fn test_go_next_forward_search() -> anyhow::Result<()> {
        // With forward=true, go_next advances (next_match) and go_prev retreats (previous_match).
        let (_f, reader) = make_reader(&["foo", "bar", "foo"]);
        let all: Vec<usize> = (0..reader.line_count()).collect();
        let mut search = Search::new();
        search.search("foo", all.iter().copied(), raw_text(&reader))?;
        search.set_forward(true);
        // start at result 0 (line 0)
        assert_eq!(search.get_current_match().unwrap().line_idx, 0);
        assert_eq!(search.go_next().unwrap().line_idx, 2); // advances
        assert_eq!(search.go_prev().unwrap().line_idx, 0); // retreats
        Ok(())
    }

    #[test]
    fn test_go_next_backward_search() -> anyhow::Result<()> {
        // With forward=false, go_next retreats (previous_match) and go_prev advances (next_match).
        let (_f, reader) = make_reader(&["foo", "bar", "foo"]);
        let all: Vec<usize> = (0..reader.line_count()).collect();
        let mut search = Search::new();
        search.search("foo", all.iter().copied(), raw_text(&reader))?;
        search.set_forward(false);
        // start at result 0 (line 0)
        assert_eq!(search.get_current_match().unwrap().line_idx, 0);
        // go_next wraps to the last result (backward direction)
        assert_eq!(search.go_next().unwrap().line_idx, 2);
        // go_prev advances back to line 0
        assert_eq!(search.go_prev().unwrap().line_idx, 0);
        Ok(())
    }

    #[test]
    fn test_clear_resets_forward() -> anyhow::Result<()> {
        let (_f, reader) = make_reader(&["foo"]);
        let all: Vec<usize> = (0..reader.line_count()).collect();
        let mut search = Search::new();
        search.search("foo", all.iter().copied(), raw_text(&reader))?;
        search.set_forward(false);
        assert!(!search.is_forward());
        search.clear();
        assert!(search.is_forward());
        Ok(())
    }

    #[test]
    fn test_set_position_for_search_forward() -> anyhow::Result<()> {
        // results at lines 0, 2, 4
        let (_f, reader) = make_reader(&["foo", "bar", "foo", "bar", "foo"]);
        let all: Vec<usize> = (0..reader.line_count()).collect();
        let mut search = Search::new();
        search.search("foo", all.iter().copied(), raw_text(&reader))?;

        // From line 1: first result at/after line 1 → line 2.
        search.set_position_for_search(1, true);
        assert_eq!(search.next_match().unwrap().line_idx, 2);

        // From line 4 (at a result): current line counts → line 4.
        search.set_position_for_search(4, true);
        assert_eq!(search.next_match().unwrap().line_idx, 4);

        // From line 0 (at a result): current line counts → line 0.
        search.set_position_for_search(0, true);
        assert_eq!(search.next_match().unwrap().line_idx, 0);
        Ok(())
    }

    #[test]
    fn test_set_position_for_search_backward() -> anyhow::Result<()> {
        // results at lines 0, 2, 4
        let (_f, reader) = make_reader(&["foo", "bar", "foo", "bar", "foo"]);
        let all: Vec<usize> = (0..reader.line_count()).collect();
        let mut search = Search::new();
        search.search("foo", all.iter().copied(), raw_text(&reader))?;

        // From line 3: last result strictly before line 3 → line 2.
        search.set_position_for_search(3, false);
        assert_eq!(search.previous_match().unwrap().line_idx, 2);

        // From line 0 (first result): no result before line 0 → wraps to line 4.
        search.set_position_for_search(0, false);
        assert_eq!(search.previous_match().unwrap().line_idx, 4);

        // From line 4 (last result): last result strictly before line 4 → line 2.
        search.set_position_for_search(4, false);
        assert_eq!(search.previous_match().unwrap().line_idx, 2);
        Ok(())
    }

    #[test]
    fn test_get_current_occurrence_index() -> anyhow::Result<()> {
        let (_f, reader) = make_reader(&["foo foo", "foo"]);
        let all: Vec<usize> = (0..reader.line_count()).collect();
        let mut search = Search::new();
        search.search("foo", all.iter().copied(), raw_text(&reader))?;
        assert_eq!(search.get_current_occurrence_index(), 0);
        search.next_match();
        assert_eq!(search.get_current_occurrence_index(), 1);
        search.next_match(); // moves to line 1, occ 0
        assert_eq!(search.get_current_occurrence_index(), 0);
        Ok(())
    }

    #[test]
    fn test_get_current_occurrence_for_line() -> anyhow::Result<()> {
        let (_f, reader) = make_reader(&["foo foo", "foo"]);
        let all: Vec<usize> = (0..reader.line_count()).collect();
        let mut search = Search::new();
        search.search("foo", all.iter().copied(), raw_text(&reader))?;
        // current is line 0, occ 0
        assert_eq!(search.get_current_occurrence_for_line(0), Some(0));
        assert_eq!(search.get_current_occurrence_for_line(1), None);
        search.next_match(); // line 0, occ 1
        assert_eq!(search.get_current_occurrence_for_line(0), Some(1));
        assert_eq!(search.get_current_occurrence_for_line(1), None);
        search.next_match(); // line 1, occ 0
        assert_eq!(search.get_current_occurrence_for_line(0), None);
        assert_eq!(search.get_current_occurrence_for_line(1), Some(0));
        Ok(())
    }

    #[test]
    fn test_get_current_occurrence_for_line_empty() {
        let search = Search::new();
        assert_eq!(search.get_current_occurrence_for_line(0), None);
    }
}