bmrk 0.3.0

A fast TUI for directory navigation and bookmark management
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
// Allow many arguments for UI render functions
#![allow(clippy::too_many_arguments)]

use crate::bookmarks::Bookmarks;
use crate::config::Config;
use crate::disks::Disks;
use crate::navigation::Navigation;
use crate::search::Search;
use ratatui::{
    layout::{Constraint, Direction, Layout},
    style::{Color, Modifier, Style},
    text::{Line, Span},
    widgets::{List, ListItem, ListState, Paragraph},
    Frame,
};
fn truncate_name_middle(name: &str, max_len: usize) -> String {
    if max_len == 0 {
        return name.to_string();
    }
    let char_count = name.chars().count();
    if char_count <= max_len || max_len < 4 {
        return name.to_string();
    }
    let available = max_len - 3;
    let first_len = available.div_ceil(2);
    let last_len = available - first_len;
    let first: String = name.chars().take(first_len).collect();
    let last: String = name
        .chars()
        .rev()
        .take(last_len)
        .collect::<String>()
        .chars()
        .rev()
        .collect();
    format!("{}...{}", first, last)
}

/// Truncate `s` to its last `max_len` characters, prefixed with `...` if truncated.
/// Operates on characters, not bytes, so it never panics on multi-byte UTF-8 input.
fn truncate_start(s: &str, max_len: usize) -> String {
    let char_count = s.chars().count();
    if char_count <= max_len {
        return s.to_string();
    }
    let tail: String = s
        .chars()
        .rev()
        .take(max_len)
        .collect::<String>()
        .chars()
        .rev()
        .collect();
    format!("...{}", tail)
}

/// Word-wrap a comma-separated bookmark key string to `width` columns.
/// Returns lines joined with `\n`; the first line has a leading space.
fn wrap_bookmark_keys(keys_str: &str, width: usize) -> String {
    if keys_str.is_empty() || width == 0 {
        return String::new();
    }
    let mut lines: Vec<String> = Vec::new();
    let mut current = String::from(" ");
    for (i, word) in keys_str.split_ascii_whitespace().enumerate() {
        if i == 0 {
            current.push_str(word);
        } else if current.len() + 1 + word.len() <= width {
            current.push(' ');
            current.push_str(word);
        } else {
            lines.push(std::mem::take(&mut current));
            current.push_str(word);
        }
    }
    if !current.is_empty() {
        lines.push(current);
    }
    lines.join("\n")
}

/// UI rendering state
pub struct UI {
    pub tree_area_start: u16,
    pub tree_area_end: u16,
    pub tree_area_top: u16,
    pub tree_area_height: u16,
    /// Absolute screen row of the first tree item (no border in compact mode).
    pub tree_item_top: u16,
    pub terminal_width: u16,
    pub terminal_height: u16,
    pub tree_scroll_offset: usize,
    /// Scroll offset of the disk list, updated each render when disk selection is active.
    pub disk_scroll_offset: usize,
    pub bottom_panel_height: u16,
    /// Visible row count of the bookmark list in creation mode, set each render.
    pub bookmark_panel_height: usize,
    /// Scroll offset of the bookmark selection list, updated each render.
    pub bookmark_scroll_offset: usize,
    /// Scroll offset of the search results list, updated each render.
    pub search_scroll_offset: usize,
}

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

impl UI {
    pub fn new() -> Self {
        Self {
            tree_area_start: 0,
            tree_area_end: 0,
            tree_area_top: 0,
            tree_area_height: 0,
            tree_item_top: 0,
            terminal_width: 0,
            terminal_height: 0,
            tree_scroll_offset: 0,
            disk_scroll_offset: 0,
            bottom_panel_height: 0,
            bookmark_panel_height: 0,
            bookmark_scroll_offset: 0,
            search_scroll_offset: 0,
        }
    }

