revue 2.71.1

A Vue-style TUI framework for Rust with CSS styling
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
//! Log Viewer widget implementation

use super::entry::{LogEntry, SearchMatch};
use super::filter::LogFilter;
use super::parser::LogParser;
use super::types::LogLevel;
use crate::event::Key;
use crate::render::{Cell, Modifier};
use crate::style::Color;
use crate::utils::{char_width, display_width, truncate_to_width};
use crate::widget::theme::{DARK_GRAY, DISABLED_FG, SUBTLE_GRAY};
use crate::widget::traits::{RenderContext, View, WidgetProps};
use crate::{impl_props_builders, impl_styled_view};

/// Advanced Log Viewer widget
pub struct LogViewer {
    /// All log entries
    entries: Vec<LogEntry>,
    /// Current scroll position
    scroll: usize,
    /// Selected entry index (in filtered view)
    selected: usize,
    /// Current filter
    filter: LogFilter,
    /// Search query (regex pattern)
    search_query: String,
    /// Search matches
    search_matches: Vec<SearchMatch>,
    /// Current search match index
    search_index: usize,
    /// Live tail mode (auto-follow new entries)
    tail_mode: bool,
    /// Show line numbers
    show_line_numbers: bool,
    /// Show timestamps
    show_timestamps: bool,
    /// Show log levels
    show_levels: bool,
    /// Show source/logger
    show_source: bool,
    /// Word wrap enabled
    wrap: bool,
    /// Log parser configuration
    parser: LogParser,
    /// Maximum entries (0 = unlimited)
    max_entries: usize,
    /// Background color
    bg: Option<Color>,
    /// Line number color
    line_number_fg: Color,
    /// Timestamp color
    timestamp_fg: Color,
    /// Source color
    source_fg: Color,
    /// Search highlight color
    search_highlight_bg: Color,
    /// Bookmark indicator color
    bookmark_fg: Color,
    /// Selected line background
    selected_bg: Color,
    /// Widget props
    props: WidgetProps,
}

impl LogViewer {
    /// Create a new log viewer
    pub fn new() -> Self {
        Self {
            entries: Vec::new(),
            scroll: 0,
            selected: 0,
            filter: LogFilter::new(),
            search_query: String::new(),
            search_matches: Vec::new(),
            search_index: 0,
            tail_mode: true,
            show_line_numbers: true,
            show_timestamps: true,
            show_levels: true,
            show_source: true,
            wrap: false,
            parser: LogParser::new(),
            max_entries: 10000,
            bg: None,
            line_number_fg: DISABLED_FG,
            timestamp_fg: SUBTLE_GRAY,
            source_fg: Color::rgb(150, 120, 200),
            search_highlight_bg: Color::YELLOW,
            bookmark_fg: Color::rgb(255, 200, 50),
            selected_bg: Color::rgb(50, 50, 80),
            props: WidgetProps::new(),
        }
    }

    /// Load log content from string (parses each line)
    pub fn load(&mut self, content: &str) {
        self.entries.clear();
        for (i, line) in content.lines().enumerate() {
            if !line.is_empty() {
                let entry = self.parser.parse(line, i + 1);
                self.entries.push(entry);
            }
        }
        self.update_search();
        if self.tail_mode {
            self.scroll_to_bottom();
        }
    }

    /// Add a single log line
    pub fn push(&mut self, line: &str) {
        let line_number = self.entries.len() + 1;
        let entry = self.parser.parse(line, line_number);
        self.entries.push(entry);

        // Trim old entries if needed
        if self.max_entries > 0 && self.entries.len() > self.max_entries {
            let excess = self.entries.len() - self.max_entries;
            self.entries.drain(0..excess);
            self.scroll = self.scroll.saturating_sub(excess);
        }

        // Update search if active
        if !self.search_query.is_empty() {
            self.update_search();
        }

        // Auto-scroll in tail mode
        if self.tail_mode {
            self.scroll_to_bottom();
        }
    }

