logana 0.6.0

Turn any log source — files, compressed archives, Docker, or OTel streams — into structured data. Filter by pattern, field, or date range; annotate lines; bookmark findings; and export to Markdown, Jira, or AI assistants via the built-in MCP server.
Documentation
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
1084
1085
1086
1087
1088
1089
1090
1091
1092
1093
1094
1095
1096
1097
1098
1099
1100
1101
1102
1103
1104
1105
1106
1107
1108
1109
1110
1111
1112
1113
1114
1115
1116
1117
1118
1119
1120
1121
1122
1123
1124
1125
1126
1127
1128
1129
1130
1131
1132
1133
1134
1135
1136
1137
1138
1139
1140
1141
1142
1143
1144
1145
1146
1147
1148
1149
1150
1151
1152
1153
1154
1155
1156
1157
1158
1159
1160
1161
1162
1163
1164
1165
1166
1167
1168
1169
1170
1171
1172
1173
1174
1175
1176
1177
1178
1179
1180
1181
1182
1183
1184
1185
1186
1187
1188
1189
1190
1191
1192
1193
1194
1195
1196
1197
1198
1199
1200
1201
1202
1203
1204
1205
1206
1207
1208
1209
1210
1211
1212
1213
1214
1215
1216
1217
1218
1219
1220
1221
1222
1223
1224
1225
1226
1227
1228
1229
1230
1231
1232
1233
1234
1235
1236
1237
1238
1239
1240
1241
1242
1243
1244
1245
1246
1247
1248
1249
1250
1251
1252
1253
1254
1255
1256
1257
1258
1259
1260
1261
1262
1263
1264
1265
1266
1267
1268
1269
1270
1271
1272
1273
1274
1275
1276
1277
1278
1279
1280
1281
1282
1283
1284
1285
1286
1287
1288
1289
1290
1291
1292
1293
1294
1295
1296
1297
1298
1299
1300
1301
1302
1303
1304
1305
1306
1307
1308
1309
1310
1311
1312
1313
1314
1315
1316
1317
1318
1319
1320
1321
1322
1323
1324
1325
1326
1327
1328
1329
1330
1331
1332
1333
1334
1335
1336
1337
1338
1339
1340
1341
1342
1343
1344
1345
1346
1347
1348
1349
1350
1351
1352
1353
1354
1355
1356
1357
1358
1359
1360
1361
1362
1363
1364
1365
1366
1367
1368
1369
1370
1371
1372
1373
1374
1375
1376
1377
1378
1379
1380
1381
1382
1383
1384
1385
1386
1387
1388
1389
1390
1391
1392
1393
1394
1395
1396
1397
1398
1399
1400
1401
1402
1403
1404
1405
1406
1407
1408
1409
1410
1411
1412
1413
1414
1415
1416
1417
1418
1419
1420
1421
1422
1423
1424
1425
1426
1427
1428
1429
1430
1431
1432
1433
1434
1435
1436
1437
1438
1439
1440
1441
1442
1443
1444
1445
1446
1447
1448
1449
1450
1451
1452
1453
1454
1455
1456
1457
1458
1459
1460
1461
1462
1463
1464
1465
1466
1467
1468
1469
1470
1471
1472
1473
1474
1475
1476
1477
1478
1479
1480
1481
1482
1483
1484
1485
1486
1487
1488
1489
1490
1491
1492
1493
1494
1495
1496
1497
1498
1499
1500
1501
1502
1503
1504
1505
1506
1507
1508
1509
1510
1511
1512
1513
1514
1515
1516
1517
1518
1519
1520
1521
1522
1523
1524
1525
1526
1527
1528
1529
1530
1531
1532
1533
1534
1535
1536
1537
1538
1539
1540
1541
1542
1543
1544
1545
1546
1547
1548
1549
1550
1551
1552
1553
1554
1555
1556
1557
1558
1559
1560
1561
1562
1563
1564
1565
1566
1567
1568
1569
1570
1571
1572
1573
1574
1575
1576
use std::collections::{HashMap, HashSet};
use std::sync::Arc;

use ratatui::{
    prelude::*,
    style::Modifier,
    widgets::{
        Block, Borders, Padding, Paragraph, Scrollbar, ScrollbarOrientation, ScrollbarState, Wrap,
    },
};

use crate::filters::{CURRENT_SEARCH_STYLE_ID, MatchCollector, SEARCH_STYLE_ID, render_line};
use crate::parser::LogLevel;
use crate::theme::Theme;
use crate::ui::field_layout::{apply_field_layout, effective_row_count};
use crate::ui::{CachedParsedLine, TabState, VisibleLines};
use crate::utils::search::SearchResult;
use crate::value_colors::{
    VALUE_STYLE_HTTP_DELETE, VALUE_STYLE_HTTP_GET, VALUE_STYLE_HTTP_OTHER, VALUE_STYLE_HTTP_PATCH,
    VALUE_STYLE_HTTP_POST, VALUE_STYLE_HTTP_PUT, VALUE_STYLE_IP, VALUE_STYLE_STATUS_2XX,
    VALUE_STYLE_STATUS_3XX, VALUE_STYLE_STATUS_4XX, VALUE_STYLE_STATUS_5XX, VALUE_STYLE_UUID,
    collect_value_color_spans,
};

fn prepend_line_number(
    line: Line<'static>,
    line_idx: usize,
    line_number_width: usize,
    is_annotated: bool,
    comment_fg: Color,
    line_number_fg: Color,
    render_style: Style,
) -> Line<'static> {
    let line_num = line_idx + 1;
    let line_num_str = format!("{:>width$} ", line_num, width = line_number_width);
    let bar_span = if is_annotated {
        Span::styled("\u{2502}", Style::default().fg(comment_fg))
    } else {
        Span::styled(" ", Style::default().fg(line_number_fg))
    };
    let num_span = Span::styled(line_num_str, Style::default().fg(line_number_fg));
    let mut all_spans = vec![bar_span, num_span];
    all_spans.extend(line.spans);
    Line::from(all_spans).style(render_style)
}

fn prepare_comment_maps(
    comments: &[(Vec<usize>, String)],
    visible_indices: &VisibleLines,
    start: usize,
    end: usize,
) -> (HashMap<usize, usize>, HashMap<usize, usize>) {
    let mut line_cmt_map: HashMap<usize, usize> = HashMap::new();
    for (cmt_idx, (line_indices, _)) in comments.iter().enumerate() {
        for &li in line_indices {
            line_cmt_map.entry(li).or_insert(cmt_idx);
        }
    }
    let mut banner_at: HashMap<usize, usize> = HashMap::new();
    let mut vis_comment_map: HashMap<usize, usize> = HashMap::new();
    let mut seen_cmts: HashSet<usize> = HashSet::new();
    for abs_vi in start..end {
        let li = visible_indices.get(abs_vi);
        if let Some(&cmt_idx) = line_cmt_map.get(&li) {
            vis_comment_map.insert(abs_vi, cmt_idx);
            if seen_cmts.insert(cmt_idx) {
                banner_at.insert(abs_vi, cmt_idx);
            }
        }
    }
    (banner_at, vis_comment_map)
}