    /// Render the compact inline viewport (COMPACT_HEIGHT rows, no alternate screen).
    /// Layout:
    ///   Row 0   — header: root path + key hints (adapts to mode)
    ///   Rows 1+ — tree items, or bookmark/search/disk list when active
    ///   Last row — search bar / bookmark input (only when active)
    pub fn render_compact(
        &mut self,
        frame: &mut Frame,
        nav: &Navigation,
        search: &Search,
        bookmarks: &Bookmarks,
        disks: &Disks,
        config: &Config,
    ) {
        let area = frame.area();
        self.terminal_width = area.width;
        self.terminal_height = area.height;

        let selected_color =
            Config::parse_color(Config::get_color(&config.appearance.colors.selected_color));

        // Reserve the last row: search bar, bookmark input, or nothing
        let (content_area, search_bar_area, bookmark_input_area) = if search.mode {
            let chunks = Layout::default()
                .direction(Direction::Vertical)
                .constraints([Constraint::Min(2), Constraint::Length(1)])
                .split(area);
            (chunks[0], Some(chunks[1]), None)
        } else if bookmarks.is_creating {
            let header_rows: u16 = 1;
            let input_rows: u16 = 1;
            let available = area.height.saturating_sub(header_rows + input_rows);
            let body_rows: u16 = if bookmarks.list().is_empty() {
                0
            } else {
                let keys = bookmarks
                    .list()
                    .iter()
                    .map(|b| b.key.as_str())
                    .collect::<Vec<_>>()
                    .join(", ");
                let wrap_width = if config.appearance.max_name_length > 0 {
                    config.appearance.max_name_length
                } else {
                    area.width as usize
                };
                let wrapped = wrap_bookmark_keys(&keys, wrap_width);
                let line_count = wrapped.lines().count().max(1);
                (line_count as u16).min(available).max(1)
            };
            let content_height = header_rows + body_rows;
            let chunks = Layout::default()
                .direction(Direction::Vertical)
                .constraints([
                    Constraint::Length(content_height),
                    Constraint::Length(input_rows),
                    Constraint::Min(0),
                ])
                .split(area);
            (chunks[0], None, Some(chunks[1]))
        } else {
            (area, None, None)
        };

        // Split content into header (1 row) + body (rest)
        let chunks = Layout::default()
            .direction(Direction::Vertical)
            .constraints([Constraint::Length(1), Constraint::Min(0)])
            .split(content_area);

        let header_area = chunks[0];
        let body_area = chunks[1];

        // Store body area geometry for mouse handling
        self.tree_area_top = body_area.y;
        self.tree_area_height = body_area.height;
        self.tree_area_start = body_area.x;
        self.tree_area_end = body_area.x + body_area.width;
        self.tree_item_top = body_area.y;

        // --- Header ---
        let root_path = nav.root.borrow().path.display().to_string();
        let header_path_color = Config::parse_color(Config::get_color(
            &config.appearance.colors.header_path_color,
        ));
        let header_hints_color = Config::parse_color(Config::get_color(
            &config.appearance.colors.header_hints_color,
        ));

        if disks.is_selecting {
            let hints = "  jk:select  Enter:go  Esc:cancel";
            let label = if disks.disks.is_empty() {
                "Disks (none found)".to_string()
            } else {
                format!("Disks ({}/{})", disks.selected_index + 1, disks.disks.len())
            };
            let max_label_len = (area.width as usize).saturating_sub(hints.len() + 4).max(8);
            let label_display = truncate_start(&label, max_label_len);
            frame.render_widget(
                Paragraph::new(Line::from(vec![
                    Span::styled(" > ", Style::default().fg(Color::Yellow)),
                    Span::styled(
                        label_display,
                        Style::default()
                            .fg(header_path_color)
                            .add_modifier(Modifier::BOLD),
                    ),
                    Span::styled(hints, Style::default().fg(header_hints_color)),
                ])),
                header_area,
            );
        } else if bookmarks.is_selecting {
            let hints = if bookmarks.filter_mode {
                "  jk:select  Enter:jump  Tab:nav  Esc:cancel"
            } else {
                "  jk:select  Enter:jump  q:exit  Tab:filter  dd:del  Esc:cancel"
            };
            let error_color =
                Config::parse_color(Config::get_color(&config.appearance.colors.error_color));
            if let Some(ref err) = bookmarks.bookmark_error {
                let max_msg_len = (area.width as usize).saturating_sub(hints.len() + 4).max(8);
                let msg_display = truncate_start(err, max_msg_len);
                let error_icon = if config.appearance.icons != "ascii" {
                    ""
                } else {
                    " ! "
                };
                frame.render_widget(
                    Paragraph::new(Line::from(vec![
                        Span::styled(error_icon, Style::default().fg(error_color)),
                        Span::styled(
                            msg_display,
                            Style::default()
                                .fg(error_color)
                                .add_modifier(Modifier::BOLD),
                        ),
                        Span::styled(hints, Style::default().fg(header_hints_color)),
                    ])),
                    header_area,
                );
            } else {
                let filtered = bookmarks.get_filtered_bookmarks();
                let label = if bookmarks.filter_mode {
                    format!("Filter: {}", bookmarks.get_input())
                } else if filtered.is_empty() {
                    "Bookmarks (empty)".to_string()
                } else {
                    format!(
                        "Bookmarks ({}/{})",
                        bookmarks.selected_index + 1,
                        filtered.len()
                    )
                };
                let max_label_len = (area.width as usize).saturating_sub(hints.len() + 4).max(8);
                let label_display = truncate_start(&label, max_label_len);
                frame.render_widget(
                    Paragraph::new(Line::from(vec![
                        Span::styled(" > ", Style::default().fg(Color::Yellow)),
                        Span::styled(
                            label_display,
                            Style::default()
                                .fg(header_path_color)
                                .add_modifier(Modifier::BOLD),
                        ),
                        Span::styled(hints, Style::default().fg(header_hints_color)),
                    ])),
                    header_area,
                );
            }
        } else if search.show_results && search.focus_on_results {
            let hints = if search.is_searching {
                "  jk:select  Enter:jump  q:exit  Esc:cancel"
            } else {
                "  jk:select  Enter:jump  q:exit  Esc:close"
            };
            let label = if search.is_searching {
                format!(
                    "Search: {} found | Scanning {} dirs...",
                    search.results.len(),
                    search.scanned_count
                )
            } else if search.results.is_empty() {
                "Search: no results".to_string()
            } else {
                format!("Search: {}/{}", search.selected + 1, search.results.len())
            };
            let max_label_len = (area.width as usize).saturating_sub(hints.len() + 4).max(8);
            let label_display = truncate_start(&label, max_label_len);
            frame.render_widget(
                Paragraph::new(Line::from(vec![
                    Span::styled(" * ", Style::default().fg(Color::Yellow)),
                    Span::styled(
                        label_display,
                        Style::default()
                            .fg(header_path_color)
                            .add_modifier(Modifier::BOLD),
                    ),
                    Span::styled(hints, Style::default().fg(header_hints_color)),
                ])),
                header_area,
            );
        } else if let Some(ref error_msg) = nav.nav_error {
            let hints = "  hjkl:nav  u:up  m:bmark  ':jump  d:disk  /:search  q:exit";
            let max_msg_len = (area.width as usize).saturating_sub(hints.len() + 4).max(8);
            let msg_display = truncate_start(error_msg, max_msg_len);
            let error_icon = if config.appearance.icons != "ascii" {
                ""
            } else {
                " ! "
            };
            let error_color =
                Config::parse_color(Config::get_color(&config.appearance.colors.error_color));
            frame.render_widget(
                Paragraph::new(Line::from(vec![
                    Span::styled(error_icon, Style::default().fg(error_color)),
                    Span::styled(
                        msg_display,
                        Style::default()
                            .fg(error_color)
                            .add_modifier(Modifier::BOLD),
                    ),
                    Span::styled(hints, Style::default().fg(header_hints_color)),
                ])),
                header_area,
            );
        } else if bookmarks.is_creating && bookmarks.bookmark_error.is_some() {
            let err = bookmarks.bookmark_error.as_deref().unwrap_or_default();
            let hints = "  Enter:save  Ctrl+j/k:scroll  Esc:cancel";
            let max_msg_len = (area.width as usize).saturating_sub(hints.len() + 4).max(8);
            let msg_display = truncate_start(err, max_msg_len);
            let error_icon = if config.appearance.icons != "ascii" {
                ""
            } else {
                " ! "
            };
            let error_color =
                Config::parse_color(Config::get_color(&config.appearance.colors.error_color));
            frame.render_widget(
                Paragraph::new(Line::from(vec![
                    Span::styled(error_icon, Style::default().fg(error_color)),
                    Span::styled(
                        msg_display,
                        Style::default()
                            .fg(error_color)
                            .add_modifier(Modifier::BOLD),
                    ),
                    Span::styled(hints, Style::default().fg(header_hints_color)),
                ])),
                header_area,
            );
        } else {
            let hints = if bookmarks.is_creating {
                "  Enter:save  Ctrl+j/k:scroll  Esc:cancel"
            } else {
                "  hjkl:nav  u:up  m:bmark  ':jump  d:disk  /:search  q:exit"
            };
            let total = nav.flat_list.len();
            let count_str = if total > 0 {
                format!(" ({}/{})", nav.selected + 1, total)
            } else {
                String::new()
            };
            let active_path = if bookmarks.is_creating {
                nav.get_selected_node()
                    .map(|n| n.borrow().path.display().to_string())
                    .unwrap_or(root_path)
            } else {
                root_path
            };
            let max_path_len = (area.width as usize)
                .saturating_sub(hints.len() + count_str.len() + 4)
                .max(8);
            let path_display = truncate_start(&active_path, max_path_len);
            let header_icon = if config.appearance.icons != "ascii" {
                ""
            } else {
                " v "
            };
            frame.render_widget(
                Paragraph::new(Line::from(vec![
                    Span::styled(
                        header_icon,
                        Style::default().fg(Config::parse_color(Config::get_color(
                            &config.appearance.colors.directory_color,
                        ))),
                    ),
                    Span::styled(
                        path_display,
                        Style::default()
                            .fg(header_path_color)
                            .add_modifier(Modifier::BOLD),
                    ),
                    Span::styled(
                        count_str,
                        Style::default()
                            .fg(header_path_color)
                            .add_modifier(Modifier::BOLD),
                    ),
                    Span::styled(hints, Style::default().fg(header_hints_color)),
                ])),
                header_area,
            );
        }

        // --- Body ---
        if disks.is_selecting {
            let file_color =
                Config::parse_color(Config::get_color(&config.appearance.colors.file_color));
            let cursor_color_str = Config::get_color(&config.appearance.colors.cursor_color);
            let highlight_style = if cursor_color_str.to_lowercase() == "dim" {
                Style::default().add_modifier(Modifier::DIM)
            } else {
                Style::default()
                    .fg(Config::parse_color(cursor_color_str))
                    .add_modifier(Modifier::BOLD)
            };

            if disks.disks.is_empty() {
                frame.render_widget(
                    Paragraph::new("No disks found.").style(Style::default().fg(Color::DarkGray)),
                    body_area,
                );
            } else {
                let items: Vec<ListItem> = disks
                    .disks
                    .iter()
                    .map(|d| ListItem::new(d.display_line()).style(Style::default().fg(file_color)))
                    .collect();

                let mut state = ListState::default();
                state.select(Some(disks.selected_index));

                let visible = body_area.height as usize;
                let total = disks.disks.len();
                let offset = if disks.center_selection {
                    if disks.selected_index < visible / 2 {
                        0
                    } else if disks.selected_index >= total.saturating_sub(visible / 2) {
                        total.saturating_sub(visible)
                    } else {
                        disks.selected_index.saturating_sub(visible / 2)
                    }
                } else {
                    let current = self.disk_scroll_offset;
                    if disks.selected_index < current {
                        disks.selected_index
                    } else if visible > 0 && disks.selected_index >= current + visible {
                        disks.selected_index.saturating_sub(visible - 1)
                    } else {
                        current
                    }
                };
                *state.offset_mut() = offset;
                self.disk_scroll_offset = offset;

                frame.render_stateful_widget(
                    List::new(items)
                        .highlight_style(highlight_style)
                        .highlight_symbol(">> "),
                    body_area,
                    &mut state,
                );
            }
        } else if bookmarks.is_selecting {
            let filtered = bookmarks.get_filtered_bookmarks();
            let file_color =
                Config::parse_color(Config::get_color(&config.appearance.colors.file_color));
            let error_color =
                Config::parse_color(Config::get_color(&config.appearance.colors.error_color));

            if filtered.is_empty() {
                let msg = if bookmarks.filter_mode {
                    format!("No bookmarks match '{}'", bookmarks.get_input())
                } else {
                    "No bookmarks. Press 'm' to create one.".to_string()
                };
                frame.render_widget(
                    Paragraph::new(msg).style(Style::default().fg(Color::DarkGray)),
                    body_area,
                );
            } else {
                let items: Vec<ListItem> = filtered
                    .iter()
                    .enumerate()
                    .map(|(idx, bookmark)| {
                        let name = bookmark.name.as_deref().unwrap_or("(unnamed)");
                        let path_str = bookmark.path.display().to_string();
                        let is_marked = bookmarks.pending_deletion_index == Some(idx);
                        let text = if is_marked {
                            format!("[DEL] {:<10} {} ({})", bookmark.key, name, path_str)
                        } else {
                            format!("{:<10} {} ({})", bookmark.key, name, path_str)
                        };
                        let style = if is_marked {
                            Style::default().fg(error_color)
                        } else {
                            Style::default().fg(file_color)
                        };
                        ListItem::new(text).style(style)
                    })
                    .collect();

                let mut state = ListState::default();
                state.select(Some(bookmarks.selected_index));

                let visible = body_area.height as usize;
                let total = filtered.len();
                let offset = if bookmarks.center_selection {
                    if bookmarks.selected_index < visible / 2 {
                        0
                    } else if bookmarks.selected_index >= total.saturating_sub(visible / 2) {
                        total.saturating_sub(visible)
                    } else {
                        bookmarks.selected_index.saturating_sub(visible / 2)
                    }
                } else {
                    let current = self.bookmark_scroll_offset;
                    if bookmarks.selected_index < current {
                        bookmarks.selected_index
                    } else if visible > 0 && bookmarks.selected_index >= current + visible {
                        bookmarks.selected_index.saturating_sub(visible - 1)
                    } else {
                        current
                    }
                };
                *state.offset_mut() = offset;
                self.bookmark_scroll_offset = offset;

                let cursor_color_str = Config::get_color(&config.appearance.colors.cursor_color);
                let highlight_style = if cursor_color_str.to_lowercase() == "dim" {
                    Style::default().add_modifier(Modifier::DIM)
                } else {
                    Style::default()
                        .fg(Config::parse_color(cursor_color_str))
                        .add_modifier(Modifier::BOLD)
                };

                frame.render_stateful_widget(
                    List::new(items)
                        .highlight_style(highlight_style)
                        .highlight_symbol(">> "),
                    body_area,
                    &mut state,
                );
            }
        } else if bookmarks.is_creating {
            self.bookmark_panel_height = body_area.height as usize;
            let all_bookmarks = bookmarks.list();
            let file_color =
                Config::parse_color(Config::get_color(&config.appearance.colors.file_color));

            if !all_bookmarks.is_empty() {
                let keys = all_bookmarks
                    .iter()
                    .map(|b| b.key.as_str())
                    .collect::<Vec<_>>()
                    .join(", ");
                let wrap_width = if config.appearance.max_name_length > 0 {
                    config.appearance.max_name_length
                } else {
                    area.width as usize
                };
                let wrapped = wrap_bookmark_keys(&keys, wrap_width);
                frame.render_widget(
                    Paragraph::new(wrapped).style(Style::default().fg(file_color)),
                    body_area,
                );
            }
        } else if search.show_results && search.focus_on_results {
            // Inline search results
            let dir_color =
                Config::parse_color(Config::get_color(&config.appearance.colors.directory_color));
            let file_color =
                Config::parse_color(Config::get_color(&config.appearance.colors.file_color));
            let highlight_color =
                Config::parse_color(Config::get_color(&config.appearance.colors.highlight_color));

            if search.results.is_empty() {
                let msg = if search.is_searching {
                    "Searching..."
                } else {
                    "No results found"
                };
                frame.render_widget(
                    Paragraph::new(msg).style(Style::default().fg(Color::DarkGray)),
                    body_area,
                );
            } else {
                let root_path_buf = nav.root.borrow().path.clone();
                let root_parent = root_path_buf
                    .parent()
                    .unwrap_or(&root_path_buf)
                    .to_path_buf();

                let items: Vec<ListItem> = search
                    .results
                    .iter()
                    .map(|result| {
                        let display_path = result
                            .path
                            .strip_prefix(&root_parent)
                            .unwrap_or(&result.path)
                            .display()
                            .to_string();

                        let base_color = if result.is_dir { dir_color } else { file_color };

                        if let (true, Some(indices)) =
                            (search.fuzzy_mode, result.match_indices.as_ref())
                        {
                            // match_indices are character offsets into the file/dir name only,
                            // so they must be shifted by the directory prefix's char length
                            // before being applied to display_path (which includes that prefix).
                            let chars: Vec<char> = display_path.chars().collect();
                            let name_offset = display_path
                                .rfind(std::path::MAIN_SEPARATOR)
                                .map(|byte_idx| display_path[..=byte_idx].chars().count())
                                .unwrap_or(0);
                            let mut spans = Vec::new();
                            let mut last_idx = 0;
                            for &raw_idx in indices {
                                let match_idx = (raw_idx + name_offset).min(chars.len());
                                if match_idx > last_idx {
                                    let text: String = chars[last_idx..match_idx].iter().collect();
                                    spans.push(Span::styled(text, Style::default().fg(base_color)));
                                }
                                if match_idx < chars.len() {
                                    let text: String =
                                        chars[match_idx..match_idx + 1].iter().collect();
                                    spans.push(Span::styled(
                                        text,
                                        Style::default()
                                            .fg(highlight_color)
                                            .add_modifier(Modifier::BOLD),
                                    ));
                                }
                                last_idx = match_idx + 1;
                            }
                            if last_idx < chars.len() {
                                let text: String = chars[last_idx..].iter().collect();
                                spans.push(Span::styled(text, Style::default().fg(base_color)));
                            }
                            ListItem::new(Line::from(spans))
                        } else {
                            ListItem::new(display_path).style(Style::default().fg(base_color))
                        }
                    })
                    .collect();

                let mut state = ListState::default();
                state.select(Some(search.selected));

                let visible = body_area.height as usize;
                let total = search.results.len();
                let offset = if search.center_selection {
                    if search.selected < visible / 2 {
                        0
                    } else if search.selected >= total.saturating_sub(visible / 2) {
                        total.saturating_sub(visible)
                    } else {
                        search.selected.saturating_sub(visible / 2)
                    }
                } else {
                    let current = self.search_scroll_offset;
                    if search.selected < current {
                        search.selected
                    } else if visible > 0 && search.selected >= current + visible {
                        search.selected.saturating_sub(visible - 1)
                    } else {
                        current
                    }
                };
                *state.offset_mut() = offset;
                self.search_scroll_offset = offset;

                let cursor_color_str = Config::get_color(&config.appearance.colors.cursor_color);
                let highlight_style = if cursor_color_str.to_lowercase() == "dim" {
                    Style::default().add_modifier(Modifier::DIM)
                } else {
                    Style::default()
                        .fg(Config::parse_color(cursor_color_str))
                        .add_modifier(Modifier::BOLD)
                };

                frame.render_stateful_widget(
                    List::new(items)
                        .highlight_style(highlight_style)
                        .highlight_symbol(">> "),
                    body_area,
                    &mut state,
                );
            }
        } else {
            // Normal tree items
            let dir_color =
                Config::parse_color(Config::get_color(&config.appearance.colors.directory_color));
            let error_color =
                Config::parse_color(Config::get_color(&config.appearance.colors.error_color));

            let items: Vec<ListItem> = nav
                .flat_list
                .iter()
                .map(|node| {
                    let n = node.borrow();
                    let indent = "  ".repeat(n.depth);
                    let unicode_icons = config.appearance.icons != "ascii";
                    let icon = if n.has_error {
                        if unicode_icons {
                            ""
                        } else {
                            "! "
                        }
                    } else if n.is_dir {
                        if n.is_expanded {
                            if unicode_icons {
                                ""
                            } else {
                                "v "
                            }
                        } else if n.has_children == Some(false) {
                            "  "
                        } else if unicode_icons {
                            ""
                        } else {
                            "> "
                        }
                    } else {
                        "  "
                    };
                    let display_name =
                        truncate_name_middle(&n.name, config.appearance.max_name_length);
                    let text = format!("{}{}{}", indent, icon, display_name);
                    let style = if n.has_error {
                        Style::default().fg(error_color)
                    } else if n.is_dir {
                        Style::default().fg(dir_color)
                    } else {
                        Style::default()
                    };
                    ListItem::new(text).style(style)
                })
                .collect();

            let mut state = ListState::default();
            state.select(Some(nav.selected));

            let visible = body_area.height as usize;
            let total = nav.flat_list.len();
            let offset = if nav.center_selection {
                // keyboard nav: keep selection centered in viewport
                if nav.selected < visible / 2 {
                    0
                } else if nav.selected >= total.saturating_sub(visible / 2) {
                    total.saturating_sub(visible)
                } else {
                    nav.selected.saturating_sub(visible / 2)
                }
            } else {
                // mouse action: minimal scroll — only move if selection left the visible area
                let current = self.tree_scroll_offset;
                if nav.selected < current {
                    nav.selected
                } else if visible > 0 && nav.selected >= current + visible {
                    nav.selected.saturating_sub(visible - 1)
                } else {
                    current
                }
            };
            *state.offset_mut() = offset;
            self.tree_scroll_offset = offset;

            let cursor_color_str = Config::get_color(&config.appearance.colors.tree_cursor_color);
            let cursor_bg_str = Config::get_color(&config.appearance.colors.tree_cursor_bg_color);

            let mut highlight_style = if cursor_color_str.to_lowercase() == "dim" {
                Style::default().add_modifier(Modifier::DIM)
            } else {
                Style::default().fg(Config::parse_color(cursor_color_str))
            };
            if cursor_bg_str.to_lowercase() != "dim" {
                highlight_style = highlight_style.bg(Config::parse_color(cursor_bg_str));
            }

            frame.render_stateful_widget(
                List::new(items)
                    .highlight_style(highlight_style)
                    .highlight_symbol(">> "),
                body_area,
                &mut state,
            );
        }

        // Search bar (single line, no block)
        if let Some(bar_area) = search_bar_area {
            let mode_tag = if search.fuzzy_mode { "(fuzzy) " } else { "" };
            let bar_text = format!("/{}{}", mode_tag, search.query);
            frame.render_widget(
                Paragraph::new(bar_text).style(Style::default().fg(selected_color)),
                bar_area,
            );
        }

        // Bookmark input bar (single line, no block)
        if let Some(input_area) = bookmark_input_area {
            let input_text = format!(" Add bookmark: {}|", bookmarks.get_input());
            frame.render_widget(
                Paragraph::new(input_text).style(
                    Style::default()
                        .fg(selected_color)
                        .add_modifier(Modifier::BOLD),
                ),
                input_area,
            );
        }
    }
}

/// Load help content from HELP.txt (embedded at compile time)
pub fn get_help_content() -> Vec<String> {
    const HELP_TEXT: &str = include_str!("../HELP.txt");
    HELP_TEXT.lines().map(|line| line.to_string()).collect()
}