git-plumber 0.1.3

Explore git internals, the plumbing
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
use ratatui::buffer::Buffer;
use ratatui::layout::{Constraint, Direction, Layout};

fn apply_git_tree_highlight_fx(
    buf: &mut Buffer,
    area: ratatui::layout::Rect,
    state: &crate::tui::main_view::MainViewState,
    reduced: bool,
    now: std::time::Instant,
) {
    let (hold_ms, shrink_ms) = if reduced {
        (5000_u64, 0_u64)
    } else {
        (1000_u64, 4000_u64)
    };
    let total = hold_ms + shrink_ms;

    // Use inner content area (exclude borders) so row indexing matches rendered list rows
    let start_row = area.y.saturating_add(1);
    let end_row = area.y.saturating_add(area.height.saturating_sub(1));
    let start_col = area.x.saturating_add(1);
    let width = area.width.saturating_sub(2);

    for (row_idx, y) in (start_row..end_row).enumerate() {
        let idx = state.git_objects.scroll_position + row_idx;
        if idx >= state.git_objects.flat_view.len() {
            continue;
        }
        let (_depth, obj, status) = &state.git_objects.flat_view[idx];
        let key = crate::tui::main_view::MainViewState::selection_key(obj);

        let mut color: Option<ratatui::style::Color> = None;
        let mut start: Option<std::time::Instant> = None;

        if let Some(until) = state.changed_keys.get(&key).copied()
            && until > now
        {
            color = Some(ratatui::style::Color::Green);
            start = Some(until - std::time::Duration::from_millis(total));
        }
        // Check for modifications (orange) - lower priority than new files
        if color.is_none()
            && let Some(until) = state.modified_keys.get(&key).copied()
            && until > now
        {
            color = Some(ratatui::style::Color::Rgb(255, 165, 0)); // Orange
            start = Some(until - std::time::Duration::from_millis(total));
        }
        if matches!(status, crate::tui::main_view::RenderStatus::PendingRemoval)
            && let Some(g) = state.ghosts.get(&key)
            && g.until > now
        {
            color = Some(ratatui::style::Color::Red);
            start = Some(g.until - std::time::Duration::from_millis(total));
        }

        let (bg, start_at) = match (color, start) {
            (Some(c), Some(s)) => (c, s),
            _ => continue,
        };

        let n_cols: u16 = if reduced {
            if now.duration_since(start_at).as_millis() as u64 <= hold_ms {
                width
            } else {
                0
            }
        } else {
            let elapsed = now.saturating_duration_since(start_at);
            if elapsed.as_millis() as u64 <= hold_ms {
                width
            } else {
                let after = elapsed - std::time::Duration::from_millis(hold_ms);
                if after.as_millis() as u64 >= shrink_ms {
                    0
                } else {
                    let p = after.as_secs_f32() / (shrink_ms as f32 / 1000.0);
                    ((width as f32) * (1.0 - p)).ceil() as u16
                }
            }
        };

        if n_cols == 0 {
            continue;
        }

        let hi = n_cols.min(width);
        for dx in 0..hi {
            let x = start_col + dx;
            if let Some(cell) = buf.cell_mut((x, y)) {
                let s = cell.style().bg(bg);
                cell.set_style(s);
            }
        }
    }
}

use ratatui::style::{Color, Style};
use ratatui::text::Span;
use ratatui::widgets::{Block, Borders, ListItem, Paragraph};
use std::time::Instant;

use super::model::{MainViewState, PackFocus, RegularFocus};
use super::{PackPreViewState, PreviewState, RegularPreViewState};
use crate::tui::helpers::{render_list_with_scrollbar, render_styled_paragraph_with_scrollbar};
use crate::tui::model::{AppState, AppView, GitObjectType};

