gitstack 5.3.0

Git history viewer with insights - Author stats, file heatmap, code ownership
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
use ratatui::{
    layout::Rect,
    style::{Modifier, Style},
    text::{Line, Span},
    widgets::{Block, Borders, Clear, Paragraph},
    Frame,
};

use super::super::helpers::{
    compute_overlay_size_standard, display_width, truncate_to_width,
    truncate_to_width_with_ellipsis, DETAIL_OVERLAY_HEIGHT, OVERLAY_BG_COLOR, OVERLAY_MARGIN,
    OVERLAY_MIN_WIDTH,
};
use super::super::theme;
use crate::app::App;
use crate::event::GitEventKind;
use crate::git::FileChangeStatus;

/// Render detail overlay
pub(crate) fn render_detail_overlay(frame: &mut Frame, app: &App) {
    let Some(event) = app.selected_event() else {
        return;
    };

    let area = frame.area();

    // Width is standard 80%
    let (overlay_x, _, overlay_width, _) = compute_overlay_size_standard(area);

    // Calculate height based on content
    let file_count = app
        .detail_diff_cache
        .as_ref()
        .map(|d| d.files.len())
        .unwrap_or(0);

    // border(2) + header(3) + separator/summary(3) + files + scroll_indicator(1) + footer(2)
    let content_height = if app.detail_diff_cache.is_some() {
        (11 + file_count) as u16
    } else {
        // loading state: border(2) + header(3) + loading(2) + footer(2) = 9
        9
    };

    let overlay_height = content_height
        .max(DETAIL_OVERLAY_HEIGHT)
        .min(area.height.saturating_sub(OVERLAY_MARGIN));

    // Recalculate y for centering
    let overlay_y = (area.height.saturating_sub(overlay_height)) / 2;

    let overlay_area = Rect::new(overlay_x, overlay_y, overlay_width, overlay_height);

    // Clear background
    frame.render_widget(Clear, overlay_area);

    let lang = app.language;
    let kind_str = match event.kind {
        GitEventKind::Commit => lang.event_commit(),
        GitEventKind::Merge => lang.event_merge(),
        GitEventKind::BranchSwitch => lang.event_branch_switch(),
    };

    // Constant to align label widths
    const LABEL_WIDTH: usize = 9; // Match the length of "Message: "

    let label_style = Style::default().fg(theme::SUBTEXT0);
    let dim_style = Style::default().fg(theme::SUBTEXT0);

    let mut content = vec![
        Line::from(vec![
            Span::styled(format!("{:>LABEL_WIDTH$}", lang.detail_type()), label_style),
            Span::raw(kind_str),
            Span::raw("  "),
            Span::styled(format!("{:>LABEL_WIDTH$}", lang.detail_hash()), label_style),
            Span::styled(event.short_hash.clone(), Style::default().fg(theme::YELLOW)),
        ]),
        Line::from(vec![
            Span::styled(
                format!("{:>LABEL_WIDTH$}", lang.detail_message()),
                label_style,
            ),
            Span::raw(event.message.clone()),
        ]),
        Line::from(vec![
            Span::styled(
                format!("{:>LABEL_WIDTH$}", lang.detail_author()),
                label_style,
            ),
            Span::raw(event.author.clone()),
            Span::raw("  "),
            Span::styled(format!("{:>LABEL_WIDTH$}", lang.detail_time()), label_style),
            Span::raw(event.relative_time()),
        ]),
    ];

    // Display file change list if diff cache is available
    if let Some(ref diff) = app.detail_diff_cache {
        // Summary line
        content.push(Line::from(vec![Span::styled(
            "".repeat(overlay_width as usize - 4),
            dim_style,
        )]));
        content.push(Line::from(vec![Span::styled(
            format!(
                " Changes: {} files, +{} -{}",
                diff.stats.files_changed, diff.stats.insertions, diff.stats.deletions
            ),
            Style::default().fg(theme::SAPPHIRE),
        )]));
        content.push(Line::from(vec![Span::styled(
            "".repeat(overlay_width as usize - 4),
            dim_style,
        )]));

        // File list (with scroll support)
        let visible_file_lines = overlay_height.saturating_sub(10) as usize;
        let scroll_offset = app.detail_nav.scroll_offset;
        let end_idx = (scroll_offset + visible_file_lines).min(diff.files.len());

        for (idx, file) in diff
            .files
            .iter()
            .enumerate()
            .skip(scroll_offset)
            .take(end_idx - scroll_offset)
        {
            let is_selected = idx == app.detail_nav.selected_index;

            // Status character and color
            let (status_char, status_color) = match file.status {
                FileChangeStatus::Added => ('A', theme::FILE_ADDED),
                FileChangeStatus::Modified => ('M', theme::FILE_MODIFIED),
                FileChangeStatus::Deleted => ('D', theme::FILE_DELETED),
                FileChangeStatus::Renamed => ('R', theme::FILE_RENAMED),
            };

            // Selection marker
            let marker = if is_selected { "" } else { " " };

            // Maximum path width (reserve space for +XX -XX display)
            let stats_width = 14;
            let path_max_width = (overlay_width as usize).saturating_sub(stats_width + 6);
            let path_display = truncate_to_width_with_ellipsis(&file.path, path_max_width);
            let path_display_width = display_width(&path_display);

            // Padding
            let padding = path_max_width.saturating_sub(path_display_width);

            // Line style
            let line_style = if is_selected {
                Style::default()
                    .fg(theme::YELLOW)
                    .add_modifier(Modifier::BOLD)
            } else {
                Style::default()
            };

            content.push(Line::from(vec![
                Span::styled(marker, line_style),
                Span::styled(
                    format!(" {} ", status_char),
                    Style::default().fg(status_color),
                ),
                Span::styled(path_display, line_style),
                Span::raw(" ".repeat(padding)),
                Span::styled(
                    format!(" +{:<3} -{:<3}", file.insertions, file.deletions),
                    dim_style,
                ),
            ]));
        }

        // Scroll indicator when there are many files
        if diff.files.len() > visible_file_lines {
            let indicator = format!(
                " [{}/{}]",
                app.detail_nav.selected_index + 1,
                diff.files.len()
            );
            content.push(Line::from(vec![Span::styled(indicator, dim_style)]));
        }
    } else {
        // Diff is loading
        content.push(Line::from(""));
        content.push(Line::from(vec![Span::styled(
            lang.loading_file_changes(),
            dim_style,
        )]));
    }

    // Footer
    content.push(Line::from(""));
    content.push(Line::from(vec![Span::styled(
        lang.detail_footer(),
        dim_style,
    )]));

    let paragraph = Paragraph::new(content).block(
        Block::default()
            .title(app.language.event_detail())
            .borders(Borders::ALL)
            .border_style(Style::default().fg(theme::LAVENDER))
            .style(Style::default().bg(OVERLAY_BG_COLOR)),
    );

    frame.render_widget(paragraph, overlay_area);
}