    /// Add a pre-built log entry
    pub fn push_entry(&mut self, entry: LogEntry) {
        self.entries.push(entry);

        if self.max_entries > 0 && self.entries.len() > self.max_entries {
            let excess = self.entries.len() - self.max_entries;
            self.entries.drain(0..excess);
            self.scroll = self.scroll.saturating_sub(excess);
        }

        if !self.search_query.is_empty() {
            self.update_search();
        }

        if self.tail_mode {
            self.scroll_to_bottom();
        }
    }

    /// Set filter
    pub fn filter(mut self, filter: LogFilter) -> Self {
        self.filter = filter;
        self
    }

    /// Set minimum log level filter
    pub fn min_level(mut self, level: LogLevel) -> Self {
        self.filter.min_level = Some(level);
        self
    }

    /// Set search query
    pub fn search(&mut self, query: &str) {
        self.search_query = query.to_string();
        self.search_index = 0;
        self.update_search();
    }

    /// Clear search
    pub fn clear_search(&mut self) {
        self.search_query.clear();
        self.search_matches.clear();
        self.search_index = 0;
    }

    /// Go to next search match
    pub fn next_match(&mut self) {
        if !self.search_matches.is_empty() {
            self.search_index = (self.search_index + 1) % self.search_matches.len();
            self.scroll_to_match(self.search_index);
        }
    }

    /// Go to previous search match
    pub fn prev_match(&mut self) {
        if !self.search_matches.is_empty() {
            self.search_index = if self.search_index == 0 {
                self.search_matches.len() - 1
            } else {
                self.search_index - 1
            };
            self.scroll_to_match(self.search_index);
        }
    }

    /// Scroll to specific search match
    fn scroll_to_match(&mut self, match_index: usize) {
        if let Some(m) = self.search_matches.get(match_index) {
            // Find position in filtered view
            let filtered: Vec<_> = self.filtered_entries().collect();
            for (view_idx, (entry_idx, _)) in filtered.iter().enumerate() {
                if *entry_idx == m.entry_index {
                    self.selected = view_idx;
                    self.ensure_visible(view_idx);
                    break;
                }
            }
        }
    }

    /// Update search matches
    fn update_search(&mut self) {
        self.search_matches.clear();

        if self.search_query.is_empty() {
            return;
        }

        let query_lower = self.search_query.to_lowercase();

        for (idx, entry) in self.entries.iter().enumerate() {
            let msg_lower = entry.message.to_lowercase();

            let mut start = 0;
            while let Some(pos) = msg_lower[start..].find(&query_lower) {
                let actual_start = start + pos;
                self.search_matches.push(SearchMatch {
                    entry_index: idx,
                    start: actual_start,
                    end: actual_start + self.search_query.len(),
                });
                start = actual_start + 1;
            }
        }
    }

    /// Enable/disable tail mode
    pub fn tail_mode(mut self, enable: bool) -> Self {
        self.tail_mode = enable;
        self
    }

    /// Toggle tail mode
    pub fn toggle_tail(&mut self) {
        self.tail_mode = !self.tail_mode;
        if self.tail_mode {
            self.scroll_to_bottom();
        }
    }

    /// Enable/disable line numbers
    pub fn show_line_numbers(mut self, show: bool) -> Self {
        self.show_line_numbers = show;
        self
    }

    /// Enable/disable timestamps
    pub fn show_timestamps(mut self, show: bool) -> Self {
        self.show_timestamps = show;
        self
    }

    /// Enable/disable log levels
    pub fn show_levels(mut self, show: bool) -> Self {
        self.show_levels = show;
        self
    }

    /// Enable/disable source
    pub fn show_source(mut self, show: bool) -> Self {
        self.show_source = show;
        self
    }

    /// Enable/disable word wrap
    pub fn wrap(mut self, enable: bool) -> Self {
        self.wrap = enable;
        self
    }

    /// Toggle word wrap
    pub fn toggle_wrap(&mut self) {
        self.wrap = !self.wrap;
    }

    /// Set parser configuration
    pub fn parser(mut self, parser: LogParser) -> Self {
        self.parser = parser;
        self
    }

    /// Set maximum entries
    pub fn max_entries(mut self, max: usize) -> Self {
        self.max_entries = max;
        self
    }

    /// Set background color
    pub fn bg(mut self, color: Color) -> Self {
        self.bg = Some(color);
        self
    }