fn build_style_table(tab: &TabState, theme: &Theme) -> (u8, [Style; 256]) {
    let mut styles: Vec<Style> = if tab.filter.enabled {
        tab.filter.text_styles.clone()
    } else {
        Vec::new()
    };
    let process_style_start = styles.len() as u8;
    for &color in &theme.process_colors {
        styles.push(Style::default().fg(color));
    }
    styles.resize(256, Style::default());

    let search_style = Style::default()
        .fg(theme.search_fg)
        .bg(theme.text_highlight_fg);
    let current_search_style = Style::default()
        .fg(theme.text_highlight_fg)
        .bg(theme.search_fg);

    styles[255] = search_style;
    styles[254] = current_search_style;
    styles[VALUE_STYLE_HTTP_GET as usize] = Style::default().fg(theme.value_colors.http_get);
    styles[VALUE_STYLE_HTTP_POST as usize] = Style::default().fg(theme.value_colors.http_post);
    styles[VALUE_STYLE_HTTP_PUT as usize] = Style::default().fg(theme.value_colors.http_put);
    styles[VALUE_STYLE_HTTP_DELETE as usize] = Style::default().fg(theme.value_colors.http_delete);
    styles[VALUE_STYLE_HTTP_PATCH as usize] = Style::default().fg(theme.value_colors.http_patch);
    styles[VALUE_STYLE_HTTP_OTHER as usize] = Style::default().fg(theme.value_colors.http_other);
    styles[VALUE_STYLE_STATUS_2XX as usize] = Style::default().fg(theme.value_colors.status_2xx);
    styles[VALUE_STYLE_STATUS_3XX as usize] = Style::default().fg(theme.value_colors.status_3xx);
    styles[VALUE_STYLE_STATUS_4XX as usize] = Style::default().fg(theme.value_colors.status_4xx);
    styles[VALUE_STYLE_STATUS_5XX as usize] = Style::default().fg(theme.value_colors.status_5xx);
    styles[VALUE_STYLE_IP as usize] = Style::default().fg(theme.value_colors.ip_address);
    styles[VALUE_STYLE_UUID as usize] = Style::default().fg(theme.value_colors.uuid);

    let mut arr = [Style::default(); 256];
    arr.copy_from_slice(&styles);
    (process_style_start, arr)
}

fn build_comment_banner_lines(
    text: &str,
    inner_width: usize,
    total_width: usize,
    banner_dash_style: Style,
    banner_text_style: Style,
    banner_cont_style: Style,
) -> Vec<Line<'static>> {
    let _ = inner_width;
    let mut lines = Vec::new();
    for (i, text_line) in text.lines().enumerate() {
        if i == 0 {
            let left = " \u{2500}\u{2500} ";
            let text_len = text_line.chars().count();
            let used = left.len() + text_len + 1;
            let right_dashes = "\u{2500}".repeat(total_width.saturating_sub(used).max(1));
            lines.push(Line::from(vec![
                Span::styled(left, banner_dash_style),
                Span::styled(text_line.to_string(), banner_text_style),
                Span::styled(format!(" {right_dashes}"), banner_dash_style),
            ]));
        } else {
            lines.push(Line::from(vec![
                Span::styled("    ", banner_cont_style),
                Span::styled(text_line.to_string(), banner_cont_style),
            ]));
        }
    }
    lines
}

#[allow(clippy::too_many_arguments)]
fn compute_viewport(
    tab: &TabState,
    scroll_offset: usize,
    viewport_offset: usize,
    num_visible: usize,
    visible_height: usize,
    wrap: bool,
    inner_width: usize,
    hidden_fields: &HashSet<String>,
    field_layout: &crate::ui::FieldLayout,
    show_keys: bool,
    raw_mode: bool,
) -> (usize, usize) {
    let parser = if raw_mode {
        None
    } else {
        tab.display.format.as_deref()
    };
    let row_count = |li: usize| -> usize {
        effective_row_count(
            tab.file_reader.get_line(li),
            inner_width,
            parser,
            field_layout,
            hidden_fields,
            show_keys,
        )
    };

    let new_viewport = if scroll_offset < viewport_offset {
        scroll_offset
    } else if wrap && inner_width > 0 && num_visible > 0 {
        let gap = scroll_offset.saturating_sub(viewport_offset);
        let overflowed = gap > visible_height || {
            let rows_used: usize = (viewport_offset..=scroll_offset)
                .map(|i| row_count(tab.filter.visible_indices.get(i)))
                .sum();
            rows_used > visible_height
        };
        if overflowed {
            let mut rows = 0usize;
            let mut new_vp = scroll_offset + 1;
            loop {
                if new_vp == 0 {
                    break;
                }
                new_vp -= 1;
                let h = row_count(tab.filter.visible_indices.get(new_vp));
                if rows + h > visible_height {
                    new_vp += 1;
                    break;
                }
                rows += h;
                if new_vp == 0 {
                    break;
                }
            }
            new_vp.min(scroll_offset)
        } else {
            viewport_offset
        }
    } else if visible_height > 0 && scroll_offset >= viewport_offset + visible_height {
        scroll_offset - visible_height + 1
    } else {
        viewport_offset
    };

    let start = new_viewport;
    let end = if wrap && inner_width > 0 {
        let mut rows = 0usize;
        let mut e = start;
        while e < num_visible {
            let h = row_count(tab.filter.visible_indices.get(e));
            if rows + h > visible_height {
                break;
            }
            rows += h;
            e += 1;
        }
        if e == start && start < num_visible {
            e = start + 1;
        }
        e
    } else {
        (start + visible_height).min(num_visible)
    };

    if end == num_visible && num_visible > 0 {
        let filled_start = if wrap && inner_width > 0 {
            let mut rows = 0usize;
            let mut s = num_visible;
            loop {
                if s == 0 {
                    break;
                }
                s -= 1;
                let h = row_count(tab.filter.visible_indices.get(s));
                if rows + h > visible_height {
                    s += 1;
                    break;
                }
                rows += h;
                if s == 0 {
                    break;
                }
            }
            s
        } else {
            num_visible.saturating_sub(visible_height)
        };
        if filled_start < new_viewport {
            let adj_end = if wrap && inner_width > 0 {
                let mut rows = 0usize;
                let mut e = filled_start;
                while e < num_visible {
                    let h = row_count(tab.filter.visible_indices.get(e));
                    if rows + h > visible_height {
                        break;
                    }
                    rows += h;
                    e += 1;
                }
                if e == filled_start && filled_start < num_visible {
                    e += 1;
                }
                e
            } else {
                num_visible
            };
            (filled_start, adj_end)
        } else {
            (new_viewport, end)
        }
    } else {
        (new_viewport, end)
    }
}