pub fn render(f: &mut ratatui::Frame, app: &mut AppState, area: ratatui::layout::Rect) {
    let project_name = app.project_name.clone();
    let reduced = app.reduced_motion;
    if let AppView::Main { state } = &mut app.view {
        // Split main content into two blocks
        let content_chunks = Layout::default()
            .direction(Direction::Horizontal)
            .constraints(
                [
                    Constraint::Length(42), // 40 chars + 2 for borders
                    Constraint::Min(0),
                ]
                .as_ref(),
            )
            .split(area);

        render_git_tree(f, state, project_name, content_chunks[0], reduced);
        // Apply cell-based highlight after rendering the tree
        apply_git_tree_highlight_fx(
            f.buffer_mut(),
            content_chunks[0],
            state,
            reduced,
            Instant::now(),
        );
        match &state.preview_state {
            PreviewState::Regular(_) => {
                render_regular_preview_layout(f, state, &app.error, content_chunks[1])
            }
            PreviewState::Pack(_) => {
                render_pack_preview_layout(f, state, &app.error, content_chunks[1])
            }
        };
    }
}

fn render_regular_preview_layout(
    f: &mut ratatui::Frame,
    main_view: &mut MainViewState,
    app_error: &Option<String>,
    area: ratatui::layout::Rect,
) {
    if let PreviewState::Regular(preview_state) = &mut main_view.preview_state {
        // Split area into two vertical sections for consistent layout
        let content_chunks = Layout::default()
            .direction(Direction::Vertical)
            .constraints(
                [
                    Constraint::Length(6), // Height for object details
                    Constraint::Min(0),    // Remaining space for educational content
                ]
                .as_ref(),
            )
            .split(area);

        // Top block - Object details
        let object_info = if app_error.is_some() {
            app_error.as_ref().unwrap()
        } else if main_view.git_object_info.is_empty()
            && !main_view.git_objects.flat_view.is_empty()
        {
            "Select an object to view details"
        } else if main_view.git_objects.flat_view.is_empty() {
            "Loading repository…"
        } else {
            &main_view.git_object_info
        };

        let details_widget = Paragraph::new(object_info).block(
            Block::default()
                .title("Object Details")
                .borders(Borders::ALL),
        );
        f.render_widget(details_widget, content_chunks[0]);

        // Bottom block - Educational/Preview content or Pack Index widget
        if let Some(pack_index_widget) = &mut preview_state.pack_index_widget {
            // Render pack index widget
            pack_index_widget.render(
                f,
                content_chunks[1],
                matches!(preview_state.focus, RegularFocus::Preview),
            );
        } else {
            // Render regular educational content
            let bottom_title = if !main_view.git_objects.flat_view.is_empty()
                && main_view.git_objects.selected_index < main_view.git_objects.flat_view.len()
            {
                let selected_object =
                    &main_view.git_objects.flat_view[main_view.git_objects.selected_index].1;
                match &selected_object.obj_type {
                    GitObjectType::Category(_) => "Educational Content",
                    GitObjectType::FileSystemFolder { is_educational, .. } => {
                        if *is_educational {
                            "Educational Content"
                        } else {
                            "Directory Info"
                        }
                    }
                    GitObjectType::FileSystemFile { .. } => "File Info",
                    GitObjectType::PackFolder { .. } => "Pack Preview",
                    _ => "Object Preview",
                }
            } else {
                "Content"
            };

            render_styled_paragraph_with_scrollbar(
                f,
                content_chunks[1],
                main_view.educational_content.clone(),
                preview_state.preview_scroll_position,
                bottom_title,
                matches!(preview_state.focus, RegularFocus::Preview),
            );
        }
    }
}