    /// Bookmark selected entry
    pub fn toggle_bookmark(&mut self) {
        let entry_idx = {
            let filtered: Vec<_> = self.filtered_entries().collect();
            filtered.get(self.selected).map(|(idx, _)| *idx)
        };
        if let Some(idx) = entry_idx {
            if let Some(entry) = self.entries.get_mut(idx) {
                entry.toggle_bookmark();
            }
        }
    }

    /// Get all bookmarked entries
    pub fn bookmarked_entries(&self) -> Vec<&LogEntry> {
        self.entries.iter().filter(|e| e.bookmarked).collect()
    }

    /// Jump to next bookmark
    pub fn next_bookmark(&mut self) {
        let filtered: Vec<_> = self.filtered_entries().collect();
        let start = self.selected + 1;

        // Search from current position to end
        for i in start..filtered.len() {
            if let Some((entry_idx, _)) = filtered.get(i) {
                if self.entries[*entry_idx].bookmarked {
                    self.selected = i;
                    self.ensure_visible(i);
                    return;
                }
            }
        }

        // Wrap around to beginning
        for i in 0..start {
            if let Some((entry_idx, _)) = filtered.get(i) {
                if self.entries[*entry_idx].bookmarked {
                    self.selected = i;
                    self.ensure_visible(i);
                    return;
                }
            }
        }
    }

    /// Jump to previous bookmark
    pub fn prev_bookmark(&mut self) {
        let filtered: Vec<_> = self.filtered_entries().collect();

        // Search from current position to beginning
        for i in (0..self.selected).rev() {
            if let Some((entry_idx, _)) = filtered.get(i) {
                if self.entries[*entry_idx].bookmarked {
                    self.selected = i;
                    self.ensure_visible(i);
                    return;
                }
            }
        }

        // Wrap around to end
        for i in (self.selected..filtered.len()).rev() {
            if let Some((entry_idx, _)) = filtered.get(i) {
                if self.entries[*entry_idx].bookmarked {
                    self.selected = i;
                    self.ensure_visible(i);
                    return;
                }
            }
        }
    }

    /// Jump to timestamp
    pub fn jump_to_timestamp(&mut self, timestamp: i64) {
        let filtered: Vec<_> = self.filtered_entries().collect();

        let mut best_idx = 0;
        let mut best_diff = i64::MAX;

        for (i, (entry_idx, _)) in filtered.iter().enumerate() {
            if let Some(ts) = self.entries[*entry_idx].timestamp_value {
                let diff = (ts - timestamp).abs();
                if diff < best_diff {
                    best_diff = diff;
                    best_idx = i;
                }
            }
        }

        self.selected = best_idx;
        self.ensure_visible(best_idx);
    }

    /// Jump to line number
    pub fn jump_to_line(&mut self, line: usize) {
        let filtered: Vec<_> = self.filtered_entries().collect();

        for (i, (entry_idx, _)) in filtered.iter().enumerate() {
            if self.entries[*entry_idx].line_number >= line {
                self.selected = i;
                self.ensure_visible(i);
                return;
            }
        }
    }

    /// Get selected entry text (for copying)
    pub fn selected_text(&self) -> Option<String> {
        let filtered: Vec<_> = self.filtered_entries().collect();
        filtered.get(self.selected).map(|(idx, _)| {
            let entry = &self.entries[*idx];
            entry.raw.clone()
        })
    }

    /// Get selected entry
    pub fn selected_entry(&self) -> Option<&LogEntry> {
        let filtered: Vec<_> = self.filtered_entries().collect();
        filtered
            .get(self.selected)
            .map(|(idx, _)| &self.entries[*idx])
    }

    /// Export filtered entries as text
    pub fn export_filtered(&self) -> String {
        self.filtered_entries()
            .map(|(_, entry)| entry.raw.as_str())
            .collect::<Vec<_>>()
            .join("\n")
    }