fn populate_parse_cache(
    tab: &mut TabState,
    start: usize,
    end: usize,
    raw_mode: bool,
    hidden_fields: &HashSet<String>,
    field_layout: &crate::ui::FieldLayout,
    show_keys: bool,
) {
    let cache_gen = tab.cache.parse_gen;
    let mut new_entries: Vec<(usize, CachedParsedLine)> = Vec::new();

    // For merged tabs, resolve per-source parsers and source labels.
    let merged_entries_arc: Option<Arc<Vec<crate::ingestion::MergedEntry>>> =
        tab.file_reader.merged_entries().cloned();
    let merged_parsers: Option<Vec<Option<Arc<dyn crate::parser::LogFormatParser>>>> =
        tab.merged.as_ref().map(|m| m.source_parsers.clone());
    let merged_labels: Option<Vec<String>> = tab.merged.as_ref().map(|m| m.source_labels.clone());
    let label_col_width: usize = tab.merged.as_ref().map(|m| m.label_col_width).unwrap_or(0);
    let source_hidden = hidden_fields.contains("source");
    let has_any_parser = merged_parsers.is_some() || tab.display.format.is_some();
    let is_merged = merged_entries_arc.is_some();

    if raw_mode {
        return;
    }

    for vi in start..end {
        let line_idx = tab.filter.visible_indices.get(vi);
        if tab
            .cache
            .parse
            .get(&line_idx)
            .map(|(g, _)| *g == cache_gen)
            .unwrap_or(false)
        {
            continue;
        }

        let line_bytes = tab.file_reader.get_line(line_idx);

        // Source prefix to prepend for merged tabs (empty string if hidden or not merged).
        // Padded to label_col_width so all log content starts at the same column.
        let source_prefix: String = if !source_hidden
            && let Some(entries) = merged_entries_arc.as_ref()
            && let Some(labels) = merged_labels.as_ref()
            && let Some(entry) = entries.get(line_idx)
            && let Some(label) = labels.get(entry.source_idx)
        {
            format!("{:<width$}", label, width = label_col_width)
        } else {
            String::new()
        };

        let parser: Option<&dyn crate::parser::LogFormatParser> =
            if let (Some(entries), Some(parsers)) =
                (merged_entries_arc.as_ref(), merged_parsers.as_ref())
            {
                entries
                    .get(line_idx)
                    .and_then(|e| parsers.get(e.source_idx))
                    .and_then(|p| p.as_deref())
            } else {
                tab.display.format.as_deref()
            };
        let year_override = if is_merged {
            None
        } else {
            tab.year_map.as_deref().map(|ym| ym.year_for_line(line_idx))
        };

        if has_any_parser
            && let Some(parser) = parser
            && let Some(parts) = parser.parse_line(line_bytes)
        {
            let cols = apply_field_layout(
                &parts,
                field_layout,
                hidden_fields,
                show_keys,
                year_override,
            );
            let all_cols_hidden = source_prefix.is_empty() && cols.is_empty();
            let level = parts.level.map(|s| s.to_string());
            let timestamp = parts.timestamp.map(|s| s.to_string());
            let target = parts.target.map(|s| s.to_string());
            let pid = parts
                .extra_fields
                .iter()
                .find(|(_, k, _)| *k == "pid")
                .map(|(_, _, v)| v.to_string());
            let rendered = if all_cols_hidden {
                String::new()
            } else {
                let col_bytes: usize = cols.iter().map(|c| c.len()).sum();
                let cap = source_prefix.len()
                    + if source_prefix.is_empty() { 0 } else { 1 }
                    + col_bytes
                    + cols.len();
                let mut buf = String::with_capacity(cap);
                if !source_prefix.is_empty() {
                    buf.push_str(&source_prefix);
                    if !cols.is_empty() {
                        buf.push(' ');
                    }
                }
                for (i, col) in cols.iter().enumerate() {
                    if i > 0 {
                        buf.push(' ');
                    }
                    buf.push_str(col);
                }
                buf
            };
            let target_offset = target
                .as_deref()
                .filter(|t| !t.is_empty())
                .and_then(|t| find_token_offset(&rendered, t));
            let pid_offset = pid
                .as_deref()
                .filter(|p| !p.is_empty())
                .and_then(|p| find_token_offset(&rendered, p));
            let timestamp_offset = timestamp
                .as_deref()
                .filter(|ts| !ts.is_empty())
                .and_then(|ts| rendered.find(ts));
            new_entries.push((
                line_idx,
                CachedParsedLine {
                    rendered,
                    level,
                    timestamp,
                    target,
                    pid,
                    all_cols_hidden,
                    target_offset,
                    pid_offset,
                    timestamp_offset,
                },
            ));
        } else if !source_prefix.is_empty() {
            // Raw/unparsed line in a merged tab with a visible source label.
            // Create a minimal cache entry so the source prefix is part of the
            // content (not a separate span), keeping char-selection offsets correct.
            let raw_text = std::str::from_utf8(line_bytes).unwrap_or_default();
            let rendered = format!("{} {}", source_prefix, raw_text);
            new_entries.push((
                line_idx,
                CachedParsedLine {
                    rendered,
                    level: None,
                    timestamp: None,
                    target: None,
                    pid: None,
                    all_cols_hidden: false,
                    target_offset: None,
                    pid_offset: None,
                    timestamp_offset: None,
                },
            ));
        }
    }
    for (line_idx, entry) in new_entries {
        tab.cache.parse.insert(line_idx, (cache_gen, entry));
    }
}

fn stable_hash(s: &str) -> usize {
    s.bytes().fold(5381usize, |acc, b| {
        acc.wrapping_mul(33).wrapping_add(b as usize)
    })
}

fn find_token_offset(haystack: &str, needle: &str) -> Option<usize> {
    if needle.is_empty() {
        return None;
    }
    let hb = haystack.as_bytes();
    let nb = needle.len();
    let mut start = 0;
    while start + nb <= hb.len() {
        match haystack[start..].find(needle) {
            None => break,
            Some(rel) => {
                let abs = start + rel;
                let before_ok = abs == 0 || hb[abs - 1] == b' ';
                let after_ok = abs + nb == hb.len() || hb[abs + nb] == b' ';
                if before_ok && after_ok {
                    return Some(abs);
                }
                start = abs + 1;
            }
        }
    }
    None
}

pub struct LogPanelData {
    pub log_lines: Vec<Line<'static>>,
    pub num_visible: usize,
    pub visible_height: usize,
    pub start: usize,
    pub horizontal_scroll: usize,
    pub logs_title: String,
    pub show_borders: bool,
    pub show_tab_bar: bool,
    pub wrap: bool,
    pub theme_border: Color,
    pub theme_border_title: Color,
    pub extraction_progress: Option<f64>,
    pub archive_name: String,
}