pub fn render_pack_preview_layout(
    f: &mut ratatui::Frame,
    main_view: &mut MainViewState,
    app_error: &Option<String>,
    area: ratatui::layout::Rect,
) {
    if let PreviewState::Pack(_) = &main_view.preview_state {
        if area.width > 116 {
            let horizontal_chunks = Layout::default()
                .direction(Direction::Horizontal)
                .constraints([Constraint::Length(46), Constraint::Min(0)].as_ref())
                .split(area);

            let pack_file_details = horizontal_chunks[0];
            let object_details_area = horizontal_chunks[1];

            // Render main content in the left area
            render_pack_file_preview(f, main_view, app_error, pack_file_details, true);

            // Extract the data we need first
            if let PreviewState::Pack(pack_preview_state) = &mut main_view.preview_state {
                // Render pack detail in the right area only if pack_object_list is not empty
                if !pack_preview_state.pack_object_list.is_empty()
                    && pack_preview_state.selected_pack_object
                        < pack_preview_state.pack_object_list.len()
                {
                    pack_preview_state.pack_object_widget_state.render(
                        f,
                        object_details_area,
                        matches!(pack_preview_state.focus, PackFocus::PackObjectDetails),
                    );
                } else {
                    // Render empty state
                    let empty_widget = Paragraph::new("Loading pack objects...").block(
                        Block::default()
                            .title("Pack Object Detail")
                            .borders(Borders::ALL),
                    );
                    f.render_widget(empty_widget, object_details_area);
                }
            }
        } else {
            render_pack_file_preview(f, main_view, app_error, area, false);
        }
    }
}

fn render_pack_file_preview(
    f: &mut ratatui::Frame,
    main_view: &mut MainViewState,
    app_error: &Option<String>,
    area: ratatui::layout::Rect,
    is_widescreen: bool,
) {
    if let PreviewState::Pack(preview_state) = &main_view.preview_state {
        // Split area into three vertical sections for consistent layout
        let content_chunks = Layout::default()
            .direction(Direction::Vertical)
            .constraints(
                [
                    Constraint::Length(6),      // Height for object details
                    Constraint::Percentage(50), // Educational content
                    Constraint::Percentage(50), // Pack objects list
                ]
                .as_ref(),
            )
            .split(area);

        // Top block - Object details (same as PackPreview)
        let object_info = if app_error.is_some() {
            app_error.as_ref().unwrap()
        } else if main_view.git_object_info.is_empty()
            && !main_view.git_objects.flat_view.is_empty()
        {
            "Select an object to view details"
        } else if main_view.git_objects.flat_view.is_empty() {
            "Loading repository…"
        } else {
            &main_view.git_object_info
        };

        let details_widget = Paragraph::new(object_info).block(
            Block::default()
                .title("Object Details")
                .borders(Borders::ALL),
        );
        f.render_widget(details_widget, content_chunks[0]);

        // Middle block - Educational content with scrolling
        // Only highlight if in ObjectPreview mode and focus is Educational
        render_styled_paragraph_with_scrollbar(
            f,
            content_chunks[1],
            main_view.educational_content.clone(),
            preview_state.educational_scroll_position,
            "Pack File Header",
            matches!(preview_state.focus, PackFocus::Educational),
        );
        // Bottom block - Pack objects list
        // Only highlight if in ObjectPreview mode and focus is PackObjects
        if preview_state.pack_object_list.is_empty() {
            let loading = Paragraph::new("Loading pack objects...")
                .block(Block::default().title("Pack Objects").borders(Borders::ALL));
            f.render_widget(loading, content_chunks[2]);
        } else {
            let selected_index = Some(
                preview_state
                    .selected_pack_object
                    .min(preview_state.pack_object_list.len().saturating_sub(1)),
            );

            render_list_with_scrollbar(
                f,
                content_chunks[2],
                &preview_state.pack_object_list,
                selected_index,
                preview_state.pack_object_list_scroll_position,
                "Pack Objects",
                matches!(preview_state.focus, PackFocus::PackObjectsList),
                |_absolute_index, pack_obj, is_selected| {
                    let display_text = format!(
                        "{}: {} | {} bytes{}",
                        pack_obj.index,
                        pack_obj.obj_type,
                        pack_obj.size,
                        if let Some(ref hash) = pack_obj.sha1 {
                            format!(" | {hash}")
                        } else {
                            String::new()
                        }
                    );

                    ListItem::new(display_text).style(
                        if is_selected && matches!(preview_state.focus, PackFocus::PackObjectsList)
                            || is_selected && is_widescreen
                        {
                            Style::default().fg(Color::Yellow)
                        } else {
                            Style::default()
                        },
                    )
                },
            );
        }
    }
}