    /// Export filtered entries with formatting
    pub fn export_formatted(&self) -> String {
        self.filtered_entries()
            .map(|(_, entry)| {
                let mut parts = Vec::new();

                if let Some(ref ts) = entry.timestamp {
                    parts.push(format!("[{}]", ts));
                }

                parts.push(format!("[{}]", entry.level.label()));

                if let Some(ref src) = entry.source {
                    parts.push(format!("[{}]", src));
                }

                parts.push(entry.message.clone());

                parts.join(" ")
            })
            .collect::<Vec<_>>()
            .join("\n")
    }

    /// Get filtered entries iterator
    fn filtered_entries(&self) -> impl Iterator<Item = (usize, &LogEntry)> {
        self.entries
            .iter()
            .enumerate()
            .filter(|(_, entry)| self.filter.matches(entry))
    }

    /// Scroll up
    pub fn scroll_up(&mut self, lines: usize) {
        self.scroll = self.scroll.saturating_sub(lines);
        self.tail_mode = false;
    }

    /// Scroll down
    pub fn scroll_down(&mut self, lines: usize) {
        let count = self.filtered_entries().count();
        self.scroll = (self.scroll + lines).min(count.saturating_sub(1));
    }

    /// Scroll to top
    pub fn scroll_to_top(&mut self) {
        self.scroll = 0;
        self.selected = 0;
        self.tail_mode = false;
    }

    /// Scroll to bottom
    pub fn scroll_to_bottom(&mut self) {
        let count = self.filtered_entries().count();
        self.scroll = count.saturating_sub(1);
        self.selected = count.saturating_sub(1);
    }

    /// Move selection up
    pub fn select_prev(&mut self) {
        if self.selected > 0 {
            self.selected -= 1;
            self.ensure_visible(self.selected);
        }
        self.tail_mode = false;
    }

    /// Move selection down
    pub fn select_next(&mut self) {
        let count = self.filtered_entries().count();
        if self.selected < count.saturating_sub(1) {
            self.selected += 1;
            self.ensure_visible(self.selected);
        }
    }

    /// Ensure index is visible
    fn ensure_visible(&mut self, idx: usize) {
        if idx < self.scroll {
            self.scroll = idx;
        }
        // Note: actual visible height depends on render area
    }

    /// Clear all entries
    pub fn clear(&mut self) {
        self.entries.clear();
        self.scroll = 0;
        self.selected = 0;
        self.search_matches.clear();
    }

    /// Get total entry count
    pub fn len(&self) -> usize {
        self.entries.len()
    }

    /// Get filtered entry count
    pub fn filtered_len(&self) -> usize {
        self.filtered_entries().count()
    }

    /// Check if empty
    pub fn is_empty(&self) -> bool {
        self.entries.is_empty()
    }

    /// Get search match count
    pub fn search_match_count(&self) -> usize {
        self.search_matches.len()
    }

    /// Get current search index
    pub fn current_search_index(&self) -> usize {
        self.search_index
    }

    /// Check if in tail mode
    pub fn is_tail_mode(&self) -> bool {
        self.tail_mode
    }

    /// Set the active filter
    pub fn set_filter(&mut self, filter: LogFilter) {
        self.filter = filter;
        self.selected = 0;
        self.scroll = 0;
    }

    /// Update minimum level filter
    pub fn set_min_level(&mut self, level: LogLevel) {
        self.filter.min_level = Some(level);
        self.selected = 0;
        self.scroll = 0;
    }

    /// Clear all filters
    pub fn clear_filter(&mut self) {
        self.filter = LogFilter::new();
    }

    /// Toggle expanded state of selected entry
    pub fn toggle_selected_expanded(&mut self) {
        let entry_idx = {
            let filtered: Vec<_> = self.filtered_entries().collect();
            filtered.get(self.selected).map(|(idx, _)| *idx)
        };
        if let Some(idx) = entry_idx {
            if let Some(entry) = self.entries.get_mut(idx) {
                entry.toggle_expanded();
            }
        }
    }