#[allow(clippy::too_many_arguments)]
pub fn prepare_log_panel(
    tab: &mut TabState,
    logs_area: Rect,
    visual_anchor: Option<usize>,
    visual_char_selection: Option<(usize, usize)>,
    mode_name: Option<&str>,
    show_tab_bar: bool,
    has_input_bar: bool,
    theme: &Theme,
) -> LogPanelData {
    let num_visible = tab.filter.visible_indices.len();
    let show_borders = tab.display.show_borders;

    let vertical_border = if show_borders {
        if show_tab_bar { 1 } else { 2 }
    } else {
        1
    };
    let horizontal_shrink = if show_borders { 2 } else { 1 };
    let visible_height = (logs_area.height as usize)
        .saturating_sub(vertical_border)
        .saturating_sub(usize::from(has_input_bar));
    tab.scroll.visible_height = visible_height;

    let show_line_numbers = tab.display.show_line_numbers;
    let total_lines = tab.file_reader.line_count();
    let line_number_width = if show_line_numbers {
        total_lines.max(1).to_string().len()
    } else {
        0
    };
    let ln_prefix_width = if show_line_numbers {
        line_number_width + 2
    } else {
        0
    };
    let inner_width =
        (logs_area.width as usize).saturating_sub(horizontal_shrink + ln_prefix_width);
    tab.scroll.visible_width = inner_width;

    let wrap = tab.display.wrap;
    let hidden_fields = tab.display.hidden_fields.clone();
    let field_layout = tab.display.field_layout.clone();
    let show_keys = tab.display.show_keys;
    let raw_mode = tab.display.raw_mode;

    if num_visible == 0 {
        tab.scroll.scroll_offset = 0;
        tab.scroll.viewport_offset = 0;
    } else {
        if tab.scroll.scroll_offset >= num_visible {
            tab.scroll.scroll_offset = num_visible - 1;
        }
        if tab.scroll.viewport_offset >= num_visible {
            tab.scroll.viewport_offset = num_visible.saturating_sub(visible_height);
        }
    }

    let scroll_offset = tab.scroll.scroll_offset;
    let viewport_offset = tab.scroll.viewport_offset;

    let (new_viewport, end) = compute_viewport(
        tab,
        scroll_offset,
        viewport_offset,
        num_visible,
        visible_height,
        wrap,
        inner_width,
        &hidden_fields,
        &field_layout,
        show_keys,
        raw_mode,
    );

    tab.scroll.viewport_offset = new_viewport;
    let start = new_viewport;

    #[cfg(unix)]
    if start < end && !tab.filter.visible_indices.is_empty() {
        let first = tab.filter.visible_indices.get(start);
        let last = tab.filter.visible_indices.get((end - 1).max(start));
        tab.file_reader.advise_viewport(first, last);
    }

    populate_parse_cache(
        tab,
        start,
        end,
        raw_mode,
        &hidden_fields,
        &field_layout,
        show_keys,
    );

    let filter_manager_arc = tab.filter.manager.clone();
    let filter_manager = &*filter_manager_arc;
    let date_filter_styles = if tab.filter.enabled {
        tab.filter.date_styles.clone()
    } else {
        Vec::new()
    };
    let field_filter_styles = if tab.filter.enabled {
        tab.filter.field_styles.clone()
    } else {
        Vec::new()
    };
    let detected_format_arc: Option<Arc<dyn crate::parser::LogFormatParser>> = if raw_mode {
        None
    } else {
        tab.display.format.clone()
    };

    let (process_style_start, styles) = build_style_table(tab, theme);
    let process_colors_len = theme.process_colors.len();

    let search_results = tab.search.query.get_results();
    let current_search_info: Option<(usize, usize)> = if search_results.is_empty() {
        None
    } else {
        let ri = tab.search.query.get_current_match_index();
        Some((
            search_results[ri].line_idx,
            tab.search.query.get_current_occurrence_index(),
        ))
    };
    let find_search_result = |line_idx: usize| -> Option<&SearchResult> {
        search_results
            .binary_search_by_key(&line_idx, |r| r.line_idx)
            .ok()
            .map(|i| &search_results[i])
    };
    let search_regex = tab.search.query.get_compiled_pattern().cloned();

    let level_colors_disabled = tab.display.level_colors_disabled.clone();
    let current_scroll = tab.scroll.scroll_offset;
    let visual_range: Option<(usize, usize)> = visual_anchor.map(|anchor| {
        let lo = anchor.min(current_scroll);
        let hi = anchor.max(current_scroll);
        (lo, hi)
    });
    let visual_style = Style::default()
        .fg(theme.visual_select_fg)
        .bg(theme.visual_select_bg);

    let comments_for_render: Vec<(Vec<usize>, String)> = tab
        .comment_manager
        .get()
        .iter()
        .map(|a| (a.line_indices.clone(), a.text.clone()))
        .collect();

    let (banner_at, vis_comment_map) = prepare_comment_maps(
        &comments_for_render,
        &tab.filter.visible_indices,
        start,
        end,
    );

    let banner_dash_style = Style::default()
        .fg(theme.comment_fg)
        .add_modifier(Modifier::DIM);
    let banner_text_style = Style::default()
        .fg(theme.comment_fg)
        .add_modifier(Modifier::BOLD);
    let banner_cont_style = Style::default().fg(theme.comment_fg);

    let render_gen = tab.cache.render_gen;
    let search_gen = tab.cache.search_result_gen;
    let parse_gen = tab.cache.parse_gen;

    let mut render_cache_misses: Vec<(usize, Option<usize>, Line<'static>)> = Vec::new();
    let mut log_lines: Vec<Line> = Vec::new();

    for abs_vis_idx in start..end {
        let line_idx = tab.filter.visible_indices.get(abs_vis_idx);
        let line_bytes = tab.file_reader.get_line(line_idx);
        let is_current = abs_vis_idx == current_scroll;
        let is_marked = tab.mark_manager.is_marked(line_idx);
        let is_visual_selected = visual_range
            .map(|(lo, hi)| abs_vis_idx >= lo && abs_vis_idx <= hi)
            .unwrap_or(false);

        let cached = tab
            .cache
            .parse
            .get(&line_idx)
            .filter(|(g, _)| *g == parse_gen)
            .map(|(_, c)| c);

        let mut base_style = Style::default().fg(theme.text);
        if level_colors_disabled.len() < 7 {
            let level = cached
                .and_then(|c| c.level.as_deref())
                .map(LogLevel::parse_level)
                .unwrap_or_else(|| {
                    // For continuation lines (parse_line returned None), inherit
                    // the parent entry's level so the whole multiline block gets
                    // the same color (e.g. a stack trace stays red under ERROR).
                    if let Some(cmap) = &tab.continuation_map {
                        let parent = cmap.get(line_idx).copied().unwrap_or(line_idx);
                        if parent != line_idx {
                            // Try the parent's cache entry first.
                            if let Some(lvl) = tab
                                .cache
                                .parse
                                .get(&parent)
                                .filter(|(g, _)| *g == parse_gen)
                                .and_then(|(_, c)| c.level.as_deref())
                            {
                                return LogLevel::parse_level(lvl);
                            }
                            // Parent not cached (outside viewport) — parse just
                            // the level from its raw bytes without full layout.
                            if let Some(parser) = tab.display.format.as_deref()
                                && let Some(parts) =
                                    parser.parse_line(tab.file_reader.get_line(parent))
                                && let Some(lvl) = parts.level
                            {
                                return LogLevel::parse_level(lvl);
                            }
                        }
                    }
                    LogLevel::detect_from_bytes(line_bytes)
                });
            match level {
                LogLevel::Trace if !level_colors_disabled.contains("trace") => {
                    base_style = base_style.fg(theme.trace_fg)
                }
                LogLevel::Debug if !level_colors_disabled.contains("debug") => {
                    base_style = base_style.fg(theme.debug_fg)
                }
                LogLevel::Info if !level_colors_disabled.contains("info") => {
                    base_style = base_style.fg(theme.info_fg)
                }
                LogLevel::Notice if !level_colors_disabled.contains("notice") => {
                    base_style = base_style.fg(theme.notice_fg)
                }
                LogLevel::Warning if !level_colors_disabled.contains("warning") => {
                    base_style = base_style.bg(theme.warning_bg)
                }
                LogLevel::Error if !level_colors_disabled.contains("error") => {
                    base_style = base_style.bg(theme.error_bg)
                }
                LogLevel::Fatal if !level_colors_disabled.contains("fatal") => {
                    base_style = base_style.bg(theme.fatal_bg)
                }
                _ => {}
            }
        }
        if is_marked {
            base_style = base_style.fg(theme.mark_fg).bg(theme.mark_bg);
        }
        if is_visual_selected {
            base_style = visual_style;
        }

        let render_style = if is_current && visual_char_selection.is_none() {
            base_style
                .add_modifier(Modifier::BOLD)
                .add_modifier(Modifier::UNDERLINED)
        } else {
            base_style
        };

        let current_occ =
            current_search_info.and_then(|(cl, co)| if cl == line_idx { Some(co) } else { None });

        let content_line: Line<'static> = if let Some((_, _, _, cached_line)) = tab
            .cache
            .render_line
            .get(&line_idx)
            .filter(|(rg, sg, occ, _)| {
                *rg == render_gen && *sg == search_gen && *occ == current_occ
            }) {
            cached_line.clone()
        } else {
            let structured_line: Option<Line<'static>> = cached.filter(|_| !raw_mode).map(|c| {
                if c.all_cols_hidden {
                    let mut collector = MatchCollector::new(line_bytes);
                    if let Ok(text) = std::str::from_utf8(line_bytes) {
                        for (s, e, sid) in collect_value_color_spans(text, &theme.value_colors) {
                            collector.push(s, e, sid);
                        }
                    }
                    collector.with_priority(500);
                    filter_manager.evaluate_into(&mut collector);
                    if let Some(sr) = find_search_result(line_idx) {
                        collector.with_priority(1000);
                        for (i, &(s, e)) in sr.matches.iter().enumerate() {
                            let sid = if current_occ == Some(i) {
                                CURRENT_SEARCH_STYLE_ID
                            } else {
                                SEARCH_STYLE_ID
                            };
                            collector.push(s, e, sid);
                        }
                    }
                    render_line(&collector, &styles)
                } else {
                    let rendered = &c.rendered;
                    let mut collector = MatchCollector::new(rendered.as_bytes());
                    for (s, e, sid) in collect_value_color_spans(rendered, &theme.value_colors) {
                        collector.push(s, e, sid);
                    }
                    collector.with_priority(10);
                    if process_colors_len > 0
                        && !theme.value_colors.is_disabled("process_colors")
                        && let Some(target) = c.target.as_deref()
                    {
                        let idx = stable_hash(target) % process_colors_len;
                        let sid = process_style_start.saturating_add(idx as u8);
                        if let Some(pos) = c.target_offset {
                            collector.push(pos, pos + target.len(), sid);
                        }
                        if let Some(pid_val) = c.pid.as_deref() {
                            let pid_sid = process_style_start
                                .saturating_add((stable_hash(target) % process_colors_len) as u8);
                            if let Some(pos) = c.pid_offset {
                                collector.push(pos, pos + pid_val.len(), pid_sid);
                            }
                        }
                    }
                    collector.with_priority(500);
                    filter_manager.evaluate_into(&mut collector);
                    if let Some(ts) = c.timestamp.as_deref() {
                        for dfs in &date_filter_styles {
                            if dfs.filter.matches(ts, None) {
                                collector.with_priority(500);
                                if dfs.match_only {
                                    if let Some(ts_pos) = c.timestamp_offset {
                                        collector.push(ts_pos, ts_pos + ts.len(), dfs.style_id);
                                    }
                                } else {
                                    collector.push(0, rendered.len(), dfs.style_id);
                                }
                            }
                        }
                    }
                    if !field_filter_styles.is_empty()
                        && let Some(ref parser_arc) = detected_format_arc
                    {
                        let ffs_parser: &dyn crate::parser::LogFormatParser = parser_arc.as_ref();
                        if let Some(parts) = ffs_parser.parse_line(line_bytes) {
                            collector.with_priority(500);
                            for ffs in &field_filter_styles {
                                if let Some(val) =
                                    crate::filters::resolve_field(&ffs.field_filter.field, &parts)
                                        .filter(|v| v.contains(ffs.field_filter.pattern.as_str()))
                                {
                                    if ffs.match_only {
                                        if let Some(pos) = rendered.find(val) {
                                            collector.push(pos, pos + val.len(), ffs.style_id);
                                        }
                                    } else {
                                        collector.push(0, rendered.len(), ffs.style_id);
                                    }
                                }
                            }
                        }
                    }
                    if let Some(ref regex) = search_regex {
                        collector.with_priority(1000);
                        for (i, m) in regex.find_iter(rendered).enumerate() {
                            let sid = if current_occ == Some(i) {
                                CURRENT_SEARCH_STYLE_ID
                            } else {
                                SEARCH_STYLE_ID
                            };
                            collector.push(m.start(), m.end(), sid);
                        }
                    }
                    render_line(&collector, &styles)
                }
            });

            let line = if let Some(sl) = structured_line {
                sl
            } else {
                let mut collector = MatchCollector::new(line_bytes);
                if let Ok(text) = std::str::from_utf8(line_bytes) {
                    for (s, e, sid) in collect_value_color_spans(text, &theme.value_colors) {
                        collector.push(s, e, sid);
                    }
                }
                collector.with_priority(500);
                filter_manager.evaluate_into(&mut collector);
                if let Some(sr) = find_search_result(line_idx) {
                    collector.with_priority(1000);
                    for (i, &(s, e)) in sr.matches.iter().enumerate() {
                        let sid = if current_occ == Some(i) {
                            CURRENT_SEARCH_STYLE_ID
                        } else {
                            SEARCH_STYLE_ID
                        };
                        collector.push(s, e, sid);
                    }
                }
                render_line(&collector, &styles)
            };
            render_cache_misses.push((line_idx, current_occ, line.clone()));
            line
        };

        let mut line = content_line.style(render_style);

        if is_current && let Some((lo, hi)) = visual_char_selection {
            line = crate::mode::visual_char_mode::apply_char_selection(line, lo, hi);
        }

        if show_line_numbers {
            let is_annotated = vis_comment_map.contains_key(&abs_vis_idx);
            line = prepend_line_number(
                line,
                line_idx,
                line_number_width,
                is_annotated,
                theme.comment_fg,
                theme.line_number_fg,
                render_style,
            );
        }

        if let Some(&cmt_idx) = banner_at.get(&abs_vis_idx) {
            let (_, text) = &comments_for_render[cmt_idx];
            let total_width = inner_width + ln_prefix_width;
            for banner_line in build_comment_banner_lines(
                text,
                inner_width,
                total_width,
                banner_dash_style,
                banner_text_style,
                banner_cont_style,
            ) {
                log_lines.push(banner_line);
            }
        }
        log_lines.push(line);
    }

    for (line_idx, current_occ, content_line) in render_cache_misses {
        tab.cache.render_line.insert(
            line_idx,
            (render_gen, search_gen, current_occ, content_line),
        );
    }

    let tail_mode = tab.stream.tail_mode;
    let paused = tab.stream.paused;
    let logs_title = if show_tab_bar {
        String::new()
    } else {
        format!(
            "{}{} ({}){}{}{}",
            mode_name.map(|m| format!("[{}] ", m)).unwrap_or_default(),
            tab.log_manager
                .source_file()
                .map(|s| {
                    std::path::Path::new(s)
                        .file_name()
                        .and_then(|n| n.to_str())
                        .unwrap_or(s)
                        .to_string()
                })
                .unwrap_or(String::from("Logs")),
            num_visible,
            if tail_mode { " [TAIL]" } else { "" },
            if raw_mode { " [RAW]" } else { "" },
            if paused { " [PAUSED]" } else { "" },
        )
    };

    LogPanelData {
        log_lines,
        num_visible,
        visible_height,
        start,
        horizontal_scroll: tab.scroll.horizontal_scroll,
        logs_title,
        show_borders,
        show_tab_bar,
        wrap,
        theme_border: theme.border,
        theme_border_title: theme.border_title,
        extraction_progress: tab.extraction_progress,
        archive_name: tab.title.clone(),
    }
}

