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
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
//! Filter pipeline: pattern matching, visibility computation, and span rendering.
//!
//! [`FilterManager`] evaluates enabled `FilterDef`s against log lines in
//! parallel via rayon. Literal patterns use Aho-Corasick; patterns with regex
//! metacharacters fall back to the `regex` crate. [`render_line`] flattens
//! overlapping styled spans into a ratatui [`Line`].
//!
//! ## Key types
//!
//! - `Filter` trait: `fn evaluate(&self, line: &[u8], collector: &mut MatchCollector) -> FilterDecision`
//! - `FilterDecision`: `Include | Exclude | Neutral`
//! - `SubstringFilter`: Aho-Corasick for literal patterns (no regex metacharacters).
//! - `RegexFilter`: `regex` crate fallback for patterns with metacharacters.
//! - `build_filter(pattern, decision, match_only, style_id)`: dispatches to the
//!   correct implementation.
//! - `FilterManager::compute_visible(&FileReader) -> Vec<usize>`: parallel
//!   evaluation via `rayon::into_par_iter()`, returns ascending sorted indices.
//! - `FilterManager::is_visible(&[u8]) -> bool`: if any enabled Include filter
//!   exists, the line must match one; any Exclude match hides the line regardless.
//! - `MatchCollector`: accumulates `MatchSpan { start, end, style: StyleId,
//!   priority }` for a single line.
//! - `StyleId` (`u8`): index into the 256-slot styles array. `SEARCH_STYLE_ID
//!   = u8::MAX = 255` reserved for search highlights; `CURRENT_SEARCH_STYLE_ID
//!   = u8::MAX - 1 = 254` reserved for the active search occurrence.
//!
//! ## render_line sweep algorithm
//!
//! Spans are sorted by start position. All start/end byte positions are
//! collected as boundary points, sorted and deduplicated. For each interval
//! `[seg_s, seg_e)`, active spans are composed: `fg` comes from the active span
//! with the highest priority that has `fg` set; `bg` comes from the active span
//! with the highest priority that has `bg` set. This allows two filters that set
//! different attributes on the same segment (e.g. one sets `fg`, another sets
//! `bg`) to both apply. Adjacent intervals with the same composed style are merged.

use aho_corasick::AhoCorasick;
use ratatui::text::{Line, Span};
use regex::Regex;

/// Index into the styles array passed to `render_line`.
pub type StyleId = u8;

/// Reserved StyleId for search highlights (always at the end of the styles array).
pub const SEARCH_STYLE_ID: StyleId = u8::MAX;

/// Reserved StyleId for the *current* search occurrence (one slot below SEARCH_STYLE_ID).
pub const CURRENT_SEARCH_STYLE_ID: StyleId = u8::MAX - 1;

#[derive(Debug, Clone, Copy, PartialEq)]
pub enum FilterDecision {
    /// This line should be shown (matched an include filter).
    Include,
    /// This line should be hidden (matched an exclude filter).
    Exclude,
    /// This filter has no opinion about this line.
    Neutral,
}

pub trait Filter: Send + Sync {
    fn evaluate(&self, line: &[u8], collector: &mut MatchCollector) -> FilterDecision;
}

