brain: Show per-commit remaining diffstats

fd2b85359cb6dc5a727b127a4494e988e6a98164
Derive each row from unaccepted hunks and leave the diffstat blank when its
commit is fully approved.

Assisted-by: Codex:gpt-5.6-sol
Alexis Sellier committed ago 1 parent 1e59964a
crates/brain/src/backend.rs +26 -20
279 279
        reconciled
280 280
    }
281 281
}
282 282
283 283
/// Global and per-commit review status for the history view.
284 -
#[derive(Clone, Debug, Default)]
284 +
#[derive(Debug, Default)]
285 285
pub struct HistoryStatus {
286 286
    /// Accepted-line progress across the review.
287 287
    pub progress: ReviewProgress,
288 288
    /// Remaining diff from the accepted projection to the target.
289 289
    pub stats: DiffStat,
290 290
    /// Remaining review status keyed by commit.
291 291
    pub commits: BTreeMap<Oid, CommitStatus>,
292 292
}
293 293
294 294
/// Remaining review status for one commit.
295 -
#[derive(Clone, Debug, Default)]
295 +
#[derive(Debug, Default)]
296 296
pub struct CommitStatus {
297 297
    /// Accepted-line progress for the commit.
298 298
    pub progress: ReviewProgress,
299 299
    /// Diff statistics from hunks that remain unaccepted.
300 300
    pub stats: DiffStat,
853 853
        }
854 854
855 855
        Ok(summaries)
856 856
    }
857 857
858 -
    /// Returns accepted-hunk progress for decoded commit documents.