pub struct LogPanel<'a> {
    pub data: &'a LogPanelData,
}

impl<'a> Widget for LogPanel<'a> {
    fn render(self, area: Rect, buf: &mut Buffer) {
        let d = self.data;
        let logs_borders = if d.show_borders {
            if d.show_tab_bar {
                Borders::LEFT | Borders::RIGHT | Borders::BOTTOM
            } else {
                Borders::ALL
            }
        } else {
            Borders::NONE
        };
        let title_style = Style::default().fg(d.theme_border_title);
        let logs_block = if d.show_borders {
            let block = Block::default()
                .borders(logs_borders)
                .border_style(Style::default().fg(d.theme_border));
            if d.logs_title.is_empty() {
                block
            } else {
                block.title(d.logs_title.clone()).title_style(title_style)
            }
        } else {
            let block = Block::default()
                .borders(Borders::NONE)
                .padding(Padding::new(1, 0, 0, 0));
            if d.logs_title.is_empty() {
                block
            } else {
                block.title(d.logs_title.clone()).title_style(title_style)
            }
        };

        let mut paragraph = Paragraph::new(d.log_lines.clone())
            .block(logs_block)
            .scroll((0, d.horizontal_scroll as u16));

        if d.wrap {
            paragraph = paragraph.wrap(Wrap { trim: false });
        }

        paragraph.render(area, buf);

        let inner = if d.show_borders {
            area.inner(ratatui::layout::Margin {
                horizontal: 1,
                vertical: 1,
            })
        } else {
            area
        };
        if let Some(fraction) = d.extraction_progress {
            let (bar, pct) = crate::ui::render::progress_bar_str(fraction);
            let text = format!("{}\n{bar}  {pct}%", d.archive_name);
            let overlay = Paragraph::new(text).alignment(Alignment::Center);
            let mid_y = (inner.y + inner.height / 2).saturating_sub(1);
            let overlay_area = Rect {
                x: inner.x,
                y: mid_y,
                width: inner.width,
                height: 2,
            };
            overlay.render(overlay_area, buf);
        }

        if d.num_visible > 0 {
            let max_scroll = d.num_visible.saturating_sub(d.visible_height);
            let mut scrollbar_state = ScrollbarState::new(max_scroll.max(1)).position(d.start);
            Scrollbar::default()
                .orientation(ScrollbarOrientation::VerticalRight)
                .style(Style::default().fg(d.theme_border))
                .render(area, buf, &mut scrollbar_state);
        }
    }
}