/// Render a line using the collected match spans and a styles array.
///
/// For each boundary interval, `fg` and `bg` are composed independently: each attribute
/// is taken from the highest-priority active span that has that attribute set.  This lets
/// a level filter (fg only) and a text filter (bg only) both apply to the same segment.
pub fn render_line<'a>(col: &MatchCollector, styles: &[ratatui::style::Style]) -> Line<'a> {
    if col.spans.is_empty() {
        let text = std::str::from_utf8(col.line).unwrap_or("").to_string();
        return Line::from(text);
    }

    let line_len = col.line.len();

    // Filter to valid spans and sort by start for the sweep activation step.
    let mut valid: Vec<(usize, usize, u32, StyleId)> = col
        .spans
        .iter()
        .filter(|s| s.start < s.end && s.end <= line_len)
        .map(|s| (s.start, s.end, s.priority, s.style))
        .collect();

    if valid.is_empty() {
        let text = std::str::from_utf8(col.line).unwrap_or("").to_string();
        return Line::from(text);
    }

    valid.sort_unstable_by_key(|&(start, _, _, _)| start);

    // Collect unique boundary points from valid spans.
    let mut boundaries: Vec<usize> = Vec::with_capacity(valid.len() * 2 + 2);
    boundaries.push(0);
    boundaries.push(line_len);
    for &(start, end, _, _) in &valid {
        boundaries.push(start);
        boundaries.push(end);
    }
    boundaries.sort_unstable();
    boundaries.dedup();

    // Sweep-line: for each boundary interval [seg_s, seg_e) maintain the list of
    // active spans and compose fg/bg independently (highest priority wins for each).
    let mut active: Vec<(u32, usize, StyleId)> = Vec::new(); // (priority, end, style_id)
    let mut span_idx = 0usize;

    // Merge adjacent intervals with the same composed style to minimise Span count.
    let mut events: Vec<(usize, usize, ratatui::style::Style)> =
        Vec::with_capacity(boundaries.len());
    for w in boundaries.windows(2) {
        let (seg_s, seg_e) = (w[0], w[1]);
        if seg_s >= seg_e {
            continue;
        }
        // Activate all spans whose start ≤ seg_s.
        while span_idx < valid.len() && valid[span_idx].0 <= seg_s {
            let (_, end, priority, style) = valid[span_idx];
            active.push((priority, end, style));
            span_idx += 1;
        }
        // Remove spans that no longer cover this interval.
        active.retain(|&(_, end, _)| end > seg_s);
        if active.is_empty() {
            continue;
        }
        let composed = compose_segment_style(&active, styles);
        if composed.fg.is_none() && composed.bg.is_none() {
            continue;
        }
        if let Some(last) = events.last_mut()
            && last.1 == seg_s
            && last.2 == composed
        {
            last.1 = seg_e;
        } else {
            events.push((seg_s, seg_e, composed));
        }
    }

    // Build ratatui spans from events, filling unstyled gaps with raw text.
    let mut spans: Vec<Span<'a>> = Vec::new();
    let mut pos = 0usize;

    for (start, end, style) in events {
        if start > pos {
            let text = std::str::from_utf8(&col.line[pos..start])
                .unwrap_or("")
                .to_string();
            if !text.is_empty() {
                spans.push(Span::raw(text));
            }
        }
        if end > start {
            let text = std::str::from_utf8(&col.line[start..end])
                .unwrap_or("")
                .to_string();
            if !text.is_empty() {
                spans.push(Span::styled(text, style));
            }
        }
        pos = end.max(pos);
    }

    if pos < line_len {
        let text = std::str::from_utf8(&col.line[pos..])
            .unwrap_or("")
            .to_string();
        if !text.is_empty() {
            spans.push(Span::raw(text));
        }
    }

    Line::from(spans)
}

/// Compose a single [`ratatui::style::Style`] from a set of active spans.
///
/// `fg` is taken from the span with the highest priority that has `fg` set;
/// `bg` from the span with the highest priority that has `bg` set.  Spans that
/// set neither attribute are ignored for that attribute's composition.
fn compose_segment_style(
    active: &[(u32, usize, StyleId)],
    styles: &[ratatui::style::Style],
) -> ratatui::style::Style {
    let mut best_fg: Option<(u32, ratatui::style::Color)> = None;
    let mut best_bg: Option<(u32, ratatui::style::Color)> = None;

    for &(priority, _, style_id) in active {
        let style = styles.get(style_id as usize).copied().unwrap_or_default();
        if let Some(fg) = style.fg
            && best_fg.is_none_or(|(p, _)| priority > p)
        {
            best_fg = Some((priority, fg));
        }
        if let Some(bg) = style.bg
            && best_bg.is_none_or(|(p, _)| priority > p)
        {
            best_bg = Some((priority, bg));
        }
    }

    let mut composed = ratatui::style::Style::default();
    if let Some((_, fg)) = best_fg {
        composed = composed.fg(fg);
    }
    if let Some((_, bg)) = best_bg {
        composed = composed.bg(bg);
    }
    composed
}

