Skip to main content

rustik_diff/
status.rs

1//! Shared diff status presentation primitives.
2
3use rustik_tui::{Color, Label, Line, Style};
4
5use crate::{DiffPalette, ReviewProgress, Stats};
6
7/// Glyph used for filled and empty review progress segments.
8pub const REVIEW_PROGRESS_BAR_GLYPH: &str = "━";
9
10/// Returns a recoverable-error status line with the standard error badge.
11pub 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.
20const REVIEW_PROGRESS_BAR_WIDTH: usize = 8;
21
22/// Returns a compact addition and deletion summary.
23pub 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.
31pub 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.
37pub 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}