#[cfg(test)]
mod tests {
    use super::*;
    use crate::config::Keybindings;
    use crate::db::Database;
    use crate::db::LogManager;
    use crate::ingestion::FileReader;
    use crate::theme::Theme;
    use crate::ui::App;
    use ratatui::{Terminal, backend::TestBackend};
    use std::sync::Arc;

    async fn make_app(lines: &[&str]) -> App {
        let data: Vec<u8> = lines.join("\n").into_bytes();
        let file_reader = FileReader::from_bytes(data);
        let db = Arc::new(Database::in_memory().await.unwrap());
        let log_manager = LogManager::new(db, None).await;
        App::builder(
            log_manager,
            file_reader,
            Theme::default(),
            Arc::new(Keybindings::default()),
        )
        .build()
        .await
    }

    fn make_terminal() -> Terminal<TestBackend> {
        Terminal::new(TestBackend::new(80, 24)).unwrap()
    }

    #[tokio::test]
    async fn test_log_panel_basic_render() {
        let mut app = make_app(&["INFO line one", "WARN line two", "ERROR line three"]).await;
        let mut terminal = make_terminal();
        terminal.draw(|f| app.ui(f)).unwrap();
        let buf = terminal.backend().buffer().clone();
        let content: String = buf.content().iter().map(|c| c.symbol()).collect();
        assert!(content.contains("INFO"));
    }

    #[tokio::test]
    async fn test_log_panel_empty_file() {
        let mut app = make_app(&[]).await;
        let mut terminal = make_terminal();
        terminal.draw(|f| app.ui(f)).unwrap();
    }

    #[tokio::test]
    async fn test_log_panel_with_borders() {
        let mut app = make_app(&["line one", "line two"]).await;
        app.tabs[0].display.show_borders = true;
        let mut terminal = make_terminal();
        terminal.draw(|f| app.ui(f)).unwrap();
    }

    #[tokio::test]
    async fn test_log_panel_without_line_numbers() {
        let mut app = make_app(&["INFO hello", "DEBUG world"]).await;
        app.tabs[0].display.show_line_numbers = false;
        let mut terminal = make_terminal();
        terminal.draw(|f| app.ui(f)).unwrap();
    }

    #[tokio::test]
    async fn test_log_panel_with_wrap() {
        let long_line = "A".repeat(200);
        let mut app = make_app(&[&long_line]).await;
        app.tabs[0].display.wrap = true;
        let mut terminal = make_terminal();
        terminal.draw(|f| app.ui(f)).unwrap();
    }

    #[tokio::test]
    async fn test_log_panel_with_horizontal_scroll() {
        let long_line = "A".repeat(200);
        let mut app = make_app(&[&long_line]).await;
        app.tabs[0].scroll.horizontal_scroll = 10;
        let mut terminal = make_terminal();
        terminal.draw(|f| app.ui(f)).unwrap();
    }