#[derive(Debug, Clone)]
pub struct MatchSpan {
    pub start: usize,
    pub end: usize,
    pub style: StyleId,
    pub priority: u32, // higher = stronger
}

pub struct MatchCollector<'a> {
    pub line: &'a [u8],
    pub spans: Vec<MatchSpan>,
    current_priority: u32,
}

impl<'a> MatchCollector<'a> {
    pub fn new(line: &'a [u8]) -> Self {
        Self {
            line,
            spans: Vec::with_capacity(8),
            current_priority: 0,
        }
    }

    pub fn with_priority(&mut self, priority: u32) -> &mut Self {
        self.current_priority = priority;
        self
    }

    pub fn push(&mut self, start: usize, end: usize, style: StyleId) {
        self.spans.push(MatchSpan {
            start,
            end,
            style,
            priority: self.current_priority,
        });
    }
}

/// Returns true if `pattern` contains any regex metacharacters.
fn is_regex_pattern(pattern: &str) -> bool {
    pattern.chars().any(|c| {
        matches!(
            c,
            '.' | '+' | '*' | '?' | '[' | ']' | '(' | ')' | '{' | '}' | '\\' | '^' | '$' | '|'
        )
    })
}

/// Include/exclude filter using Aho-Corasick for efficient literal substring matching.
pub struct SubstringFilter {
    ac: AhoCorasick,
    decision: FilterDecision,
    style_id: StyleId,
    /// If true, only colour the matched spans rather than the whole line.
    match_only: bool,
}

impl SubstringFilter {
    pub fn new(
        pattern: &str,
        decision: FilterDecision,
        match_only: bool,
        style_id: StyleId,
    ) -> Option<Self> {
        let ac = AhoCorasick::builder()
            .ascii_case_insensitive(false)
            .build([pattern])
            .inspect_err(|e| {
                tracing::error!("Failed to build Aho-Corasick automaton: {}", e);
            })
            .ok()?;
        Some(SubstringFilter {
            ac,
            decision,
            style_id,
            match_only,
        })
    }
}

impl Filter for SubstringFilter {
    fn evaluate(&self, line: &[u8], collector: &mut MatchCollector) -> FilterDecision {
        let mut found = false;
        for mat in self.ac.find_iter(line) {
            found = true;
            if matches!(self.decision, FilterDecision::Include) && self.match_only {
                collector.push(mat.start(), mat.end(), self.style_id);
            }
        }
        if found {
            // For Include filters, add a full-line span when not match_only
            if matches!(self.decision, FilterDecision::Include) && !self.match_only {
                collector.push(0, line.len(), self.style_id);
            }
            self.decision
        } else {
            FilterDecision::Neutral
        }
    }
}

/// Include/exclude filter using Regex for pattern matching.
pub struct RegexFilter {
    re: Regex,
    decision: FilterDecision,
    style_id: StyleId,
    match_only: bool,
}

impl RegexFilter {
    /// Returns `None` if the pattern is not a valid regex.
    pub fn new(
        pattern: &str,
        decision: FilterDecision,
        match_only: bool,
        style_id: StyleId,
    ) -> Option<Self> {
        Regex::new(pattern).ok().map(|re| RegexFilter {
            re,
            decision,
            style_id,
            match_only,
        })
    }
}

impl Filter for RegexFilter {
    fn evaluate(&self, line: &[u8], collector: &mut MatchCollector) -> FilterDecision {
        let text = match std::str::from_utf8(line) {
            Ok(s) => s,
            Err(_) => return FilterDecision::Neutral,
        };
        let mut found = false;
        for mat in self.re.find_iter(text) {
            found = true;
            if matches!(self.decision, FilterDecision::Include) && self.match_only {
                collector.push(mat.start(), mat.end(), self.style_id);
            }
        }
        if found {
            if matches!(self.decision, FilterDecision::Include) && !self.match_only {
                collector.push(0, line.len(), self.style_id);
            }
            self.decision
        } else {
            FilterDecision::Neutral
        }
    }
}