/// Render file history view overlay
pub(crate) fn render_file_history_view_overlay(frame: &mut Frame, app: &App) {
    let area = frame.area();

    // Calculate overlay size (use 80% of screen, guarantee minimum width)
    let overlay_width = ((area.width as f32 * 0.8) as u16)
        .max(OVERLAY_MIN_WIDTH)
        .min(area.width.saturating_sub(OVERLAY_MARGIN * 2));
    let overlay_height = (area.height as f32 * 0.8) as u16;
    let x = (area.width.saturating_sub(overlay_width)) / 2;
    let y = (area.height.saturating_sub(overlay_height)) / 2;
    let overlay_area = Rect::new(x, y, overlay_width, overlay_height);
    let inner_width = overlay_width.saturating_sub(2) as usize;

    // Clear background
    frame.render_widget(Clear, overlay_area);

    let Some(ref history) = app.file_history_view.cache else {
        let paragraph = Paragraph::new(app.language.loading_history()).block(
            Block::default()
                .title(app.language.file_history())
                .borders(Borders::ALL)
                .border_style(Style::default().fg(theme::LAVENDER))
                .style(Style::default().bg(OVERLAY_BG_COLOR)),
        );
        frame.render_widget(paragraph, overlay_area);
        return;
    };

    let dim_style = Style::default().fg(theme::SUBTEXT0);

    // Column display control based on width
    let show_author = inner_width >= 50;
    let hash_len = if inner_width >= 45 { 8 } else { 6 };
    let date_len = if inner_width >= 50 {
        10
    } else if inner_width >= 35 {
        7
    } else {
        6
    };

    // Calculate visible line count (excluding 2 border + 4 header + 2 footer lines)
    let visible_lines = overlay_height.saturating_sub(8) as usize;

    // Generate content
    let mut content = Vec::new();

    // Display file path in title
    let file_path = app.file_history_view.path.as_deref().unwrap_or("unknown");
    let title_width = inner_width.saturating_sub(16); // space for " File History: "
    let file_path_display = truncate_to_width_with_ellipsis(file_path, title_width);

    // Header (summary)
    let summary = if inner_width >= 25 {
        format!(" Total: {} commits", history.len())
    } else {
        format!(" {} commits", history.len())
    };
    content.push(Line::from(vec![Span::styled(
        summary,
        Style::default().fg(theme::SAPPHIRE),
    )]));
    content.push(Line::from(""));

    let lang = app.language;
    // Table header
    if show_author {
        content.push(Line::from(vec![
            Span::styled("  ", dim_style),
            Span::styled(
                format!("{:<width$} ", lang.header_hash(), width = hash_len),
                dim_style,
            ),
            Span::styled(format!("{:<15}", lang.header_author()), dim_style),
            Span::styled(
                format!("{:<width$}", lang.header_date(), width = date_len),
                dim_style,
            ),
            Span::styled(lang.header_message(), dim_style),
        ]));
    } else {
        content.push(Line::from(vec![
            Span::styled("  ", dim_style),
            Span::styled(
                format!("{:<width$} ", lang.header_hash(), width = hash_len),
                dim_style,
            ),
            Span::styled(
                format!("{:<width$} ", lang.header_date(), width = date_len),
                dim_style,
            ),
            Span::styled(lang.header_msg_short(), dim_style),
        ]));
    }
    content.push(Line::from(vec![Span::styled(
        "".repeat(inner_width.saturating_sub(2)),
        dim_style,
    )]));

    // Calculate scroll range
    let scroll_offset = app.file_history_view.nav.scroll_offset;
    let end_idx = (scroll_offset + visible_lines).min(history.len());

    // Display each entry
    for (idx, entry) in history
        .iter()
        .enumerate()
        .skip(scroll_offset)
        .take(end_idx - scroll_offset)
    {
        let is_selected = idx == app.file_history_view.nav.selected_index;

        // Selection marker
        let marker = if is_selected { "" } else { " " };

        // Line style
        let line_style = if is_selected {
            Style::default()
                .fg(theme::YELLOW)
                .add_modifier(Modifier::BOLD)
        } else {
            Style::default()
        };

        // Calculate relative time (shortened based on width)
        let relative_time = {
            let now = chrono::Local::now();
            let diff = now.signed_duration_since(entry.date);
            if date_len <= 6 {
                // Short format: "5m", "3h", "7d"
                if diff.num_hours() < 1 {
                    format!("{}m", diff.num_minutes().max(1))
                } else if diff.num_hours() < 24 {
                    format!("{}h", diff.num_hours())
                } else if diff.num_days() < 100 {
                    format!("{}d", diff.num_days())
                } else {
                    entry.date.format("%m/%d").to_string()
                }
            } else if date_len <= 7 {
                if diff.num_hours() < 1 {
                    format!("{}m ago", diff.num_minutes().max(1))
                } else if diff.num_hours() < 24 {
                    format!("{}h ago", diff.num_hours())
                } else if diff.num_days() < 30 {
                    format!("{}d ago", diff.num_days())
                } else {
                    entry.date.format("%m/%d").to_string()
                }
            } else if diff.num_hours() < 1 {
                format!("{}m ago", diff.num_minutes().max(1))
            } else if diff.num_hours() < 24 {
                format!("{}h ago", diff.num_hours())
            } else if diff.num_days() < 30 {
                format!("{}d ago", diff.num_days())
            } else {
                entry.date.format("%Y-%m-%d").to_string()
            }
        };

        // Calculate maximum message width
        let msg_base_offset = if show_author { 45 } else { 20 };
        let message_max_width = inner_width.saturating_sub(msg_base_offset);
        let message_display = truncate_to_width_with_ellipsis(&entry.message, message_max_width);

        // Truncate hash
        let hash_display = truncate_to_width(&entry.hash, hash_len);

        if show_author {
            // Truncate author name
            let author_display = truncate_to_width_with_ellipsis(&entry.author, 13);

            content.push(Line::from(vec![
                Span::styled(format!("{} ", marker), line_style),
                Span::styled(
                    format!("{:<width$} ", hash_display, width = hash_len),
                    line_style,
                ),
                Span::styled(format!("{:<15}", author_display), line_style),
                Span::styled(
                    format!("{:<width$} ", relative_time, width = date_len),
                    line_style,
                ),
                Span::styled(message_display, line_style),
            ]));
        } else {
            content.push(Line::from(vec![
                Span::styled(format!("{} ", marker), line_style),
                Span::styled(
                    format!("{:<width$} ", hash_display, width = hash_len),
                    line_style,
                ),
                Span::styled(
                    format!("{:<width$} ", relative_time, width = date_len),
                    line_style,
                ),
                Span::styled(message_display, line_style),
            ]));
        }
    }

    // Footer
    content.push(Line::from(""));
    let scroll_indicator = if history.len() > visible_lines {
        format!(
            " [{}/{}]",
            app.file_history_view.nav.selected_index + 1,
            history.len()
        )
    } else {
        String::new()
    };
    // Width-adaptive footer
    if inner_width >= 40 {
        content.push(Line::from(vec![
            Span::styled("j/k", Style::default().fg(theme::YELLOW)),
            Span::raw(lang.hint_move()),
            Span::styled("Enter", Style::default().fg(theme::YELLOW)),
            Span::raw(lang.hint_show_diff()),
            Span::styled("F/Esc", Style::default().fg(theme::YELLOW)),
            Span::raw(lang.hint_close()),
            Span::styled(scroll_indicator, Style::default().fg(theme::SUBTEXT0)),
        ]));
    } else {
        content.push(Line::from(vec![
            Span::styled("j/k Enter F/Esc", Style::default().fg(theme::SUBTEXT0)),
            Span::styled(scroll_indicator, Style::default().fg(theme::SUBTEXT0)),
        ]));
    }

    let paragraph = Paragraph::new(content).block(
        Block::default()
            .title(format!(" History: {} ", file_path_display))
            .borders(Borders::ALL)
            .border_style(Style::default().fg(theme::LAVENDER))
            .style(Style::default().bg(OVERLAY_BG_COLOR)),
    );

    frame.render_widget(paragraph, overlay_area);
}