    #[tokio::test]
    async fn test_log_panel_json_structured_lines() {
        let mut app = make_app(&[
            r#"{"level":"INFO","msg":"hello world","target":"myapp"}"#,
            r#"{"level":"ERROR","msg":"something failed"}"#,
        ])
        .await;
        let mut terminal = make_terminal();
        terminal.draw(|f| app.ui(f)).unwrap();
    }

    #[tokio::test]
    async fn test_log_panel_with_mark() {
        let mut app = make_app(&["line one", "line two", "line three"]).await;
        app.tabs[0].mark_manager.toggle(0);
        let mut terminal = make_terminal();
        terminal.draw(|f| app.ui(f)).unwrap();
    }

    #[tokio::test]
    async fn test_log_panel_scroll_offset_mid() {
        let lines: Vec<&str> = (0..30).map(|_| "line content here").collect();
        let mut app = make_app(&lines).await;
        app.tabs[0].scroll.scroll_offset = 15;
        let mut terminal = make_terminal();
        terminal.draw(|f| app.ui(f)).unwrap();
    }

    #[tokio::test]
    async fn test_log_panel_raw_mode() {
        let mut app = make_app(&[
            r#"{"level":"INFO","msg":"hello"}"#,
            r#"{"level":"WARN","msg":"warning"}"#,
        ])
        .await;
        app.tabs[0].display.raw_mode = true;
        let mut terminal = make_terminal();
        terminal.draw(|f| app.ui(f)).unwrap();
    }

    #[test]
    fn test_prepend_line_number_basic() {
        let line = Line::from("hello");
        let result = prepend_line_number(
            line,
            0,
            3,
            false,
            Color::Yellow,
            Color::Gray,
            Style::default(),
        );
        let combined: String = result.spans.iter().map(|s| s.content.as_ref()).collect();
        assert!(combined.contains("hello"));
        assert!(combined.contains("1"));
    }

    #[test]
    fn test_prepend_line_number_annotated() {
        let line = Line::from("annotated");
        let result = prepend_line_number(
            line,
            4,
            2,
            true,
            Color::Yellow,
            Color::Gray,
            Style::default(),
        );
        let bar_span = &result.spans[0];
        assert_eq!(bar_span.content, "\u{2502}");
    }

    #[test]
    fn test_build_comment_banner_single_line() {
        let lines = build_comment_banner_lines(
            "my comment",
            60,
            70,
            Style::default(),
            Style::default(),
            Style::default(),
        );
        assert_eq!(lines.len(), 1);
        let text: String = lines[0].spans.iter().map(|s| s.content.as_ref()).collect();
        assert!(text.contains("my comment"));
    }

    #[test]
    fn test_build_comment_banner_multi_line() {
        let lines = build_comment_banner_lines(
            "first\nsecond",
            60,
            70,
            Style::default(),
            Style::default(),
            Style::default(),
        );
        assert_eq!(lines.len(), 2);
    }

    #[test]
    fn test_stable_hash_deterministic() {
        assert_eq!(stable_hash("hello"), stable_hash("hello"));
        assert_ne!(stable_hash("hello"), stable_hash("world"));
    }

    #[test]
    fn test_find_token_offset_found() {
        assert_eq!(find_token_offset("hello world foo", "world"), Some(6));
    }

    #[test]
    fn test_find_token_offset_not_found() {
        assert_eq!(find_token_offset("helloworld", "world"), None);
    }

    #[test]
    fn test_find_token_offset_empty_needle() {
        assert_eq!(find_token_offset("hello world", ""), None);
    }

    #[test]
    fn test_find_token_offset_at_start() {
        assert_eq!(find_token_offset("foo bar", "foo"), Some(0));
    }

    #[test]
    fn test_find_token_offset_at_end() {
        assert_eq!(find_token_offset("foo bar", "bar"), Some(4));
    }

    #[test]
    fn test_log_panel_widget_no_borders_no_title() {
        let data = LogPanelData {
            log_lines: vec![Line::from("hello")],
            num_visible: 1,
            visible_height: 10,
            start: 0,
            horizontal_scroll: 0,
            logs_title: String::new(),
            show_borders: false,
            show_tab_bar: false,
            wrap: false,
            theme_border: Color::Gray,
            theme_border_title: Color::White,
            extraction_progress: None,
            archive_name: String::new(),
        };
        let mut terminal = Terminal::new(TestBackend::new(80, 10)).unwrap();
        terminal
            .draw(|f| f.render_widget(LogPanel { data: &data }, f.area()))
            .unwrap();
    }

    #[test]
    fn test_log_panel_widget_borders_with_tab_bar() {
        let data = LogPanelData {
            log_lines: vec![Line::from("content")],
            num_visible: 5,
            visible_height: 4,
            start: 1,
            horizontal_scroll: 0,
            logs_title: String::new(),
            show_borders: true,
            show_tab_bar: true,
            wrap: false,
            theme_border: Color::Gray,
            theme_border_title: Color::White,
            extraction_progress: None,
            archive_name: String::new(),
        };
        let mut terminal = Terminal::new(TestBackend::new(80, 10)).unwrap();
        terminal
            .draw(|f| f.render_widget(LogPanel { data: &data }, f.area()))
            .unwrap();
    }

    #[test]
    fn test_log_panel_widget_with_title_no_borders() {
        let data = LogPanelData {
            log_lines: vec![],
            num_visible: 0,
            visible_height: 10,
            start: 0,
            horizontal_scroll: 0,
            logs_title: "myfile.log (0)".to_string(),
            show_borders: false,
            show_tab_bar: false,
            wrap: true,
            theme_border: Color::Gray,
            theme_border_title: Color::White,
            extraction_progress: None,
            archive_name: String::new(),
        };
        let mut terminal = Terminal::new(TestBackend::new(80, 10)).unwrap();
        terminal
            .draw(|f| f.render_widget(LogPanel { data: &data }, f.area()))
            .unwrap();
    }

    #[test]
    fn test_log_panel_widget_borders_with_title() {
        let data = LogPanelData {
            log_lines: vec![Line::from("line a"), Line::from("line b")],
            num_visible: 10,
            visible_height: 5,
            start: 2,
            horizontal_scroll: 5,
            logs_title: "app.log (10)".to_string(),
            show_borders: true,
            show_tab_bar: false,
            wrap: false,
            theme_border: Color::Gray,
            theme_border_title: Color::Cyan,
            extraction_progress: None,
            archive_name: String::new(),
        };
        let mut terminal = Terminal::new(TestBackend::new(80, 10)).unwrap();
        terminal
            .draw(|f| f.render_widget(LogPanel { data: &data }, f.area()))
            .unwrap();
    }

    #[tokio::test]
    async fn test_log_panel_with_comment_banner() {
        let mut app = make_app(&["line one", "line two", "line three"]).await;
        app.tabs[0]
            .comment_manager
            .add("my annotation".to_string(), vec![0]);
        let mut terminal = make_terminal();
        terminal.draw(|f| app.ui(f)).unwrap();
    }

