brain: Show commit review progress

f8074f848b58fec2011bb18cb12f08ccd03fbbd6
Alexis Sellier committed ago 1 parent 3cb7e9e2
crates/brain/src/decorate.rs +3 -3
8 8
//! hunks show a comment marker and count.
9 9
10 10
use std::collections::{BTreeMap, BTreeSet};
11 11
12 12
use rustik_diff::{
13 -
    DiffDecorator, DiffPalette, FileDecoration, FileId, FileProgress, HunkDecoration, HunkId,
13 +
    DiffDecorator, DiffPalette, FileDecoration, FileId, HunkDecoration, HunkId, ReviewProgress,
14 14
};
15 15
use rustik_tui as tui;
16 16
use tui::{Label, Line, Paint, Style, colors};
17 17
18 18
use crate::comments::Comments;
54 54
}
55 55
56 56
impl DiffDecorator for Decorations {
57 57
    fn file(&self, file: FileId) -> FileDecoration {
58 58
        let hunks = self.file_hunks.get(&file).map_or(&[][..], Vec::as_slice);
59 -
        let progress = (!hunks.is_empty()).then(|| FileProgress {
59 +
        let progress = (!hunks.is_empty()).then(|| ReviewProgress {
60 60
            accepted: hunks
61 61
                .iter()
62 62
                .filter(|hunk| self.accepted.contains(hunk))
63 63
                .count(),
64 64
            total: hunks.len(),
69 69
            .sum::<usize>();
70 70
        let mut decoration = FileDecoration {
71 71
            progress,
72 72
            ..FileDecoration::default()
73 73
        };
74 -
        if progress.is_some_and(FileProgress::is_complete) {
74 +
        if progress.is_some_and(ReviewProgress::is_complete) {
75 75
            decoration.status = Some(Paint::new(ACCEPTED_MARK).fg(self.palette.positive_fg));
76 76
        } else if comment_count > 0 {
77 77
            decoration.annotations = vec![Line::new(
78 78
                Label::new(&format!("{comment_count} {COMMENT_MARK}")).fg(colors::rgb::DIM),
79 79
            )];
crates/brain/src/session.rs +10 -2
4 4
use std::mem;
5 5
use std::sync::mpsc::{self, Receiver, Sender};
6 6
7 7
use rustik_diff::{
8 8
    ContextChange, DiffAction, DiffDocument, DiffFocus, DiffOptions, DiffViewer, FileDiff,
9 -
    FileTreeMode, HighlightOutput, HighlightRequest, Highlighter, ViewChange, spawn_highlighter,
9 +
    FileTreeMode, HighlightOutput, HighlightRequest, Highlighter, ReviewProgress, ViewChange,
10 +
    spawn_highlighter,
10 11
};
11 12
use rustik_tui::{Control, Event, Key, Line, List, ListState, Mouse, MouseButton, Rect, Terminal};
12 13
use rustik_tui_termion as tui_termion;
13 14
14 15
use crate::backend::{Backend, Brain, Commit, CommitHunks};
937 938
    }
938 939
939 940
    /// Returns the footer status line for the current commit.
940 941
    fn footer(&self) -> Status {
941 942
        let slot = &self.commits[self.current];
943 +
        let progress = slot
944 +
            .loaded
945 +
            .as_ref()
946 +
            .map_or_else(ReviewProgress::default, |loaded| ReviewProgress {
947 +
                accepted: loaded.review.accepted().len(),
948 +
                total: loaded.file_hunks.values().map(Vec::len).sum(),
949 +
            });
942 950
        Status {
943 951
            mode: self.mode.label(),
944 952
            short: slot.commit.id.short(),
945 953
            summary: slot.commit.summary.clone(),
946 -
            reviewed: slot.reviewed,
954 +
            progress,
947 955
            stats: slot.commit.stats,
948 956
            palette: self.options.diff_palette,
949 957
            visibility: self.visibility,
950 958
        }
951 959
    }
crates/brain/src/session/status.rs +28 -17
1 1
//! Status and chrome rendering for the review session.
2 2
3 3
use std::time::{SystemTime, UNIX_EPOCH};
4 4
5 -
use rustik_diff::DiffPalette;
5 +
use rustik_diff::{DiffPalette, ReviewProgress};
6 6
use rustik_repo::DiffStat;
7 7
use rustik_tui::{
8 8
    Border, Buffer, Color, Constraint, HStack, Label, Line, Rect, Size, Style, Table, TableOptions,
9 9
    Widget, colors,
10 10
};
32 32
const FOOTER_SUMMARY_FG: Color = Color::Fixed(153);
33 33
/// Border color for the selected comment in the hunk view.
34 34
const SELECTED_COMMENT_BORDER: Color = Color::Fixed(239);
35 35
/// Marker shown beside a commit whose hunks have all been accepted: ✔.
36 36
const REVIEWED_MARK: char = '✔';
37 -
/// Marker shown when the active commit is fully reviewed: ✓.
38 -
const REVIEWED_STATUS_MARK: &str = "✓";
39 -
/// Marker shown when the active commit still needs review: ·.
40 -
const UNREVIEWED_STATUS_MARK: &str = "·";
41 37
/// Marker shown when accepted hunks are visible in the footer: ✔.
42 38
const APPROVED_HUNKS_MODE_ICON: &str = "✔";
43 39
/// Marker shown when full-file mode is visible in the footer: ↕.
44 40
const FULL_FILE_MODE_ICON: &str = "↕";
41 +
/// Width of the commit review progress bar in the footer.
42 +
const COMMIT_PROGRESS_BAR_WIDTH: usize = 8;
43 +
/// Glyph text used by commit review progress bars: ━.
44 +
const COMMIT_PROGRESS_BAR_GLYPH: &str = "━";
45 45
/// Separator shown between footer status segments: ·.
46 46
const FOOTER_SEPARATOR: &str = " · ";
47 47
/// Marker used when footer text truncates: ….
48 48
const FOOTER_ELLIPSIS: &str = "…";
49 49
/// Foreground color for active context-expansion mode.
64 64
    pub(super) mode: &'static str,
65 65
    /// Abbreviated current commit hash.
66 66
    pub(super) short: String,
67 67
    /// First line of the current commit message.
68 68
    pub(super) summary: String,
69 -
    /// Whether all hunks in the current commit are accepted.
70 -
    pub(super) reviewed: bool,
69 +
    /// Accepted-hunk progress for the current commit.
70 +
    pub(super) progress: ReviewProgress,
71 71
    /// Aggregated diff size for the current commit.
72 72
    pub(super) stats: DiffStat,
73 73
    /// Diff palette used for stat and review colors.
74 74
    pub(super) palette: DiffPalette,
75 75
    /// Session-wide accepted-hunk visibility.
134 134
            .style(Style::default().bg(FOOTER_BG))
135 135
    }
136 136
137 137
    /// Returns the right status section with review state and diff statistics.
138 138
    fn right_status(&self) -> Line {
139 -
        let review = if self.reviewed {
140 -
            REVIEWED_STATUS_MARK
141 -
        } else {
142 -
            UNREVIEWED_STATUS_MARK
143 -
        };
144 139
        let accepted_fg = if self.visibility.show_accepted {
145 140
            self.palette.positive_fg
146 141
        } else {
147 142
            FOOTER_FG
148 143
        };
153 148
        };
154 149
        let files = self.stats.files_changed;
155 150
        let plural = if files == 1 { "" } else { "s" };
156 151
157 152
        Line::blank()
158 -
            .item(Label::new(review).fg(self.palette.positive_fg))
159 -
            .item(Label::space())
160 -
            .item(Label::new(APPROVED_HUNKS_MODE_ICON).fg(accepted_fg))
161 -
            .item(Label::space())
162 -
            .item(Label::new(FULL_FILE_MODE_ICON).fg(full_file_fg))
153 +
            .extend(Self::progress_bar(self.progress, self.palette))
163 154
            .item(Label::space())
164 155
            .item(Label::new(&format!("+{}", self.stats.insertions)).fg(self.palette.positive_fg))
165 156
            .item(Label::space())
166 157
            .item(Label::new(&format!("-{}", self.stats.deletions)).fg(self.palette.negative_fg))
167 158
            .item(Label::new(FOOTER_SEPARATOR).fg(FOOTER_FG))
168 159
            .item(Label::new(&format!("{files} file{plural}")).fg(FOOTER_VALUE_FG))
160 +
            .item(Label::new(FOOTER_SEPARATOR).fg(FOOTER_FG))
161 +
            .item(Label::new(APPROVED_HUNKS_MODE_ICON).fg(accepted_fg))
162 +
            .item(Label::space())
163 +
            .item(Label::new(FULL_FILE_MODE_ICON).fg(full_file_fg))
164 +
            .item(Label::space())
169 165
            .style(Style::default().bg(FOOTER_BG))
170 166
    }
167 +
168 +
    /// Returns a compact commit review progress bar.
169 +
    fn progress_bar(progress: ReviewProgress, palette: DiffPalette) -> Line {
170 +
        let filled = progress
171 +
            .accepted
172 +
            .saturating_mul(COMMIT_PROGRESS_BAR_WIDTH)
173 +
            .checked_div(progress.total)
174 +
            .unwrap_or(0)
175 +
            .min(COMMIT_PROGRESS_BAR_WIDTH);
176 +
        let empty = COMMIT_PROGRESS_BAR_WIDTH - filled;
177 +
178 +
        Line::blank()
179 +
            .item(Label::new(&COMMIT_PROGRESS_BAR_GLYPH.repeat(filled)).fg(palette.positive_fg))
180 +
            .item(Label::new(&COMMIT_PROGRESS_BAR_GLYPH.repeat(empty)).fg(FOOTER_FG))
181 +
    }
171 182
}
172 183
173 184
/// Returns the viewer area above the footer chrome.
174 185
pub(super) fn viewer_area(area: Rect) -> Rect {
175 186
    Status::areas(area).0
crates/diff/src/decorate.rs +7 -7
42 42
pub struct FileDecoration {
43 43
    /// Status glyph inserted before the path, for example an approval marker.
44 44
    pub status: Option<Paint<char>>,
45 45
    /// Right-aligned segments shown before the diffstat, outermost first.
46 46
    pub annotations: Vec<Line>,
47 -
    /// File-level review progress shown in the header.
48 -
    pub progress: Option<FileProgress>,
47 +
    /// Review progress shown in the file header.
48 +
    pub progress: Option<ReviewProgress>,
49 49
    /// Replacement for the computed `+adds -dels` diffstat, when set.
50 50
    pub stats: Option<Line>,
51 51
}
52 52
53 -
/// File-level review progress.
53 +
/// Review progress over a fixed set of hunks.
54 54
#[derive(Clone, Copy, Debug, Default, Eq, PartialEq)]
55 -
pub struct FileProgress {
56 -
    /// Number of reviewed hunks in the file.
55 +
pub struct ReviewProgress {
56 +
    /// Number of accepted hunks.
57 57
    pub accepted: usize,
58 -
    /// Total number of reviewable hunks in the file.
58 +
    /// Total number of reviewable hunks.
59 59
    pub total: usize,
60 60
}
61 61
62 -
impl FileProgress {
62 +
impl ReviewProgress {
63 63
    /// Returns whether all reviewable hunks have been accepted.
64 64
    pub fn is_complete(self) -> bool {
65 65
        self.total > 0 && self.accepted >= self.total
66 66
    }
67 67
}
crates/diff/src/lib.rs +1 -1
31 31
#[cfg(feature = "builtins")]
32 32
pub use builtins::Builtins;
33 33
pub use commit::{Commit, Header, ParseCommitError};
34 34
pub use context::ContextChange;
35 35
pub use decode::Decode;
36 -
pub use decorate::{DiffDecorator, FileDecoration, FileProgress, HunkDecoration};
36 +
pub use decorate::{DiffDecorator, FileDecoration, HunkDecoration, ReviewProgress};
37 37
pub use document::{
38 38
    DecodeError, DiffDocument, DiffLine, DiffLineKind, FileDiff, FileHeader, FileId, FileKind,
39 39
    FileMode, FileStats, Hunk, HunkHeader, HunkId, Oid, OidError, RowId, Stats,
40 40
};
41 41
pub use encode::Encode;
crates/diff/src/viewer.rs +33 -10
16 16
    MouseButton, Rect, Scroll, ScrollView, ScrollbarStyle, Size, Style, Table, TableOptions, Text,
17 17
    VStack, View, Viewport, Widget, colors,
18 18
};
19 19
20 20
use crate::context::{self, ContextChange, ContextState};
21 -
use crate::decorate::{DiffDecorator, FileDecoration, FileProgress, HunkDecoration};
21 +
use crate::decorate::{DiffDecorator, FileDecoration, HunkDecoration, ReviewProgress};
22 22
use crate::highlight::{DiffHighlighter, HighlightKeyInput, HighlightResult};
23 23
use crate::word;
24 24
use crate::{
25 25
    Commit, Decode, DecodeError, DiffDocument, DiffLine, DiffLineKind, DiffSide, FileDiff, FileId,
26 26
    FileKind, HighlightKey, HighlightRequest, HighlightSource, Hunk, HunkId, RowId,
74 74
/// Marker cell shown between hunks in the diff marker column.
75 75
const HUNK_GAP_MARKER_CELL: &str = " ⋮ ";
76 76
/// Separator shown between file header metadata segments: ·.
77 77
const FILE_HEADER_SEGMENT_SEPARATOR: &str = "  · ";
78 78
/// Width of the file review progress bar in file headers.
79 -
const FILE_PROGRESS_BAR_WIDTH: usize = 12;
79 +
const FILE_PROGRESS_BAR_WIDTH: usize = 10;
80 80
/// Glyph text used by file review progress bars: ━.
81 81
const FILE_PROGRESS_BAR_GLYPH: &str = "━";
82 +
/// Columns reserved for file-header review status, matching "+9999 -9999".
83 +
const FILE_REVIEW_STATUS_COLS: usize = 11;
82 84
/// Default syntax theme selection stored in highlight keys.
83 85
const DEFAULT_THEME: &str = "auto";
84 86
/// Number of rows in a sticky file header.
85 87
const STICKY_HEADER_ROWS: usize = 3;
86 88
/// Number of body rows moved by keyboard line-scroll inputs.
1991 1993
        }
1992 1994
        // Right-aligned segments: host annotations first, then the diffstat (or
1993 1995
        // its decoration override), joined by a fixed gap.
1994 1996
        let mut right = decoration.annotations.clone();
1995 1997
        if let Some(progress) = decoration.progress {
1996 -
            right.push(Self::progress_bar(progress, palette));
1997 -
        }
1998 -
        if decoration.progress.is_some_and(FileProgress::is_complete) {
1999 -
            right.push(Line::new(Label::new("Approved").fg(palette.positive_fg)));
1998 +
            let mut progress_line = Self::progress_bar(progress, palette);
1999 +
            progress_line.push(Label::space());
2000 +
            if progress.is_complete() {
2001 +
                progress_line = progress_line.extend(
2002 +
                    Self::review_status_line(Line::new(
2003 +
                        Label::new("Approved").fg(palette.positive_fg),
2004 +
                    ))
2005 +
                    .labels()
2006 +
                    .iter()
2007 +
                    .cloned(),
2008 +
                );
2009 +
            } else if let Some(stats) = decoration.stats.clone().or_else(|| {
2010 +
                let stats = self.stats();
2011 +
                Self::diffstat_line(stats.additions, stats.deletions, palette)
2012 +
            }) {
2013 +
                progress_line =
2014 +
                    progress_line.extend(Self::review_status_line(stats).labels().iter().cloned());
2015 +
            }
2016 +
            right.push(progress_line);
2000 2017
        } else if let Some(stats) = decoration.stats.clone().or_else(|| {
2001 2018
            let stats = self.stats();
2002 2019
            Self::diffstat_line(stats.additions, stats.deletions, palette)
2003 2020
        }) {
2004 2021
            right.push(stats);
2034 2051
        }
2035 2052
        let mut line = Line::blank();
2036 2053
        if additions > 0 {
2037 2054
            line.push(Label::new(&format!("+{additions}")).fg(palette.positive_fg));
2038 2055
        }
2056 +
        if additions > 0 && deletions > 0 {
2057 +
            line.push(Label::space());
2058 +
        }
2039 2059
        if deletions > 0 {
2040 -
            if additions > 0 {
2041 -
                line.push(Label::space());
2042 -
            }
2043 2060
            line.push(Label::new(&format!("-{deletions}")).fg(palette.negative_fg));
2044 2061
        }
2045 2062
        Some(line)
2046 2063
    }
2047 2064
2065 +
    /// Returns a status line padded to the file-header review status width.
2066 +
    fn review_status_line(line: Line) -> Line {
2067 +
        let padding = FILE_REVIEW_STATUS_COLS.saturating_sub(line.width());
2068 +
        Line::new(Label::new(&" ".repeat(padding))).extend(line.labels().iter().cloned())
2069 +
    }
2070 +
2048 2071
    /// Returns a compact file review progress bar.
2049 -
    fn progress_bar(progress: FileProgress, palette: DiffPalette) -> Line {
2072 +
    fn progress_bar(progress: ReviewProgress, palette: DiffPalette) -> Line {
2050 2073
        let filled = progress
2051 2074
            .accepted
2052 2075
            .saturating_mul(FILE_PROGRESS_BAR_WIDTH)
2053 2076
            .checked_div(progress.total)
2054 2077
            .unwrap_or(0)
a.rs +0 -0
a.rs +0 -0
b.rs +0 -0
a.rs +0 -0
a.rs +0 -0
a.rs +0 -0
b.rs +0 -0
crates/diff/tests/golden/sticky_header_before_next_file_64x5.txt +0 -0
src/a.rs +0 -0
src/b.rs +0 -0
README.md +0 -0
src/app/a.rs +0 -0
src/lib/b.rs +0 -0
README.md +0 -0
a.rs +0 -0
b.rs +0 -0
assets/css/style.css +0 -0
assets/css/style.css +0 -0
first.rs +0 -0
second.rs +0 -0
a.rs +0 -0
a.rs +0 -0
a.rs +0 -0
a.rs +0 -0
b.rs +0 -0