/// Render blame view overlay
pub(crate) fn render_blame_view_overlay(frame: &mut Frame, app: &App) {
    let area = frame.area();

    // Calculate overlay size (use 90% of screen, guarantee minimum width)
    let overlay_width = ((area.width as f32 * 0.9) as u16)
        .max(OVERLAY_MIN_WIDTH)
        .min(area.width.saturating_sub(OVERLAY_MARGIN * 2));
    let overlay_height = (area.height as f32 * 0.85) as u16;
    let x = (area.width.saturating_sub(overlay_width)) / 2;
    let y = (area.height.saturating_sub(overlay_height)) / 2;
    let overlay_area = Rect::new(x, y, overlay_width, overlay_height);
    let inner_width = overlay_width.saturating_sub(2) as usize;

    // Clear background
    frame.render_widget(Clear, overlay_area);

    let Some(ref blame) = app.blame_view.cache else {
        let paragraph = Paragraph::new(app.language.loading_blame()).block(
            Block::default()
                .title(app.language.blame())
                .borders(Borders::ALL)
                .border_style(Style::default().fg(theme::MAUVE))
                .style(Style::default().bg(OVERLAY_BG_COLOR)),
        );
        frame.render_widget(paragraph, overlay_area);
        return;
    };

    let dim_style = Style::default().fg(theme::SUBTEXT0);

    // Column display control based on width
    let show_author = inner_width >= 55;
    let show_date = inner_width >= 45;
    let line_no_width = if inner_width >= 40 { 5 } else { 4 };
    let hash_len = if inner_width >= 50 {
        8
    } else if inner_width >= 35 {
        6
    } else {
        5
    };

    // Calculate visible line count (excluding 2 border + 2 footer lines)
    let visible_lines = overlay_height.saturating_sub(4) as usize;

    // Generate content
    let mut content = Vec::new();

    // File path
    let file_path = app.blame_view.path.as_deref().unwrap_or("unknown");
    let title_width = inner_width.saturating_sub(10);
    let file_path_display = truncate_to_width_with_ellipsis(file_path, title_width);
    let total_lines = blame.len();

    // Display lines
    for (i, line) in blame
        .iter()
        .enumerate()
        .skip(app.blame_view.nav.scroll_offset)
        .take(visible_lines.saturating_sub(2))
    {
        let is_selected = i == app.blame_view.nav.selected_index;
        let line_style = if is_selected {
            Style::default().bg(theme::SURFACE2)
        } else {
            Style::default()
        };

        // Calculate maximum content width
        let metadata_width = if show_author && show_date {
            line_no_width + 1 + hash_len + 1 + 13 + 10 // line_no + hash + author + date
        } else if show_date {
            line_no_width + 1 + hash_len + 1 + 8 // line_no + hash + date
        } else {
            line_no_width + 1 + hash_len + 1 // line_no + hash
        };
        let content_max_width = inner_width.saturating_sub(metadata_width + 2);
        let content_display = truncate_to_width_with_ellipsis(&line.content, content_max_width);

        // Truncate hash
        let hash_display = truncate_to_width(&line.hash, hash_len);

        if show_author && show_date {
            // Calculate relative time
            let relative_time = {
                let now = chrono::Local::now();
                let diff = now.signed_duration_since(line.date);
                if diff.num_hours() < 1 {
                    format!("{}m ago", diff.num_minutes().max(1))
                } else if diff.num_hours() < 24 {
                    format!("{}h ago", diff.num_hours())
                } else if diff.num_days() < 30 {
                    format!("{}d ago", diff.num_days())
                } else {
                    line.date.format("%Y-%m-%d").to_string()
                }
            };

            // Truncate author name
            let author_display = truncate_to_width_with_ellipsis(&line.author, 12);

            content.push(Line::from(vec![
                Span::styled(
                    format!("{:>width$} ", line.line_number, width = line_no_width),
                    dim_style,
                ),
                Span::styled(
                    format!("{:<width$} ", hash_display, width = hash_len),
                    Style::default().fg(theme::YELLOW),
                ),
                Span::styled(
                    format!("{:<13}", author_display),
                    Style::default().fg(theme::SAPPHIRE),
                ),
                Span::styled(format!("{:<10}", relative_time), dim_style),
                Span::styled(content_display, line_style),
            ]));
        } else if show_date {
            // Relative time only (short format)
            let relative_time = {
                let now = chrono::Local::now();
                let diff = now.signed_duration_since(line.date);
                if diff.num_hours() < 1 {
                    format!("{}m", diff.num_minutes().max(1))
                } else if diff.num_hours() < 24 {
                    format!("{}h", diff.num_hours())
                } else if diff.num_days() < 100 {
                    format!("{}d", diff.num_days())
                } else {
                    line.date.format("%m/%d").to_string()
                }
            };

            content.push(Line::from(vec![
                Span::styled(
                    format!("{:>width$} ", line.line_number, width = line_no_width),
                    dim_style,
                ),
                Span::styled(
                    format!("{:<width$} ", hash_display, width = hash_len),
                    Style::default().fg(theme::YELLOW),
                ),
                Span::styled(format!("{:<8}", relative_time), dim_style),
                Span::styled(content_display, line_style),
            ]));
        } else {
            // Line number + hash + code only
            content.push(Line::from(vec![
                Span::styled(
                    format!("{:>width$} ", line.line_number, width = line_no_width),
                    dim_style,
                ),
                Span::styled(
                    format!("{:<width$} ", hash_display, width = hash_len),
                    Style::default().fg(theme::YELLOW),
                ),
                Span::styled(content_display, line_style),
            ]));
        }
    }

    // Footer
    content.push(Line::from(""));
    let scroll_indicator = if total_lines > visible_lines.saturating_sub(2) {
        format!(
            " [{}/{}]",
            app.blame_view.nav.selected_index + 1,
            total_lines
        )
    } else {
        String::new()
    };
    // Width-adaptive footer
    let lang = app.language;
    if inner_width >= 40 {
        content.push(Line::from(vec![
            Span::styled("j/k", Style::default().fg(theme::YELLOW)),
            Span::raw(lang.hint_scroll()),
            Span::styled("Enter", Style::default().fg(theme::YELLOW)),
            Span::raw(lang.hint_show_commit()),
            Span::styled("B/Esc", Style::default().fg(theme::YELLOW)),
            Span::raw(lang.hint_close()),
            Span::styled(scroll_indicator, Style::default().fg(theme::SUBTEXT0)),
        ]));
    } else {
        content.push(Line::from(vec![
            Span::styled("j/k Enter B/Esc", Style::default().fg(theme::SUBTEXT0)),
            Span::styled(scroll_indicator, Style::default().fg(theme::SUBTEXT0)),
        ]));
    }

    let paragraph = Paragraph::new(content).block(
        Block::default()
            .title(format!(" Blame: {} ", file_path_display))
            .borders(Borders::ALL)
            .border_style(Style::default().fg(theme::MAUVE))
            .style(Style::default().bg(OVERLAY_BG_COLOR)),
    );

    frame.render_widget(paragraph, overlay_area);
}

