crates/diff/src/status.rs 2.0 KiB raw
1
//! Shared diff status presentation primitives.
2
3
use rustik_tui::{Color, Label, Line, Style};
4
5
use crate::{DiffPalette, ReviewProgress, Stats};
6
7
/// Glyph used for filled and empty review progress segments.
8
pub const REVIEW_PROGRESS_BAR_GLYPH: &str = "━";
9
10
/// Returns a recoverable-error status line with the standard error badge.
11
pub fn recoverable_error(error: &str, value_fg: Color, background: Color) -> Line {
12
    let message = format!(" {error} ");
13
    Line::blank()
14
        .item(Label::new(" error ").style(Style::default().fg(Color::White).bg(Color::Red).bold()))
15
        .item(Label::new(&message).style(Style::default().fg(value_fg).bg(background)))
16
        .style(Style::default().bg(background))
17
}
18
19
/// Width of the standard review progress bar.
20
const REVIEW_PROGRESS_BAR_WIDTH: usize = 8;
21
22
/// Returns a compact addition and deletion summary.
23
pub fn diffstat(stats: Stats, palette: DiffPalette) -> Line {
24
    Line::blank()
25
        .item(Label::new(&format!("+{}", stats.insertions)).fg(palette.positive_fg))
26
        .item(Label::space())
27
        .item(Label::new(&format!("-{}", stats.deletions)).fg(palette.negative_fg))
28
}
29
30
/// Formats a changed-file count with the correct singular or plural noun.
31
pub fn file_count(files: usize) -> String {
32
    let plural = if files == 1 { "" } else { "s" };
33
    format!("{files} file{plural}")
34
}
35
36
/// Returns the standard fixed-width review progress bar.
37
pub fn progress_bar(progress: ReviewProgress, palette: DiffPalette, empty_fg: Color) -> Line {
38
    let filled = progress.filled(REVIEW_PROGRESS_BAR_WIDTH);
39
    let empty = REVIEW_PROGRESS_BAR_WIDTH - filled;
40
41
    Line::blank()
42
        .item(Label::new(&REVIEW_PROGRESS_BAR_GLYPH.repeat(filled)).fg(palette.positive_fg))
43
        .item(Label::new(&REVIEW_PROGRESS_BAR_GLYPH.repeat(empty)).fg(empty_fg))
44
}
45
46
/// Returns a fully filled standard review progress bar in `fg`.
47
pub fn full_progress_bar(fg: Color) -> Line {
48
    Line::new(Label::new(&REVIEW_PROGRESS_BAR_GLYPH.repeat(REVIEW_PROGRESS_BAR_WIDTH)).fg(fg))
49
}