    /// Handle key input
    pub fn handle_key(&mut self, key: &Key) -> bool {
        match key {
            Key::Up | Key::Char('k') => {
                self.select_prev();
                true
            }
            Key::Down | Key::Char('j') => {
                self.select_next();
                true
            }
            Key::PageUp => {
                for _ in 0..10 {
                    self.select_prev();
                }
                true
            }
            Key::PageDown => {
                for _ in 0..10 {
                    self.select_next();
                }
                true
            }
            Key::Home | Key::Char('g') => {
                self.scroll_to_top();
                true
            }
            Key::End | Key::Char('G') => {
                self.scroll_to_bottom();
                true
            }
            Key::Char('f') => {
                self.toggle_tail();
                true
            }
            Key::Char('w') => {
                self.toggle_wrap();
                true
            }
            Key::Char('b') => {
                self.toggle_bookmark();
                true
            }
            Key::Char('n') => {
                self.next_match();
                true
            }
            Key::Char('N') => {
                self.prev_match();
                true
            }
            Key::Char(']') => {
                self.next_bookmark();
                true
            }
            Key::Char('[') => {
                self.prev_bookmark();
                true
            }
            Key::Enter => {
                self.toggle_selected_expanded();
                true
            }
            _ => false,
        }
    }
}

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

impl View for LogViewer {
    crate::impl_view_meta!("LogViewer");