/// Builds a `Box<dyn Filter>` from a pattern, choosing Aho-Corasick vs Regex automatically.
pub fn build_filter(
    pattern: &str,
    decision: FilterDecision,
    match_only: bool,
    style_id: StyleId,
) -> Option<Box<dyn Filter>> {
    if is_regex_pattern(pattern) {
        RegexFilter::new(pattern, decision, match_only, style_id)
            .map(|f| Box::new(f) as Box<dyn Filter>)
    } else {
        SubstringFilter::new(pattern, decision, match_only, style_id)
            .map(|f| Box::new(f) as Box<dyn Filter>)
    }
}

/// Orchestrates a layered pipeline of filters and provides parallel visibility computation.
pub struct FilterManager {
    filters: Vec<Box<dyn Filter>>,
    /// True if any enabled Include filter exists.
    has_include_filters: bool,
}

impl FilterManager {
    pub fn new(filters: Vec<Box<dyn Filter>>, has_include_filters: bool) -> Self {
        FilterManager {
            filters,
            has_include_filters,
        }
    }

    pub fn empty() -> Self {
        FilterManager {
            filters: Vec::new(),
            has_include_filters: false,
        }
    }

    /// Returns true if any enabled Include filter exists.
    pub fn has_include(&self) -> bool {
        self.has_include_filters
    }

    /// Evaluate text filters and return the first-match decision.
    ///
    /// Returns `Include` or `Exclude` on the first match; `Neutral` if no filter matched.
    /// This is the same as `is_visible` but returns the decision rather than a bool,
    /// allowing callers to combine it with field and date filter results.
    pub fn evaluate_text(&self, line: &[u8]) -> FilterDecision {
        let mut dummy = MatchCollector::new(line);
        for filter in &self.filters {
            match filter.evaluate(line, &mut dummy) {
                d @ (FilterDecision::Include | FilterDecision::Exclude) => return d,
                FilterDecision::Neutral => {}
            }
        }
        FilterDecision::Neutral
    }

    /// Returns true if `line` should be visible under the current filter set.
    ///
    /// Filters are evaluated top-to-bottom (index 0 = highest precedence).
    /// The first filter that matches (Include or Exclude) determines the outcome.
    /// If no filter matches, the line is visible only when there are no Include filters.
    pub fn is_visible(&self, line: &[u8]) -> bool {
        let mut dummy = MatchCollector::new(line);
        for filter in &self.filters {
            match filter.evaluate(line, &mut dummy) {
                FilterDecision::Include => return true,
                FilterDecision::Exclude => return false,
                FilterDecision::Neutral => {}
            }
        }
        !self.has_include_filters
    }