pub fn navigation_hints(app: &AppState) -> Vec<Span<'_>> {
    let is_wide_screen = app.is_wide_screen();
    let mut hints = Vec::new();
    if let AppView::Main { state } = &app.view {
        let MainViewState {
            preview_state,
            git_objects,
            ..
        } = &state;
        match &preview_state {
            PreviewState::Pack(PackPreViewState { focus, .. }) => {
                match focus {
                    PackFocus::GitObjects => {
                        hints.append(&mut vec![
                            Span::styled("", Style::default().fg(Color::Green)),
                            Span::styled("↕→", Style::default().fg(Color::Blue)),
                        ]);
                    }
                    PackFocus::Educational => {
                        if is_wide_screen {
                            hints.push(Span::styled("←↕→", Style::default().fg(Color::Blue)));
                        } else {
                            hints.append(&mut vec![
                                Span::styled("←↕", Style::default().fg(Color::Blue)),
                                Span::styled("", Style::default().fg(Color::Gray)),
                            ]);
                        }
                    }
                    PackFocus::PackObjectsList => {
                        if is_wide_screen {
                            hints.push(Span::styled("←↕→", Style::default().fg(Color::Blue)));
                        } else {
                            hints.append(&mut vec![
                                Span::styled("←↕", Style::default().fg(Color::Blue)),
                                Span::styled("", Style::default().fg(Color::Green)),
                            ]);
                        }
                    }
                    PackFocus::PackObjectDetails => {
                        hints.append(&mut vec![
                            Span::styled("←↕", Style::default().fg(Color::Blue)),
                            Span::styled("", Style::default().fg(Color::Gray)),
                        ]);
                    }
                };
            }
            PreviewState::Regular(RegularPreViewState { focus, .. }) => match focus {
                RegularFocus::GitObjects => {
                    if !git_objects.flat_view.is_empty()
                        && git_objects.selected_index < git_objects.flat_view.len()
                    {
                        match git_objects.flat_view[git_objects.selected_index].1.obj_type {
                            GitObjectType::Category(_) => {
                                hints.append(&mut vec![
                                    Span::styled("", Style::default().fg(Color::Green)),
                                    Span::styled("↕→", Style::default().fg(Color::Blue)),
                                ]);
                            }
                            _ => {
                                hints.append(&mut vec![
                                    Span::styled("", Style::default().fg(Color::Green)),
                                    Span::styled("↕→", Style::default().fg(Color::Blue)),
                                ]);
                            }
                        };
                    }
                }
                RegularFocus::Preview => {
                    hints.append(&mut vec![
                        Span::styled("←↕", Style::default().fg(Color::Blue)),
                        Span::styled("", Style::default().fg(Color::Gray)),
                    ]);
                }
            },
        };
    };
    hints.append(&mut vec![
        Span::raw(" to navigate | "),
        Span::raw("("),
        Span::styled("Q", Style::default().fg(Color::Blue)),
        Span::raw(")uit"),
    ]);
    hints
}