    fn render(&self, ctx: &mut RenderContext) {
        let area = ctx.area;
        let filtered: Vec<_> = self.filtered_entries().collect();

        if filtered.is_empty() {
            // Show empty message
            let msg = "No log entries";
            let x = (area.width.saturating_sub(display_width(msg) as u16)) / 2;
            let y = area.height / 2;
            let mut dx: u16 = 0;
            for ch in msg.chars() {
                let cw = char_width(ch) as u16;
                let mut cell = Cell::new(ch);
                cell.fg = Some(DISABLED_FG);
                cell.bg = self.bg;
                ctx.set(x + dx, y, cell);
                dx += cw;
            }
            return;
        }

        // Calculate column widths
        let line_num_width = if self.show_line_numbers { 6 } else { 0 };
        let timestamp_width = if self.show_timestamps { 12 } else { 0 };
        let level_width = if self.show_levels { 5 } else { 0 };
        let source_width = if self.show_source { 12 } else { 0 };
        let bookmark_width = 2;

        let prefix_width =
            line_num_width + bookmark_width + timestamp_width + level_width + source_width;
        let message_width = area.width.saturating_sub(prefix_width);

        // Visible range
        let visible_height = area.height as usize;
        let start = self.scroll.min(filtered.len().saturating_sub(1));
        let end = (start + visible_height).min(filtered.len());

        for (view_idx, (entry_idx, entry)) in
            filtered.iter().enumerate().skip(start).take(end - start)
        {
            let row = (view_idx - start) as u16;
            let y = row;

            if y >= area.height {
                break;
            }

            let is_selected = view_idx == self.selected;
            let level_color = entry.level.color();

            // Fill background
            let row_bg = if is_selected {
                Some(self.selected_bg)
            } else {
                self.bg
            };

            for x in 0..area.width {
                let mut cell = Cell::new(' ');
                cell.bg = row_bg;
                ctx.set(x, y, cell);
            }

            let mut x = 0u16;

            // Draw line number
            if self.show_line_numbers {
                let num_str = format!("{:>5}", entry.line_number);
                for ch in num_str.chars() {
                    let mut cell = Cell::new(ch);
                    cell.fg = Some(self.line_number_fg);
                    cell.bg = row_bg;
                    ctx.set(x, y, cell);
                    x += 1;
                }
                x += 1; // Space after line number
            }

            // Draw bookmark indicator
            let bookmark_char = if entry.bookmarked { '' } else { ' ' };
            let mut cell = Cell::new(bookmark_char);
            cell.fg = Some(self.bookmark_fg);
            cell.bg = row_bg;
            ctx.set(x, y, cell);
            x += bookmark_width;

            // Draw timestamp
            if self.show_timestamps {
                if let Some(ref ts) = entry.timestamp {
                    let ts_display = truncate_to_width(ts, timestamp_width as usize - 1);
                    for ch in ts_display.chars() {
                        let cw = char_width(ch) as u16;
                        let mut cell = Cell::new(ch);
                        cell.fg = Some(self.timestamp_fg);
                        cell.bg = row_bg;
                        ctx.set(x, y, cell);
                        x += cw;
                    }
                }
                x = line_num_width + bookmark_width + timestamp_width;
            }

            // Draw level
            if self.show_levels {
                let icon = entry.level.icon();
                let mut cell = Cell::new(icon);
                cell.fg = Some(level_color);
                cell.bg = row_bg;
                ctx.set(x, y, cell);
                x += 1;

                let label = entry.level.label();
                for ch in label.chars().take(3) {
                    let mut cell = Cell::new(ch);
                    cell.fg = Some(level_color);
                    cell.bg = row_bg;
                    cell.modifier |= Modifier::BOLD;
                    ctx.set(x, y, cell);
                    x += 1;
                }
                x = line_num_width + bookmark_width + timestamp_width + level_width;
            }

            // Draw source
            if self.show_source {
                if let Some(ref src) = entry.source {
                    let src_display = truncate_to_width(src, source_width as usize - 1);
                    for ch in src_display.chars() {
                        let cw = char_width(ch) as u16;
                        let mut cell = Cell::new(ch);
                        cell.fg = Some(self.source_fg);
                        cell.bg = row_bg;
                        ctx.set(x, y, cell);
                        x += cw;
                    }
                }
                x = prefix_width;
            }

            // Find search matches for this entry
            let matches_for_entry: Vec<_> = self
                .search_matches
                .iter()
                .filter(|m| m.entry_index == *entry_idx)
                .collect();

            // Draw message with search highlighting
            // Search match indices are byte offsets from the find() call in update_search
            let mut col_dx: u16 = 0;
            let mut byte_pos: usize = 0;
            for ch in entry.message.chars() {
                let cw = char_width(ch) as u16;
                if col_dx + cw > message_width {
                    break;
                }
                // Check if this character's byte range overlaps a search match
                let ch_byte_len = ch.len_utf8();
                let in_match = matches_for_entry
                    .iter()
                    .any(|m| byte_pos >= m.start && byte_pos < m.end);

                let mut cell = Cell::new(ch);
                cell.fg = Some(if is_selected {
                    Color::WHITE
                } else {
                    level_color
                });
                cell.bg = if in_match {
                    Some(self.search_highlight_bg)
                } else {
                    row_bg
                };
                if in_match {
                    cell.fg = Some(Color::BLACK);
                    cell.modifier |= Modifier::BOLD;
                }
                if is_selected {
                    cell.modifier |= Modifier::BOLD;
                }
                ctx.set(x + col_dx, y, cell);
                col_dx += cw;
                byte_pos += ch_byte_len;
            }
            let _ = col_dx; // message column is always last
        }

        // Draw scroll indicator
        if filtered.len() > visible_height {
            let scroll_ratio = self.scroll as f32 / (filtered.len() - visible_height) as f32;
            let indicator_pos = (scroll_ratio * (area.height as f32 - 1.0)) as u16;
            let indicator_y = indicator_pos.min(area.height - 1);

            let mut cell = Cell::new('');
            cell.fg = Some(DARK_GRAY);
            ctx.set(area.width - 1, indicator_y, cell);
        }

        // Draw tail mode indicator
        if self.tail_mode {
            let indicator = "◉ TAIL";
            let indicator_w = display_width(indicator) as u16;
            let x = area.width.saturating_sub(indicator_w + 2);
            let y = 0u16;
            let mut dx: u16 = 0;
            for ch in indicator.chars() {
                let cw = char_width(ch) as u16;
                let mut cell = Cell::new(ch);
                cell.fg = Some(Color::GREEN);
                cell.bg = self.bg;
                ctx.set(x + dx, y, cell);
                dx += cw;
            }
        }
    }
}

impl_styled_view!(LogViewer);
impl_props_builders!(LogViewer);

/// Create a new log viewer
pub fn log_viewer() -> LogViewer {
    LogViewer::new()
}

/// Create a new log entry
pub fn log_entry(raw: impl Into<String>, line_number: usize) -> LogEntry {
    LogEntry::new(raw, line_number)
}

/// Create a new log filter
pub fn log_filter() -> LogFilter {
    LogFilter::new()
}

/// Create a new log parser
pub fn log_parser() -> LogParser {
    LogParser::new()
}