    /// Run all filters on `line` and collect styling spans for rendering.
    pub fn evaluate_line<'a>(&self, line: &'a [u8]) -> MatchCollector<'a> {
        let mut collector = MatchCollector::new(line);
        for filter in &self.filters {
            filter.evaluate(line, &mut collector);
        }
        collector
    }

    /// Run all filters and add styling spans into an existing collector.
    /// Use this when you need to pre-seed the collector with other spans
    /// (e.g. process colors) before filter spans are added.
    pub fn evaluate_into(&self, collector: &mut MatchCollector<'_>) {
        let line = collector.line;
        for filter in &self.filters {
            filter.evaluate(line, collector);
        }
    }

    /// Returns the number of compiled text filters in this manager.
    pub fn filter_count(&self) -> usize {
        self.filters.len()
    }

    /// Evaluate each filter independently on `line` and increment the corresponding
    /// counter in `counts` (indexed parallel to the internal filter list).
    ///
    /// Unlike `is_visible`, this does not short-circuit: every filter is evaluated
    /// so that per-filter match counts accumulate correctly across lines.
    pub fn count_line_matches(&self, line: &[u8], counts: &[std::sync::atomic::AtomicUsize]) {
        for (i, filter) in self.filters.iter().enumerate() {
            let mut dummy = MatchCollector::new(line);
            if filter.evaluate(line, &mut dummy) != FilterDecision::Neutral
                && let Some(c) = counts.get(i)
            {
                c.fetch_add(1, std::sync::atomic::Ordering::Relaxed);
            }
        }
    }

    /// Compute visible line indices from a `FileReader` using Rayon parallel processing.
    /// The returned indices are in ascending order.
    pub fn compute_visible(&self, reader: &crate::file_reader::FileReader) -> Vec<usize> {
        use rayon::prelude::*;
        let count = reader.line_count();
        let has_include = self.has_include_filters;
        let filters = &self.filters;

        let visible: Vec<usize> = (0..count)
            .into_par_iter()
            .filter(|&idx| {
                let line = reader.get_line(idx);
                let mut dummy = MatchCollector::new(line);
                for filter in filters.iter() {
                    match filter.evaluate(line, &mut dummy) {
                        FilterDecision::Include => return true,
                        FilterDecision::Exclude => return false,
                        FilterDecision::Neutral => {}
                    }
                }
                !has_include
            })
            .collect();

        visible
    }
}