fn render_git_tree(
    f: &mut ratatui::Frame,
    state: &mut MainViewState,
    project_name: String,
    area: ratatui::layout::Rect,
    reduced: bool,
) {
    render_list_with_scrollbar(
        f,
        area,
        &state.git_objects.flat_view,
        Some(state.git_objects.selected_index),
        state.git_objects.scroll_position,
        &format!("{project_name}/.git"),
        state.are_git_objects_focused(),
        |i, (depth, obj, _status), is_selected| {
            let _ = reduced;
            // Create indentation based on depth
            let indent = if *depth > 0 {
                let mut indent = String::new();

                // For each level from 0 to depth-1, determine if we need a vertical line
                for d in 0..(*depth - 1) {
                    // We need a vertical line at depth d if there are more siblings
                    // at depth d that will come after the current branch
                    let needs_vertical_line = {
                        // Find the ancestor of the current item at depth d+1
                        let mut ancestor_index = None;
                        for k in (0..i).rev() {
                            let (ancestor_depth, _, _) = &state.git_objects.flat_view[k];
                            if *ancestor_depth == d + 1 {
                                ancestor_index = Some(k);
                                break;
                            } else if *ancestor_depth <= d {
                                break;
                            }
                        }

                        // If we found an ancestor, check if it has siblings after it
                        if let Some(ancestor_idx) = ancestor_index {
                            let mut has_sibling = false;
                            for j in (ancestor_idx + 1)..state.git_objects.flat_view.len() {
                                let (next_depth, _, _) = &state.git_objects.flat_view[j];
                                if *next_depth == d + 1 {
                                    has_sibling = true;
                                    break;
                                } else if *next_depth <= d {
                                    break;
                                }
                            }
                            has_sibling
                        } else {
                            false
                        }
                    };

                    indent.push_str(if needs_vertical_line { "" } else { " " });
                }

                indent
            } else {
                String::new()
            };

            // Add expansion indicator for categories and folders
            let prefix = match &obj.obj_type {
                GitObjectType::Category(_) if !obj.children.is_empty() => {
                    if obj.expanded {
                        if *depth == 0 {
                            ""
                        } else {
                            // Find if this is the last category at this depth
                            let is_last = {
                                let mut is_last = true;
                                for j in (i + 1)..state.git_objects.flat_view.len() {
                                    let (next_depth, _, _) = &state.git_objects.flat_view[j];
                                    if *next_depth == *depth {
                                        is_last = false;
                                        break;
                                    } else if *next_depth < *depth {
                                        break;
                                    }
                                }
                                is_last
                            };
                            if is_last { "└▼ " } else { "├▼ " }
                        }
                    } else if *depth == 0 {
                        ""
                    } else {
                        // Find if this is the last category at this depth
                        let is_last = {
                            let mut is_last = true;
                            for j in (i + 1)..state.git_objects.flat_view.len() {
                                let (next_depth, _, _) = &state.git_objects.flat_view[j];
                                // next_depth check considers tuple (usize, GitObject, RenderStatus)

                                if *next_depth == *depth {
                                    is_last = false;
                                    break;
                                } else if *next_depth < *depth {
                                    break;
                                }
                            }
                            is_last
                        };
                        if is_last { "└▶ " } else { "├▶ " }
                    }
                }
                GitObjectType::FileSystemFolder { .. } => {
                    // FileSystemFolder should always show expansion indicators (directories are expandable)
                    if obj.expanded {
                        if *depth == 0 {
                            ""
                        } else {
                            // Find if this is the last folder at this depth
                            let is_last = {
                                let mut is_last = true;
                                for j in (i + 1)..state.git_objects.flat_view.len() {
                                    let (next_depth, _, _) = &state.git_objects.flat_view[j];
                                    if *next_depth == *depth {
                                        is_last = false;
                                        break;
                                    } else if *next_depth < *depth {
                                        break;
                                    }
                                }
                                is_last
                            };
                            if is_last { "└▼ " } else { "├▼ " }
                        }
                    } else if *depth == 0 {
                        ""
                    } else {
                        // Find if this is the last folder at this depth
                        let is_last = {
                            let mut is_last = true;
                            for j in (i + 1)..state.git_objects.flat_view.len() {
                                let (next_depth, _, _) = &state.git_objects.flat_view[j];
                                if *next_depth == *depth {
                                    is_last = false;
                                    break;
                                } else if *next_depth < *depth {
                                    break;
                                }
                            }
                            is_last
                        };
                        if is_last { "└▶ " } else { "├▶ " }
                    }
                }
                GitObjectType::Category(name) => {
                    // Special handling for "Loose Objects" to always show folder indicators
                    if name == "Loose Objects" {
                        // Always show triangle that changes based on expansion state
                        // Use ▽ when expanded, ▷ when collapsed (regardless of content)
                        if *depth == 0 {
                            if obj.expanded { "" } else { "" }
                        } else {
                            // Find if this is the last category at this depth
                            let is_last = {
                                let mut is_last = true;
                                for j in (i + 1)..state.git_objects.flat_view.len() {
                                    let (next_depth, _, _) = &state.git_objects.flat_view[j];
                                    if *next_depth == *depth {
                                        is_last = false;
                                        break;
                                    } else if *next_depth < *depth {
                                        break;
                                    }
                                }
                                is_last
                            };

                            if obj.expanded {
                                if is_last { "└▽ " } else { "├▽ " }
                            } else if is_last {
                                "└▷ "
                            } else {
                                "├▷ "
                            }
                        }
                    } else if *depth == 0 {
                        // No prefix for other root-level categories
                        ""
                    } else {
                        // Find if this is the last category at this depth
                        let is_last = {
                            let mut is_last = true;
                            for j in (i + 1)..state.git_objects.flat_view.len() {
                                let (next_depth, _, _) = &state.git_objects.flat_view[j];
                                if *next_depth == *depth {
                                    is_last = false;
                                    break;
                                } else if *next_depth < *depth {
                                    break;
                                }
                            }
                            is_last
                        };
                        if is_last { "└─ " } else { "├─ " }
                    }
                }
                GitObjectType::PackFolder { .. } => {
                    // PackFolder should show expansion indicators like a directory
                    if obj.expanded {
                        if *depth == 0 {
                            ""
                        } else {
                            // Find if this is the last folder at this depth
                            let is_last = {
                                let mut is_last = true;
                                for j in (i + 1)..state.git_objects.flat_view.len() {
                                    let (next_depth, _, _) = &state.git_objects.flat_view[j];
                                    if *next_depth == *depth {
                                        is_last = false;
                                        break;
                                    } else if *next_depth < *depth {
                                        break;
                                    }
                                }
                                is_last
                            };
                            if is_last { "└▼ " } else { "├▼ " }
                        }
                    } else if *depth == 0 {
                        ""
                    } else {
                        // Find if this is the last folder at this depth
                        let is_last = {
                            let mut is_last = true;
                            for j in (i + 1)..state.git_objects.flat_view.len() {
                                let (next_depth, _, _) = &state.git_objects.flat_view[j];
                                if *next_depth == *depth {
                                    is_last = false;
                                    break;
                                } else if *next_depth < *depth {
                                    break;
                                }
                            }
                            is_last
                        };
                        if is_last { "└▶ " } else { "├▶ " }
                    }
                }
                _ => {
                    // Find if this is the last item in its group
                    let is_last = if *depth > 0 {
                        // Look ahead to find the next item at the same depth
                        let mut is_last = true;
                        for j in (i + 1)..state.git_objects.flat_view.len() {
                            let (next_depth, _, _) = &state.git_objects.flat_view[j];
                            if *next_depth == *depth {
                                is_last = false;
                                break;
                            } else if *next_depth < *depth {
                                break;
                            }
                        }
                        is_last
                    } else {
                        false
                    };

                    match *depth {
                        0 => "",
                        _ => {
                            if is_last {
                                "└─ "
                            } else {
                                "├─ "
                            }
                        }
                    }
                }
            };

            let display_text = format!("{}{}{}", indent, prefix, obj.name);
            let _key = MainViewState::selection_key(obj);

            // Simple item rendering; highlight is applied in a post-render cell pass
            ListItem::new(display_text).style({
                if is_selected {
                    Style::default().fg(Color::Yellow)
                } else {
                    Style::default()
                }
            })
        },
    );

    // If there are no items yet, render a placeholder "Loading…"
    if state.git_objects.flat_view.is_empty() {
        use ratatui::widgets::Paragraph;
        let placeholder = Paragraph::new("Loading…").block(
            Block::default()
                .title(format!("{project_name}/.git"))
                .borders(Borders::ALL),
        );
        f.render_widget(placeholder, area);
    }
}