/// Render patch view overlay
pub(crate) fn render_patch_view_overlay(frame: &mut Frame, app: &App) {
    let Some(ref patch) = app.patch_view.cache else {
        return;
    };

    let area = frame.area();

    // Calculate overlay size (use 90% of screen, guarantee minimum width)
    let overlay_width = ((area.width as f32 * 0.9) as u16)
        .max(OVERLAY_MIN_WIDTH)
        .min(area.width.saturating_sub(OVERLAY_MARGIN * 2));
    let overlay_height = (area.height as f32 * 0.85) as u16;
    let x = (area.width.saturating_sub(overlay_width)) / 2;
    let y = (area.height.saturating_sub(overlay_height)) / 2;
    let overlay_area = Rect::new(x, y, overlay_width, overlay_height);
    let inner_width = overlay_width.saturating_sub(2) as usize;

    // Clear background
    frame.render_widget(Clear, overlay_area);

    let dim_style = Style::default().fg(theme::SUBTEXT0);

    // Line number display control based on width
    let show_old_lineno = inner_width >= 45;
    let lineno_width = if show_old_lineno { 4 } else { 3 };

    // Calculate visible line count (excluding 2 border + 2 footer lines)
    let visible_lines = overlay_height.saturating_sub(4) as usize;

    // Generate content
    let mut content: Vec<Line> = Vec::new();

    // Display patch lines
    let total_lines = patch.lines.len();
    let scroll_offset = app.patch_view.scroll_offset;
    let end_idx = (scroll_offset + visible_lines.saturating_sub(2)).min(total_lines);

    for line in patch
        .lines
        .iter()
        .skip(scroll_offset)
        .take(end_idx - scroll_offset)
    {
        let (style, prefix) = match line.origin {
            '+' => (Style::default().fg(theme::GREEN), "+"),
            '-' => (Style::default().fg(theme::RED), "-"),
            _ => (Style::default().fg(theme::TEXT), " "),
        };

        // Display line numbers (adjusted based on width)
        let lineno_str = if show_old_lineno {
            match (line.old_lineno, line.new_lineno) {
                (Some(old), Some(new)) => format!("{:>4} {:>4}", old, new),
                (Some(old), None) => format!("{:>4}     ", old),
                (None, Some(new)) => format!("     {:>4}", new),
                (None, None) => "         ".to_string(),
            }
        } else {
            // Display new line number only
            match line.new_lineno {
                Some(new) => format!("{:>width$}", new, width = lineno_width),
                None => match line.old_lineno {
                    Some(old) => format!("{:>width$}", old, width = lineno_width),
                    None => " ".repeat(lineno_width),
                },
            }
        };

        // Calculate maximum content width (subtract line number + prefix + margin)
        let metadata_width = if show_old_lineno {
            12
        } else {
            lineno_width + 3
        };
        let content_max_width = inner_width.saturating_sub(metadata_width);
        let content_display = truncate_to_width(&line.content, content_max_width);

        content.push(Line::from(vec![
            Span::styled(format!("{} ", lineno_str), dim_style),
            Span::styled(prefix.to_string(), style),
            Span::styled(content_display, style),
        ]));
    }

    // Footer
    content.push(Line::from(""));
    let scroll_indicator = if total_lines > visible_lines.saturating_sub(2) {
        format!(" [{}/{}]", scroll_offset + 1, total_lines)
    } else {
        String::new()
    };
    // Width-adaptive footer
    let lang = app.language;
    if inner_width >= 35 {
        content.push(Line::from(vec![
            Span::styled("j/k", Style::default().fg(theme::YELLOW)),
            Span::raw(lang.hint_scroll()),
            Span::styled("P/Esc", Style::default().fg(theme::YELLOW)),
            Span::raw(lang.hint_close()),
            Span::styled(scroll_indicator, Style::default().fg(theme::SUBTEXT0)),
        ]));
    } else {
        content.push(Line::from(vec![
            Span::styled("j/k P/Esc", Style::default().fg(theme::SUBTEXT0)),
            Span::styled(scroll_indicator, Style::default().fg(theme::SUBTEXT0)),
        ]));
    }

    // Display file path in the title (adjust based on width)
    let title_width = inner_width.saturating_sub(10);
    let title = format!(
        " Patch: {} ",
        truncate_to_width_with_ellipsis(&patch.path, title_width)
    );

    let paragraph = Paragraph::new(content).block(
        Block::default()
            .title(title)
            .borders(Borders::ALL)
            .border_style(Style::default().fg(theme::GREEN))
            .style(Style::default().bg(OVERLAY_BG_COLOR)),
    );

    frame.render_widget(paragraph, overlay_area);
}