    #[test]
    fn test_prepare_comment_maps_empty() {
        let comments: Vec<(Vec<usize>, String)> = vec![];
        let visible = crate::ui::VisibleLines::default();
        let (banner_at, vis_comment_map) = prepare_comment_maps(&comments, &visible, 0, 0);
        assert!(banner_at.is_empty());
        assert!(vis_comment_map.is_empty());
    }

    #[tokio::test]
    async fn test_log_panel_visual_selection() {
        let mut app = make_app(&["line one", "line two", "line three"]).await;
        app.tabs[0].scroll.scroll_offset = 1;
        let mut terminal = make_terminal();
        terminal.draw(|f| app.ui(f)).unwrap();
    }

    #[tokio::test]
    async fn test_log_panel_with_wrap_and_long_json() {
        let long_msg = "x".repeat(100);
        let line = format!(
            r#"{{"level":"INFO","msg":"{}","target":"myapp"}}"#,
            long_msg
        );
        let mut app = make_app(&[&line]).await;
        app.tabs[0].display.wrap = true;
        let mut terminal = make_terminal();
        terminal.draw(|f| app.ui(f)).unwrap();
    }

    #[tokio::test]
    async fn test_log_panel_raw_mode_no_format() {
        let mut app = make_app(&[r#"{"level":"INFO","msg":"hello"}"#, "plain line"]).await;
        app.tabs[0].display.raw_mode = true;
        let mut terminal = make_terminal();
        terminal.draw(|f| app.ui(f)).unwrap();
    }

    #[tokio::test]
    async fn test_log_panel_all_level_colors_disabled() {
        let mut app = make_app(&[
            "TRACE low",
            "DEBUG detail",
            "INFO info",
            "NOTICE note",
            "WARNING caution",
            "ERROR bad",
            "FATAL critical",
        ])
        .await;
        for level in &[
            "trace", "debug", "info", "notice", "warning", "error", "fatal",
        ] {
            app.tabs[0]
                .display
                .level_colors_disabled
                .insert(level.to_string());
        }
        let mut terminal = make_terminal();
        terminal.draw(|f| app.ui(f)).unwrap();
    }

    #[tokio::test]
    async fn test_log_panel_with_many_lines_scroll_offset() {
        let lines: Vec<String> = (0..50).map(|i| format!("line {}", i)).collect();
        let lines_ref: Vec<&str> = lines.iter().map(|s| s.as_str()).collect();
        let mut app = make_app(&lines_ref).await;
        app.tabs[0].scroll.scroll_offset = 40;
        app.tabs[0].scroll.viewport_offset = 38;
        let mut terminal = make_terminal();
        terminal.draw(|f| app.ui(f)).unwrap();
    }

    #[tokio::test]
    async fn test_log_panel_show_tab_bar_suppresses_title() {
        let mut app = make_app(&["line one"]).await;
        app.tabs[0].log_manager =
            crate::db::LogManager::new(app.db.clone(), Some("test.log".to_string())).await;
        let mut terminal = make_terminal();
        terminal.draw(|f| app.ui(f)).unwrap();
    }

    #[tokio::test]
    async fn test_log_panel_comment_banner_with_line_numbers() {
        let mut app = make_app(&["line one", "line two", "line three"]).await;
        app.tabs[0]
            .comment_manager
            .add("note\ncontinuation".to_string(), vec![1]);
        app.tabs[0].display.show_line_numbers = true;
        let mut terminal = make_terminal();
        terminal.draw(|f| app.ui(f)).unwrap();
    }

    #[tokio::test]
    async fn test_log_panel_json_with_hidden_fields() {
        let mut app = make_app(&[r#"{"level":"INFO","msg":"hello","target":"app"}"#]).await;
        app.tabs[0]
            .display
            .hidden_fields
            .insert("level".to_string());
        let mut terminal = make_terminal();
        terminal.draw(|f| app.ui(f)).unwrap();
    }

    #[tokio::test]
    async fn test_log_panel_tail_mode_label() {
        let mut app = make_app(&["line one", "line two"]).await;
        app.tabs[0].stream.tail_mode = true;
        let mut terminal = make_terminal();
        terminal.draw(|f| app.ui(f)).unwrap();
    }

    #[tokio::test]
    async fn test_log_panel_paused_label() {
        let mut app = make_app(&["line one", "line two"]).await;
        app.tabs[0].stream.paused = true;
        let mut terminal = make_terminal();
        terminal.draw(|f| app.ui(f)).unwrap();
    }

    #[test]
    fn test_find_token_offset_partial_match() {
        assert_eq!(find_token_offset("helloworld foo", "world"), None);
        assert_eq!(find_token_offset("world hello", "world"), Some(0));
    }

    #[test]
    fn test_prepare_comment_maps_with_comments() {
        let comments = vec![(vec![1usize, 2usize], "note".to_string())];
        let visible = crate::ui::VisibleLines::Filtered(vec![0, 1, 2]);
        let (banner_at, vis_comment_map) = prepare_comment_maps(&comments, &visible, 0, 3);
        assert!(banner_at.contains_key(&1));
        assert!(vis_comment_map.contains_key(&1));
        assert!(vis_comment_map.contains_key(&2));
        assert!(!banner_at.contains_key(&2));
    }

    #[test]
    fn test_log_panel_widget_wrap_mode() {
        let data = LogPanelData {
            log_lines: vec![Line::from("a very long line that wraps")],
            num_visible: 1,
            visible_height: 5,
            start: 0,
            horizontal_scroll: 0,
            logs_title: String::new(),
            show_borders: false,
            show_tab_bar: false,
            wrap: true,
            theme_border: Color::Gray,
            theme_border_title: Color::White,
            extraction_progress: None,
            archive_name: String::new(),
        };
        let mut terminal = Terminal::new(TestBackend::new(20, 10)).unwrap();
        terminal
            .draw(|f| f.render_widget(LogPanel { data: &data }, f.area()))
            .unwrap();
    }

    #[test]
    fn test_log_panel_renders_extraction_progress_overlay() {
        let backend = TestBackend::new(40, 10);
        let mut terminal = Terminal::new(backend).unwrap();
        let data = LogPanelData {
            log_lines: vec![],
            num_visible: 0,
            visible_height: 8,
            start: 0,
            horizontal_scroll: 0,
            logs_title: String::new(),
            show_borders: false,
            show_tab_bar: false,
            wrap: false,
            theme_border: ratatui::style::Color::White,
            theme_border_title: ratatui::style::Color::White,
            extraction_progress: Some(0.5),
            archive_name: "logs.tar.gz".to_string(),
        };
        terminal
            .draw(|frame| {
                frame.render_widget(LogPanel { data: &data }, frame.area());
            })
            .unwrap();
        let rendered = terminal.backend().buffer().clone();
        let has_progress = rendered
            .content()
            .iter()
            .any(|c| c.symbol() == "\u{2588}" || c.symbol() == "\u{2591}");
        assert!(
            has_progress,
            "expected progress bar characters in rendered output"
        );
    }
}