859 -
    fn review_progress(
858 +
    /// Returns review progress and remaining statistics for each commit.
859 +
    fn commit_statuses(
860 860
        &self,
861 861
        commits: &[Oid],
862 862
        documents: &[&DiffDocument],
863 863
    ) -> Result<BTreeMap<Oid, CommitStatus>, Error> {
864 864
        let identities = commits
865 865
            .iter()
866 866
            .zip(documents)
867 -
            .map(|(commit, document)| self.hunk_identities(document, commit.id))
867 +
            .map(|(commit, document)| self.hunk_identities(document, *commit))
868 868
            .collect::<Result<Vec<_>, _>>()?;
869 869
        let hunks = commits
870 870
            .iter()
871 871
            .zip(&identities)
872 872
            .map(|(commit, identities)| CommitHunks {
873 -
                commit: commit.id,
873 +
                commit: *commit,
874 874
                hunks: Some(identities),
875 875
            })
876 876
            .collect::<Vec<_>>();
877 877
        let reconciled = self.reconcile(&hunks);
878 878
        let mut statuses = BTreeMap::new();
880 880
            .iter()
881 881
            .zip(documents)
882 882
            .zip(reconciled)
883 883
            .zip(identities)
884 884
        {
885 -
            let mut stats = DiffStat::default();
886 -
            for file in &document.files {
887 -
                let mut remaining = false;
888 -
                for hunk in &file.hunks {
889 -
                    if reconciled.accepted.contains(&hunk.id) {
890 -
                        continue;
891 -
                    }
892 -
                    remaining = true;
893 -
                    let hunk = hunk.stats();
894 -
                    stats.insertions += hunk.additions;
895 -
                    stats.deletions += hunk.deletions;
896 -
                }
897 -
                stats.files_changed += usize::from(remaining);
898 -
            }
885 +
            let stats = commit_remaining_stats(document, &reconciled.accepted);
899 886
            statuses.insert(
900 887
                *commit,
901 888
                CommitStatus {
902 889
                    progress: reconciled.progress,
903 890
                    stats,
1239 1226
    pub fn reconcile(&self, commits: &[CommitHunks<'_>]) -> Vec<Reconciled> {
1240 1227
        Reconciled::for_commits(&self.accepted, commits)
1241 1228
    }
1242 1229
}
1243 1230
1231 +
/// Returns unaccepted hunk statistics while retaining hunkless file sections.
1232 +
fn commit_remaining_stats(document: &DiffDocument, accepted: &BTreeSet<HunkId>) -> DiffStat {
1233 +
    let mut stats = DiffStat::default();
1234 +
    for file in &document.files {
1235 +
        let mut remaining = file.hunks.is_empty();
1236 +
        for hunk in &file.hunks {
1237 +
            if accepted.contains(&hunk.id) {
1238 +
                continue;
1239 +
            }
1240 +
            remaining = true;
1241 +
            let hunk = hunk.stats();
1242 +
            stats.insertions += hunk.additions;
1243 +
            stats.deletions += hunk.deletions;
1244 +
        }
1245 +
        stats.files_changed += usize::from(remaining);
1246 +
    }
1247 +
    stats
1248 +
}
1249 +
1244 1250
/// Returns the patch text used to identify `hunk` across base movement.
1245 1251
fn hunk_identity_patch(file: &FileDiff, hunk: &Hunk) -> Result<String, Error> {
1246 1252
    Ok(DiffDocument::for_hunks(file, [hunk.identity()]).to_unified_string()?)
1247 1253
}
1248 1254
f +0 -0
empty +0 -0
crates/brain/src/session.rs +10 -6
15 15
    Constraint, Control, Event, Key, Line, List, ListState, Mouse, MouseButton, Rect, ScrollView,
16 16
    Size, Style, Table, TableOptions, Terminal,
17 17
};
18 18
use rustik_tui_termion as tui_termion;
19 19
20 -
use crate::backend::{Backend, Brain, Commit, CommitHunks, FilePatches};
20 +
use crate::backend::{Backend, Brain, Commit, CommitHunks, FilePatches, HistoryStatus};
21 21
use crate::comments::ReviewComments;
22 22
use crate::error::Error;
23 23
use crate::review::Visibility;
24 24
25 25
mod commit;
231 231
    /// Per-commit review slots, newest first.
232 232
    commits: Vec<CommitSlot>,
233 233
    /// User-facing revision range under review.
234 234
    range: String,
235 235
    /// Lazily cached global progress and remaining statistics for history.
236 -
    history_status: Option<(ReviewProgress, DiffStat)>,
236 +
    history_status: Option<HistoryStatus>,
237 237
    /// Index of the commit currently displayed.
238 238
    current: usize,
239 239
    /// Session-wide accepted-hunk visibility.
240 240
    visibility: Visibility,
241 241
    /// Active view: the commit list, the diff, or a hunk's comment box.
1094 1094
                let now = current_timestamp();
1095 1095
                let last = self.commits.len() - 1;
1096 1096
                self.terminal.draw(|frame| {
1097 1097
                    let (body, chrome_area) = Chrome::areas(frame.area);
1098 1098
                    let cols = ScrollView::body_cols(body);
1099 -
                    let mut author_table = Table::<11, Line>::new(TableOptions {
1099 +
                    let mut author_table = Table::<10, Line>::new(TableOptions {
1100 1100
                        spacing: 2,
1101 1101
                        ..TableOptions::default()
1102 1102
                    });
1103 1103
                    for (index, slot) in self.commits.iter().enumerate() {
1104 1104
                        let graph = match (index, last) {
1112 1112
                        ));
1113 1113
                    }
1114 1114
                    let rows = if author_table.size(Constraint::UNBOUNDED).cols <= cols {
1115 1115
                        author_table.lines(Constraint::max(Size::new(cols, usize::MAX)))
1116 1116
                    } else {
1117 -
                        let mut table = Table::<10, Line>::new(TableOptions {
1117 +
                        let mut table = Table::<9, Line>::new(TableOptions {
1118 1118
                            spacing: 2,
1119 1119
                            ..TableOptions::default()
1120 1120
                        });
1121 1121
                        for (index, slot) in self.commits.iter().enumerate() {
1122 1122
                            let graph = match (index, last) {
1154 1154
    }
1155 1155
1156 1156
    /// Returns the footer chrome for the current session mode.
1157 1157
    fn chrome(&self) -> Chrome {
1158 1158
        let (context, progress, stats) = if matches!(self.mode, Mode::Commits) {
1159 -
            let (progress, stats) = self.history_status.unwrap_or_default();
1160 -
            (StatusContext::Range(self.range.clone()), progress, stats)
1159 +
            let status = self.history_status.as_ref();
1160 +
            (
1161 +
                StatusContext::Range(self.range.clone()),
1162 +
                status.map_or_else(ReviewProgress::default, |status| status.progress),
1163 +
                status.map_or_else(DiffStat::default, |status| status.stats),
1164 +
            )
1161 1165
        } else {
1162 1166
            let slot = &self.commits[self.current];
1163 1167
            let progress = slot
1164 1168
                .loaded
1165 1169
                .as_ref()
crates/brain/src/session/commit.rs +2 -2
57 57
    /// Original diff stats per hunk, used for file-level remaining diffstats.
58 58
    pub(super) hunk_stats: BTreeMap<HunkId, FileStats>,
59 59
}
60 60
61 61
impl CommitReview {
62 -
    /// Returns the combined diffstat of files with hunks that still need review.
62 +
    /// Returns the combined diffstat of files that still need review.
63 63
    pub(super) fn remaining_stats(&self) -> DiffStat {
64 64
        let mut total = DiffStat::default();
65 65
66 66
        for hunks in self.file_hunks.values() {
67 -
            let mut remaining = false;
67 +
            let mut remaining = hunks.is_empty();
68 68
            for hunk in hunks {
69 69
                if self.review.is_accepted(*hunk) {
70 70
                    continue;
71 71
                }
72 72
                remaining = true;
a.rs +0 -0
crates/brain/src/session/status.rs +30 -55
11 11
};
12 12
13 13
use crate::comments::Comment;
14 14
use crate::review::Visibility;
15 15
16 -
use super::commit::CommitSlot;
16 +
use super::commit::{CommitReview, CommitSlot};
17 17
18 18
/// Number of rows used by the status bar.
19 19
const STATUS_ROWS: usize = 1;
20 20
/// Number of rows used by the action bar.
21 21
const ACTION_ROWS: usize = 1;
356 356
pub(super) fn commit_row(
357 357
    slot: &CommitSlot,
358 358
    palette: DiffPalette,
359 359
    graph: &str,
360 360
    now: u64,
361 -
1: ) -> [Line; 10] {
362 -
    let [
363 -
        graph, id, insertions, deletions, files, status, progress, branches, _, age, summary,
364 -
    ] = commit_row_with_author(slot, palette, graph, now);
361 +
) -> [Line; 9] {
362 +
    let [graph, id, diffstat, files, status, progress, branches, _, age, summary] =
363 +
        commit_row_with_author(slot, palette, graph, now);
365 364
366 365
    [
367 -
        graph, id, insertions, deletions, files, status, progress, branches, age, summary,
368 -
2:         Line::new(Label::new(&format!("+{}", stats.insertions)).fg(palette.positive_fg)),
369 -
        Line::new(Label::new(&format!("-{}", stats.deletions)).fg(palette.negative_fg)),
370 -
        Line::new(Label::new(&file_count(stats.files_changed)).fg(FOOTER_FG)),
371 -
3:         Line::new(slot.commit.summary.as_str()),
372 -
4:         let mut table = Table::<11, Line>::new(TableOptions {
366 +
        graph, id, diffstat, files, status, progress, branches, age, summary,
373 367
    ]
374 368
}
375 369
376 370
/// Returns one commit-table row including an author column.
377 371
pub(super) fn commit_row_with_author(
378 372
    slot: &CommitSlot,
379 373
    palette: DiffPalette,
380 374
    graph: &str,
381 375
    now: u64,
382 -
) -> [Line; 11] {
383 -
    let stats = slot.commit.stats;
376 +
) -> [Line; 10] {
377 +
    let stats = slot
378 +
        .loaded
379 +
        .as_ref()
380 +
        .map_or(slot.remaining_stats, CommitReview::remaining_stats);
384 381
    let short = slot.commit.id.short();
385 -
    let status = if slot.progress.is_complete() {
382 +
    let complete = slot.progress.is_complete();
383 +
    let status = if complete {
386 384
        Label::from_char(REVIEWED_MARK).fg(palette.positive_fg)
387 385
    } else {
388 386
        Label::space()
389 387
    };
388 +
    let diffstat = if complete {
389 +
        Line::blank()
390 +
    } else {
391 +
        Line::blank()
392 +
            .item(Label::new(&format!("+{}", stats.insertions)).fg(palette.positive_fg))
393 +
            .item(Label::space())
394 +
            .item(Label::new(&format!("-{}", stats.deletions)).fg(palette.negative_fg))
395 +
    };
390 396
    [
391 397
        Line::new(Label::new(graph).fg(colors::fixed::MUTED)),
392 398
        Line::new(Label::new(&short).fg(colors::rgb::ORANGE)),
393 -
1: ) -> [Line; 10] {
394 -
    let [
395 -
        graph, id, insertions, deletions, files, status, progress, branches, _, age, summary,
396 -
    ] = commit_row_with_author(slot, palette, graph, now);
397 -
398 -
    [
399 -
        graph, id, insertions, deletions, files, status, progress, branches, age, summary,
400 -
2:         Line::new(Label::new(&format!("+{}", stats.insertions)).fg(palette.positive_fg)),
401 -
        Line::new(Label::new(&format!("-{}", stats.deletions)).fg(palette.negative_fg)),
399 +
        diffstat,
402 400
        Line::new(Label::new(&file_count(stats.files_changed)).fg(FOOTER_FG)),
403 -
3:         Line::new(slot.commit.summary.as_str()),
404 -
4:         let mut table = Table::<11, Line>::new(TableOptions {
405 401
        Line::new(status),
406 402
        progress_bar(slot.progress, palette, FOOTER_FG),
407 403
        Line::new(Label::new(&slot.commit.branches.join(", ")).fg(colors::rgb::TEAL)),
408 404
        Line::new(Label::new(slot.commit.author.as_str()).fg(colors::fixed::MUTED)),
409 405
        Line::new(
411 407
                slot.commit.recency.seconds.into(),
412 408
                now.into(),
413 409
            ))
414 410
            .fg(colors::fixed::DIM),
415 411
        ),
416 -
1: ) -> [Line; 10] {
417 -
    let [
418 -
        graph, id, insertions, deletions, files, status, progress, branches, _, age, summary,
419 -
    ] = commit_row_with_author(slot, palette, graph, now);
420 -
421 -
    [
422 -
        graph, id, insertions, deletions, files, status, progress, branches, age, summary,
423 -
2:         Line::new(Label::new(&format!("+{}", stats.insertions)).fg(palette.positive_fg)),
424 -
        Line::new(Label::new(&format!("-{}", stats.deletions)).fg(palette.negative_fg)),
425 -
        Line::new(Label::new(&file_count(stats.files_changed)).fg(FOOTER_FG)),
426 -
3:         Line::new(slot.commit.summary.as_str()),
427 -
4:         let mut table = Table::<11, Line>::new(TableOptions {
412 +
        Line::new(slot.commit.summary.as_str()),
428 413
    ]
429 414
}
430 415
431 416
/// Renders one stored comment as a bordered card.
432 417
pub(super) fn comment_card_lines(comment: &Comment, selected: bool, cols: usize) -> Vec<Line> {
626 611
    fn commit_row_keeps_status_and_pointers_before_summary() {
627 612
        let now = 2_000_000_000;
628 613
        let recency = CommitTime {
629 614
            seconds: now - 3_600,
630 615
        };
616 +
        let stats = DiffStat {
617 +
            files_changed: 1,
618 +
            insertions: 2,
619 +
            deletions: 3,
620 +
        };
631 621
        let slot = CommitSlot {
632 622
            commit: Commit {
633 623
                id: "0123456789abcdef0123456789abcdef01234567"
634 624
                    .parse()
635 625
                    .unwrap(),
636 626
                summary: "A commit summary that cannot fit in the remaining width".to_owned(),
637 627
                author: "Alice".to_owned(),
638 628
                branches: vec!["main".to_owned()],
639 629
                recency,
640 -
                stats: DiffStat {
641 -
                    files_changed: 1,
642 -
                    insertions: 2,
643 -
                    deletions: 3,
644 -
                },
630 +
                stats,
645 631
            },
646 632
            progress: ReviewProgress {
647 633
                accepted: 1,
648 634
                total: 1,
649 635
            },
650 636
            remaining_stats: stats,
651 637
            document: None,
652 638
            identities: None,
653 639
            loaded: None,
654 640
        };
655 -
1: ) -> [Line; 10] {
656 -
    let [
657 -
        graph, id, insertions, deletions, files, status, progress, branches, _, age, summary,
658 -
    ] = commit_row_with_author(slot, palette, graph, now);
659 -
660 -
    [
661 -
        graph, id, insertions, deletions, files, status, progress, branches, age, summary,
662 -
2:         Line::new(Label::new(&format!("+{}", stats.insertions)).fg(palette.positive_fg)),
663 -
        Line::new(Label::new(&format!("-{}", stats.deletions)).fg(palette.negative_fg)),
664 -
        Line::new(Label::new(&file_count(stats.files_changed)).fg(FOOTER_FG)),
665 -
3:         Line::new(slot.commit.summary.as_str()),
666 -
4:         let mut table = Table::<11, Line>::new(TableOptions {
641 +
        let mut table = Table::<10, Line>::new(TableOptions {
667 642
            spacing: 2,
668 643
            ..TableOptions::default()
669 644
        });
670 645
        table.push(commit_row_with_author(
671 646
            &slot,
685 660
686 661
        assert!(line.contains(REVIEWED_MARK), "line: {line:?}");
687 662
        assert!(line.contains("main"), "line: {line:?}");
688 663
        assert!(!line.contains(&slot.commit.summary), "line: {line:?}");
689 664
690 -
        let mut hidden = Table::<10, Line>::new(TableOptions {
665 +
        let mut hidden = Table::<9, Line>::new(TableOptions {
691 666
            spacing: 2,
692 667
            ..TableOptions::default()
693 668
        });
694 669
        hidden.push(commit_row(&slot, DiffPalette::default(), "โ”€โ”€โ—", now as u64));
695 670
        assert_eq!(