/// Render stash view overlay
pub(crate) fn render_stash_view_overlay(frame: &mut Frame, app: &App) {
    let Some(ref stashes) = app.stash_view.cache else {
        return;
    };

    let area = frame.area();

    // Calculate overlay size (standard 80%)
    let (overlay_x, overlay_y, overlay_width, overlay_height) = compute_overlay_size_standard(area);
    let overlay_area = Rect::new(overlay_x, overlay_y, overlay_width, overlay_height);

    // Clear background
    frame.render_widget(Clear, overlay_area);

    let mut content: Vec<Line> = Vec::new();

    let lang = app.language;
    // Title display
    content.push(Line::from(vec![
        Span::styled(lang.stash_list_label(), Style::default().fg(theme::YELLOW)),
        Span::styled(
            format!("({} stashes)", stashes.len()),
            Style::default().fg(theme::SUBTEXT0),
        ),
    ]));
    content.push(Line::from(""));

    if stashes.is_empty() {
        content.push(Line::from(Span::styled(
            lang.no_stashes(),
            Style::default().fg(theme::SUBTEXT0),
        )));
    } else {
        // Visible line count
        let visible_lines = (overlay_height as usize).saturating_sub(6);

        for (i, entry) in stashes
            .iter()
            .enumerate()
            .skip(app.stash_view.nav.scroll_offset)
            .take(visible_lines)
        {
            let is_selected = i == app.stash_view.nav.selected_index;
            let line_style = if is_selected {
                Style::default()
                    .bg(theme::SURFACE2)
                    .add_modifier(Modifier::BOLD)
            } else {
                Style::default()
            };

            let marker = if is_selected { "" } else { " " };

            // stash@{N}: message
            let stash_label = format!("stash@{{{}}}", entry.index);
            let max_message_width = (overlay_width as usize).saturating_sub(stash_label.len() + 6);
            let message = truncate_to_width_with_ellipsis(&entry.message, max_message_width);

            content.push(Line::from(vec![
                Span::styled(format!("{} ", marker), line_style),
                Span::styled(
                    format!("{}: ", stash_label),
                    Style::default().fg(theme::SAPPHIRE),
                ),
                Span::styled(message, line_style),
            ]));
        }
    }

    // Footer
    content.push(Line::from(""));
    let scroll_indicator = if !stashes.is_empty() {
        format!(
            " [{}/{}]",
            app.stash_view.nav.selected_index + 1,
            stashes.len()
        )
    } else {
        String::new()
    };
    content.push(Line::from(vec![
        Span::styled("Space", Style::default().fg(theme::YELLOW)),
        Span::raw(lang.hint_apply()),
        Span::styled("p", Style::default().fg(theme::YELLOW)),
        Span::raw(lang.hint_pop()),
        Span::styled("d", Style::default().fg(theme::YELLOW)),
        Span::raw(lang.hint_drop()),
        Span::styled("z/Esc", Style::default().fg(theme::YELLOW)),
        Span::raw(lang.hint_close()),
        Span::styled(scroll_indicator, Style::default().fg(theme::SUBTEXT0)),
    ]));

    let paragraph = Paragraph::new(content).block(
        Block::default()
            .title(app.language.stash_overlay())
            .borders(Borders::ALL)
            .border_style(Style::default().fg(theme::MAUVE))
            .style(Style::default().bg(OVERLAY_BG_COLOR)),
    );

    frame.render_widget(paragraph, overlay_area);
}