#[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)
    }

    #[test]
    fn test_substring_filter_include() {
        let line = b"ERROR: connection refused";
        let f = SubstringFilter::new("ERROR", FilterDecision::Include, false, 0).unwrap();
        let mut col = MatchCollector::new(line);
        assert_eq!(f.evaluate(line, &mut col), FilterDecision::Include);

        let no_match = b"INFO: all good";
        let mut col2 = MatchCollector::new(no_match);
        assert_eq!(f.evaluate(no_match, &mut col2), FilterDecision::Neutral);
    }

    #[test]
    fn test_substring_filter_exclude() {
        let line = b"DEBUG: verbose output";
        let f = SubstringFilter::new("DEBUG", FilterDecision::Exclude, false, 0).unwrap();
        let mut col = MatchCollector::new(line);
        assert_eq!(f.evaluate(line, &mut col), FilterDecision::Exclude);

        let no_match = b"INFO: important";
        let mut col2 = MatchCollector::new(no_match);
        assert_eq!(f.evaluate(no_match, &mut col2), FilterDecision::Neutral);
    }

    #[test]
    fn test_substring_filter_match_only_spans() {
        let line = b"ERROR: something went wrong";
        let f = SubstringFilter::new("ERROR", FilterDecision::Include, true, 1).unwrap();
        let mut col = MatchCollector::new(line);
        f.evaluate(line, &mut col);
        assert_eq!(col.spans.len(), 1);
        assert_eq!(col.spans[0].start, 0);
        assert_eq!(col.spans[0].end, 5);
        assert_eq!(col.spans[0].style, 1);
    }

    #[test]
    fn test_regex_filter_include() {
        let line = b"GET /api/users 200 OK";
        let f = RegexFilter::new(r"\d{3}", FilterDecision::Include, true, 0).unwrap();
        let mut col = MatchCollector::new(line);
        assert_eq!(f.evaluate(line, &mut col), FilterDecision::Include);
        // Should have a span covering "200"
        assert_eq!(col.spans.len(), 1);
        assert_eq!(&line[col.spans[0].start..col.spans[0].end], b"200");
    }

    #[test]
    fn test_regex_filter_invalid_pattern() {
        assert!(RegexFilter::new("[invalid", FilterDecision::Include, false, 0).is_none());
    }

    #[test]
    fn test_build_filter_selects_substring_for_literal() {
        // Pure literal — should work (no panic)
        let f = build_filter("error", FilterDecision::Include, false, 0);
        assert!(f.is_some());
    }

    #[test]
    fn test_build_filter_selects_regex_for_pattern() {
        // Has regex chars — should use RegexFilter
        let f = build_filter(r"error\d+", FilterDecision::Include, false, 0);
        assert!(f.is_some());
    }

    #[test]
    fn test_filter_manager_no_filters_all_visible() {
        let fm = FilterManager::empty();
        assert!(fm.is_visible(b"anything"));
        assert!(fm.is_visible(b""));
    }

    #[test]
    fn test_filter_manager_include_filter() {
        let f = SubstringFilter::new("ERROR", FilterDecision::Include, false, 0).unwrap();
        let fm = FilterManager::new(vec![Box::new(f)], true);

        assert!(fm.is_visible(b"ERROR: bad things"));
        assert!(!fm.is_visible(b"INFO: all good"));
    }

    #[test]
    fn test_filter_manager_exclude_filter() {
        let f = SubstringFilter::new("DEBUG", FilterDecision::Exclude, false, 0).unwrap();
        let fm = FilterManager::new(vec![Box::new(f)], false);

        assert!(fm.is_visible(b"INFO: something"));
        assert!(!fm.is_visible(b"DEBUG: verbose"));
    }

    #[test]
    fn test_filter_manager_include_then_exclude() {
        // Exclude "minor" at top (higher precedence), Include "ERROR" below.
        // First-match-wins: a line matching the top Exclude is hidden even if
        // a lower Include filter also matches it.
        let exc = SubstringFilter::new("minor", FilterDecision::Exclude, false, 1).unwrap();
        let inc = SubstringFilter::new("ERROR", FilterDecision::Include, false, 0).unwrap();
        let fm = FilterManager::new(vec![Box::new(exc), Box::new(inc)], true);

        assert!(fm.is_visible(b"ERROR: critical failure")); // no exclude match → include matches
        assert!(!fm.is_visible(b"ERROR: minor issue")); // exclude at top wins
        assert!(!fm.is_visible(b"INFO: unrelated")); // no match at all → has include filters → hidden
    }

    #[test]
    fn test_filter_manager_compute_visible() {
        let (_f, reader) = make_reader(&[
            "ERROR: bad",
            "INFO: good",
            "ERROR: also bad",
            "DEBUG: verbose",
        ]);

        let inc = SubstringFilter::new("ERROR", FilterDecision::Include, false, 0).unwrap();
        let fm = FilterManager::new(vec![Box::new(inc)], true);
        let visible = fm.compute_visible(&reader);

        assert_eq!(visible, vec![0, 2]);
    }

    #[test]
    fn test_filter_manager_compute_visible_exclude() {
        let (_f, reader) = make_reader(&["ERROR: bad", "DEBUG: verbose", "INFO: good"]);

        let exc = SubstringFilter::new("DEBUG", FilterDecision::Exclude, false, 0).unwrap();
        let fm = FilterManager::new(vec![Box::new(exc)], false);
        let visible = fm.compute_visible(&reader);

        assert_eq!(visible, vec![0, 2]);
    }

    #[test]
    fn test_render_line_no_spans() {
        let line = b"plain text";
        let col = MatchCollector::new(line);
        let styles: Vec<ratatui::style::Style> = vec![];
        let rendered = render_line(&col, &styles);
        let text: String = rendered.spans.iter().map(|s| s.content.as_ref()).collect();
        assert_eq!(text, "plain text");
    }

    #[test]
    fn test_render_line_with_span() {
        let line = b"hello world";
        let mut col = MatchCollector::new(line);
        let style = ratatui::style::Style::default().fg(ratatui::style::Color::Red);
        let styles = vec![style];
        col.push(6, 11, 0); // "world"
        let rendered = render_line(&col, &styles);
        // Should have "hello " unstyled and "world" styled
        assert!(rendered.spans.len() >= 2);
    }

    #[test]
    fn test_evaluate_line_collects_spans() {
        let line = b"ERROR: connection refused to host";
        let f = SubstringFilter::new("ERROR", FilterDecision::Include, true, 0).unwrap();
        let fm = FilterManager::new(vec![Box::new(f)], true);
        let col = fm.evaluate_line(line);
        assert!(!col.spans.is_empty());
        assert_eq!(&line[col.spans[0].start..col.spans[0].end], b"ERROR");
    }

    #[test]
    fn test_render_line_overlapping_spans_priority() {
        // Two spans cover the same region. Higher priority must win.
        let line = b"hello world";
        let style_lo = ratatui::style::Style::default().fg(ratatui::style::Color::Blue);
        let style_hi = ratatui::style::Style::default().fg(ratatui::style::Color::Red);
        let styles = vec![style_lo, style_hi];

        let mut col = MatchCollector::new(line);
        col.with_priority(0);
        col.push(0, 5, 0); // "hello" — low priority, style 0 (Blue)
        col.with_priority(10);
        col.push(0, 5, 1); // "hello" — high priority, style 1 (Red)

        let rendered = render_line(&col, &styles);
        let hello_span = rendered
            .spans
            .iter()
            .find(|s| s.content.as_ref() == "hello");
        assert!(hello_span.is_some());
        assert_eq!(
            hello_span.unwrap().style.fg,
            Some(ratatui::style::Color::Red)
        );
    }

    #[test]
    fn test_render_line_adjacent_same_style_merged() {
        // Two adjacent spans with the same style should be merged into one.
        let line = b"abcdef";
        let style = ratatui::style::Style::default().fg(ratatui::style::Color::Green);
        let styles = vec![style];

        let mut col = MatchCollector::new(line);
        col.push(0, 3, 0); // "abc"
        col.push(3, 6, 0); // "def" — same style, adjacent

        let rendered = render_line(&col, &styles);
        let styled: Vec<_> = rendered
            .spans
            .iter()
            .filter(|s| s.style.fg.is_some())
            .collect();
        assert_eq!(styled.len(), 1);
        assert_eq!(styled[0].content.as_ref(), "abcdef");
    }

    #[test]
    fn test_render_line_composes_fg_and_bg_from_different_spans() {
        // One span sets fg, another sets bg on the same segment — both must apply.
        let line = b"hello world";
        let style_fg = ratatui::style::Style::default().fg(ratatui::style::Color::Yellow);
        let style_bg = ratatui::style::Style::default().bg(ratatui::style::Color::DarkGray);
        let styles = vec![style_fg, style_bg];

        let mut col = MatchCollector::new(line);
        col.with_priority(0);
        col.push(0, 5, 0); // "hello" — fg=Yellow
        col.with_priority(0);
        col.push(0, 5, 1); // "hello" — bg=DarkGray

        let rendered = render_line(&col, &styles);
        let hello_span = rendered
            .spans
            .iter()
            .find(|s| s.content.as_ref() == "hello");
        assert!(hello_span.is_some());
        let span = hello_span.unwrap();
        assert_eq!(span.style.fg, Some(ratatui::style::Color::Yellow));
        assert_eq!(span.style.bg, Some(ratatui::style::Color::DarkGray));
    }

    #[test]
    fn test_render_line_higher_priority_fg_wins_over_lower() {
        // Two spans both set fg; the higher-priority one must win for fg.
        let line = b"hello";
        let style_lo = ratatui::style::Style::default().fg(ratatui::style::Color::Blue);
        let style_hi = ratatui::style::Style::default().fg(ratatui::style::Color::Red);
        let styles = vec![style_lo, style_hi];

        let mut col = MatchCollector::new(line);
        col.with_priority(0);
        col.push(0, 5, 0); // low priority, fg=Blue
        col.with_priority(10);
        col.push(0, 5, 1); // high priority, fg=Red

        let rendered = render_line(&col, &styles);
        let span = rendered
            .spans
            .iter()
            .find(|s| s.content.as_ref() == "hello");
        assert!(span.is_some());
        assert_eq!(span.unwrap().style.fg, Some(ratatui::style::Color::Red));
    }

    #[test]
    fn test_render_line_higher_priority_bg_wins_independent_of_fg() {
        // High-priority span sets bg; low-priority span sets fg — both apply independently.
        let line = b"hello";
        let style_lo = ratatui::style::Style::default().fg(ratatui::style::Color::Cyan);
        let style_hi = ratatui::style::Style::default().bg(ratatui::style::Color::Red);
        let styles = vec![style_lo, style_hi];

        let mut col = MatchCollector::new(line);
        col.with_priority(0);
        col.push(0, 5, 0); // low priority, fg=Cyan
        col.with_priority(10);
        col.push(0, 5, 1); // high priority, bg=Red

        let rendered = render_line(&col, &styles);
        let span = rendered
            .spans
            .iter()
            .find(|s| s.content.as_ref() == "hello");
        assert!(span.is_some());
        assert_eq!(span.unwrap().style.fg, Some(ratatui::style::Color::Cyan));
        assert_eq!(span.unwrap().style.bg, Some(ratatui::style::Color::Red));
    }

    #[test]
    fn test_filter_count_returns_number_of_compiled_filters() {
        let f1 = SubstringFilter::new("ERROR", FilterDecision::Include, false, 0).unwrap();
        let f2 = SubstringFilter::new("DEBUG", FilterDecision::Exclude, false, 1).unwrap();
        let fm = FilterManager::new(vec![Box::new(f1), Box::new(f2)], true);
        assert_eq!(fm.filter_count(), 2);
    }

    #[test]
    fn test_filter_count_empty() {
        let fm = FilterManager::empty();
        assert_eq!(fm.filter_count(), 0);
    }

    #[test]
    fn test_count_line_matches_independent_no_short_circuit() {
        // Both filters match — counts must increment independently even though
        // pipeline evaluation would short-circuit after the first match.
        let line = b"ERROR DEBUG both";
        let f1 = SubstringFilter::new("ERROR", FilterDecision::Include, false, 0).unwrap();
        let f2 = SubstringFilter::new("DEBUG", FilterDecision::Exclude, false, 1).unwrap();
        let fm = FilterManager::new(vec![Box::new(f1), Box::new(f2)], true);

        let counts: Vec<std::sync::atomic::AtomicUsize> = (0..2)
            .map(|_| std::sync::atomic::AtomicUsize::new(0))
            .collect();
        fm.count_line_matches(line, &counts);

        assert_eq!(counts[0].load(std::sync::atomic::Ordering::Relaxed), 1);
        assert_eq!(counts[1].load(std::sync::atomic::Ordering::Relaxed), 1);
    }

    #[test]
    fn test_count_line_matches_only_matching_filters_increment() {
        let line = b"INFO: all good";
        let f_error = SubstringFilter::new("ERROR", FilterDecision::Include, false, 0).unwrap();
        let f_info = SubstringFilter::new("INFO", FilterDecision::Include, false, 1).unwrap();
        let fm = FilterManager::new(vec![Box::new(f_error), Box::new(f_info)], true);

        let counts: Vec<std::sync::atomic::AtomicUsize> = (0..2)
            .map(|_| std::sync::atomic::AtomicUsize::new(0))
            .collect();
        fm.count_line_matches(line, &counts);

        assert_eq!(counts[0].load(std::sync::atomic::Ordering::Relaxed), 0);
        assert_eq!(counts[1].load(std::sync::atomic::Ordering::Relaxed), 1);
    }

    #[test]
    fn test_count_line_matches_accumulates_across_lines() {
        let f = SubstringFilter::new("ERROR", FilterDecision::Include, false, 0).unwrap();
        let fm = FilterManager::new(vec![Box::new(f)], true);

        let counts: Vec<std::sync::atomic::AtomicUsize> = (0..1)
            .map(|_| std::sync::atomic::AtomicUsize::new(0))
            .collect();
        fm.count_line_matches(b"ERROR: first", &counts);
        fm.count_line_matches(b"INFO: skip", &counts);
        fm.count_line_matches(b"ERROR: second", &counts);

        assert_eq!(counts[0].load(std::sync::atomic::Ordering::Relaxed), 2);
    }
}