brain: Show hunk progress in history

55247b1665d69cd6229d699045ea80163bb0dc96
Display accepted hunk percentages and totals in commit history so
review progress is visible at a glance. Match progress colors to
commit review state, including negative coloring for rejected commits.

Assisted-by: Codex:gpt-5.6-sol
Alexis Sellier committed ago 1 parent b872f27d
crates/brain/src/backend.rs +7 -1
420 420
    pub progress: ReviewProgress,
421 421
    /// Final review state of the commit.
422 422
    pub state: CommitReviewState,
423 423
    /// Diff statistics from hunks that remain unaccepted.
424 424
    pub stats: DiffStat,
425 +
    /// Number of accepted hunks in the commit.
426 +
    pub accepted_hunks: usize,
427 +
    /// Total number of hunks in the commit.
428 +
    pub total_hunks: usize,
425 429
}
426 430
427 431
impl HistoryStatus {
428 432
    /// Calculates history status from identities cached by the session.
429 433
    pub(crate) fn for_commits(
1102 1106
            .collect::<Vec<_>>();
1103 1107
        let reconciled = self.reconcile(&hunks);
1104 1108
        commits
1105 1109
            .iter()
1106 1110
            .zip(reconciled)
1107 -
            .map(|((commit, document, _), reconciled)| {
1111 +
            .map(|((commit, document, identities), reconciled)| {
1108 1112
                (
1109 1113
                    *commit,
1110 1114
                    CommitStatus {
1111 1115
                        progress: reconciled.progress,
1112 1116
                        state: reconciled.state,
1113 1117
                        stats: commit_remaining_stats(document, &reconciled.accepted),
1118 +
                        accepted_hunks: reconciled.accepted.len(),
1119 +
                        total_hunks: identities.len(),
1114 1120
                    },
1115 1121
                )
1116 1122
            })
1117 1123
            .collect()
1118 1124
    }
f +0 -0
new.rs +0 -0
image.png +0 -0
new.txt +0 -0
crates/brain/src/session.rs +14 -5
148 148
        .into_iter()
149 149
        .map(|commit| CommitSlot {
150 150
            remaining_stats: commit.stats,
151 151
            commit,
152 152
            progress: ReviewProgress::default(),
153 +
            accepted_hunks: 0,
154 +
            total_hunks: 0,
153 155
            state: CommitReviewState::Pending,
154 156
            document: None,
155 157
            identities: None,
156 158
            loaded: None,
157 159
        })
249 251
    commits: Vec<Commit>,
250 252
    backend: &dyn Backend,
251 253
    brain: &Brain,
252 254
    palette: rustik_diff::DiffPalette,
253 255
    width: usize,
254 -
) -> Result<Table<7, Line>, Error> {
256 +
) -> Result<Table<8, Line>, Error> {
255 257
    let (mut slots, _) = reviewed_slots(commits, backend, brain)?;
256 258
    slots.reverse();
257 259
    Ok(history_table(&slots, palette, width))
258 260
}
259 261
281 283
        .into_iter()
282 284
        .map(|(commit, document, identities)| {
283 285
            let status = history.commits.entry(commit.id).or_default();
284 286
            CommitSlot {
285 287
                progress: status.progress,
288 +
                accepted_hunks: status.accepted_hunks,
289 +
                total_hunks: status.total_hunks,
286 290
                state: status.state,
287 291
                remaining_stats: status.stats,
288 292
                commit,
289 293
                document: Some(document),
290 294
                identities: Some(identities),
298 302
/// Builds the commit table shared by interactive and regular history output.
299 303
fn history_table(
300 304
    commits: &[CommitSlot],
301 305
    palette: rustik_diff::DiffPalette,
302 306
    width: usize,
303 -
) -> Table<7, Line> {
307 +
) -> Table<8, Line> {
304 308
    let now = current_timestamp();
305 309
    let largest_change_count = commits
306 310
        .iter()
307 311
        .map(|slot| {
308 312
            let stats = slot.remaining_stats();
310 314
        })
311 315
        .max()
312 316
        .unwrap_or(0);
313 317
    let diffstat_width = (width / HISTORY_DIFFSTAT_WIDTH_DIVISOR)
314 318
        .clamp(HISTORY_DIFFSTAT_MIN_WIDTH, HISTORY_DIFFSTAT_MAX_WIDTH);
315 -
    let mut table = Table::<7, Line>::new(TableOptions {
319 +
    let mut table = Table::<8, Line>::new(TableOptions {
316 320
        spacing: 2,
317 321
        ..TableOptions::default()
318 322
    })
319 323
    .with_layout([
320 324
        Column::auto(),
322 326
        Column::flex(1),
323 327
        Column::collapse(2),
324 328
        Column::collapse(3),
325 329
        Column::collapse(1),
326 330
        Column::collapse(4),
331 +
        Column::auto(),
327 332
    ]);
328 333
    for slot in commits {
329 334
        let [state, id, summary] = commit_row(slot, palette);
330 -
        let [author, age, branches, diffstat] =
335 +
        let [author, age, branches, diffstat, hunks] =
331 336
            commit_row_suffix(slot, palette, now, largest_change_count, diffstat_width);
332 -
        table.push([state, id, summary, author, age, branches, diffstat]);
337 +
        table.push([state, id, summary, author, age, branches, diffstat, hunks]);
333 338
    }
334 339
    table
335 340
}
336 341
337 342
/// Commit-local hunk location used by acceptance and undo state.
1111 1116
                .collect::<Result<Vec<_>, Error>>()?;
1112 1117
            let status = HistoryStatus::for_commits(&self.brain, &status_inputs);
1113 1118
            for slot in &mut self.commits {
1114 1119
                if let Some(commit) = status.commits.get(&slot.commit.id) {
1115 1120
                    slot.progress = commit.progress;
1121 +
                    slot.accepted_hunks = commit.accepted_hunks;
1122 +
                    slot.total_hunks = commit.total_hunks;
1116 1123
                    slot.state = commit.state;
1117 1124
                    slot.remaining_stats = commit.stats;
1118 1125
                }
1119 1126
            }
1120 1127
            self.history_status = Some(status);
1461 1468
            })
1462 1469
            .collect::<Vec<_>>();
1463 1470
        let reconciled = self.brain.reconcile(&commits);
1464 1471
        for (slot, reconciled) in self.commits.iter_mut().zip(reconciled) {
1465 1472
            slot.progress = reconciled.progress;
1473 +
            slot.accepted_hunks = reconciled.accepted.len();
1474 +
            slot.total_hunks = slot.hunk_count();
1466 1475
            slot.state = reconciled.state;
1467 1476
            if let Some(loaded) = &mut slot.loaded {
1468 1477
                loaded.review.replace(reconciled.accepted);
1469 1478
                loaded.refresh_decorations();
1470 1479
            }
a.rs +0 -0
crates/brain/src/session/commit.rs +16 -0
20 20
    pub(super) commit: Commit,
21 21
    /// Accepted-line progress for this commit.
22 22
    ///
23 23
    /// Persisted line totals provide progress before the diff is decoded.
24 24
    pub(super) progress: ReviewProgress,
25 +
    /// Number of accepted hunks in this commit.
26 +
    pub(super) accepted_hunks: usize,
27 +
    /// Total number of hunks in this commit.
28 +
    pub(super) total_hunks: usize,
25 29
    /// Final review state of this commit.
26 30
    pub(super) state: CommitReviewState,
27 31
    /// Remaining diff statistics for this commit.
28 32
    pub(super) remaining_stats: DiffStat,
29 33
    /// Split diff document retained from history progress calculation.
49 53
            .as_ref()
50 54
            .or_else(|| self.loaded.as_ref().map(|loaded| &loaded.identities))
51 55
            .ok_or_else(|| Error::Git("commit slot has no cached identities".to_owned()))
52 56
    }
53 57
58 +
    /// Returns the number of canonical hunks cached for this commit.
59 +
    pub(super) fn hunk_count(&self) -> usize {
60 +
        self.identities.as_ref().map_or_else(
61 +
            || {
62 +
                self.loaded
63 +
                    .as_ref()
64 +
                    .map_or(0, |loaded| loaded.identities.len())
65 +
            },
66 +
            BTreeMap::len,
67 +
        )
68 +
    }
69 +
54 70
    /// Returns the remaining diff statistics in either slot state.
55 71
    pub(super) fn remaining_stats(&self) -> DiffStat {
56 72
        self.loaded
57 73
            .as_ref()
58 74
            .map_or(self.remaining_stats, CommitReview::remaining_stats)
a.rs +0 -0
crates/brain/src/session/status.rs +63 -4
26 26
const CHROME_ROWS: usize = STATUS_ROWS + ACTION_ROWS;
27 27
/// Empty columns between status content and the status bar's right edge.
28 28
const STATUS_RIGHT_PADDING: usize = 1;
29 29
/// Maximum width of a history diffstat graph.
30 30
pub(super) const HISTORY_DIFFSTAT_MAX_WIDTH: usize = 32;
31 +
/// Columns reserved for the total hunk count in commit history.
32 +
const HISTORY_HUNK_COUNT_WIDTH: usize = 4;
31 33
/// Foreground color for dim footer text.
32 34
const FOOTER_FG: Color = Color::Fixed(240);
33 35
/// Foreground color for prominent history values.
34 36
const HISTORY_VALUE_FG: Color = Color::Fixed(246);
35 37
/// Background color for the footer row, matching file border chrome.
56 58
const DROPPED_MARK: char = '-';
57 59
/// Marker shown beside a commit that has not been reviewed.
58 60
const PENDING_MARK: char = '○';
59 61
/// Marker shown beside a commit whose review is in progress.
60 62
const IN_PROGRESS_MARK: char = '◐';
63 +
/// Marker identifying a hunk count in commit history: ▤.
64 +
const HUNK_MARK: char = '▤';
61 65
/// Marker highlighted when all hunks are visible in the footer: ✔.
62 66
const ALL_HUNKS_MODE_ICON: &str = "✔";
63 67
/// Marker shown when full-file mode is visible in the footer: ↕.
64 68
const FULL_FILE_MODE_ICON: &str = "↕";
65 69
/// Separator shown between footer status segments: ·.
388 392
            Style::default()
389 393
        }),
390 394
    ]
391 395
}
392 396
393 -
/// Returns the optional right-side columns for one commit-table row.
397 +
/// Returns the right-side columns for one commit-table row.
394 398
///
395 399
/// Columns are ordered by their wide-layout position: author, age, branches,
396 -
/// and diffstat.
400 +
/// diffstat, and hunk progress.
397 401
pub(super) fn commit_row_suffix(
398 402
    slot: &CommitSlot,
399 403
    palette: DiffPalette,
400 404
    now: u64,
401 405
    largest_change_count: usize,
402 406
    max_graph_width: usize,
403 -
) -> [Line; 4] {
407 +
) -> [Line; 5] {
404 408
    let stats = slot.remaining_stats();
405 409
    let diffstat = match slot.state {
406 410
        CommitReviewState::Pending => {
407 411
            history_diffstat(stats, largest_change_count, max_graph_width, palette)
408 412
        }
419 423
    let identity_style = if matches!(slot.state, CommitReviewState::Dropped) {
420 424
        Style::default().dim().strikethrough()
421 425
    } else {
422 426
        Style::default()
423 427
    };
428 +
    let percent = slot
429 +
        .accepted_hunks
430 +
        .saturating_mul(100)
431 +
        .checked_div(slot.total_hunks)
432 +
        .unwrap_or(0);
433 +
    let hunk_color = match (slot.state, percent) {
434 +
        (CommitReviewState::Rejected, _) => palette.negative_fg,
435 +
        (_, 0) => colors::fixed::MUTED,
436 +
        (_, 100) => palette.positive_fg,
437 +
        _ => colors::rgb::ORANGE,
438 +
    };
439 +
    let hunks = Line::new(
440 +
        Label::new(&format!(
441 +
            "{HUNK_MARK} {percent:>3}%/{:<HISTORY_HUNK_COUNT_WIDTH$}",
442 +
            slot.total_hunks
443 +
        ))
444 +
        .fg(hunk_color),
445 +
    );
424 446
    [
425 447
        Line::new(Label::new(slot.commit.author.as_str()).fg(HISTORY_VALUE_FG))
426 448
            .style(identity_style),
427 449
        Line::new(
428 450
            Label::from(relative_time(
433 455
        )
434 456
        .style(identity_style),
435 457
        Line::new(Label::new(&slot.commit.branches.join(", ")).fg(colors::rgb::TEAL))
436 458
            .style(row_style),
437 459
        diffstat.style(row_style),
460 +
        hunks,
438 461
    ]
439 462
}
440 463
441 464
/// Returns a change count and proportional addition and deletion graph.
442 465
fn history_diffstat(
705 728
            },
706 729
            progress: ReviewProgress {
707 730
                accepted: 0,
708 731
                total: 1,
709 732
            },
733 +
            accepted_hunks: 0,
734 +
            total_hunks: 1,
710 735
            state: CommitReviewState::Pending,
711 736
            remaining_stats: stats,
712 737
            document: None,
713 738
            identities: None,
714 739
            loaded: None,
735 760
                .is_empty()
736 761
            );
737 762
        }
738 763
739 764
        slot.state = CommitReviewState::Dropped;
740 -
        let [mut author, mut age, _, diffstat] = commit_row_suffix(
765 +
        let [mut author, mut age, _, diffstat, hunks] = commit_row_suffix(
741 766
            &slot,
742 767
            DiffPalette::default(),
743 768
            now as u64,
744 769
            stats.insertions + stats.deletions,
745 770
            8,
747 772
        assert_eq!(
748 773
            commit_row(&slot, DiffPalette::default())[0].to_string(),
749 774
            DROPPED_MARK.to_string()
750 775
        );
751 776
        assert!(diffstat.to_string().is_empty());
777 +
        assert_eq!(hunks.to_string(), "▤   0%/1   ");
752 778
753 779
        let mut author_buffer = Buffer::empty(Rect::new(0, 0, slot.commit.author.len(), 1));
754 780
        let mut age_buffer = Buffer::empty(Rect::new(0, 0, 16, 1));
755 781
        author.render(author_buffer.area(), &mut author_buffer);
756 782
        age.render(age_buffer.area(), &mut age_buffer);
775 801
        slot.progress.accepted = 1;
776 802
        assert_eq!(
777 803
            commit_row(&slot, DiffPalette::default())[0].to_string(),
778 804
            IN_PROGRESS_MARK.to_string()
779 805
        );
806 +
807 +
        for (state, accepted_hunks, expected_color) in [
808 +
            (CommitReviewState::Pending, 0, colors::fixed::MUTED),
809 +
            (CommitReviewState::Pending, 1, colors::rgb::ORANGE),
810 +
            (
811 +
                CommitReviewState::Pending,
812 +
                2,
813 +
                DiffPalette::default().positive_fg,
814 +
            ),
815 +
            (
816 +
                CommitReviewState::Rejected,
817 +
                0,
818 +
                DiffPalette::default().negative_fg,
819 +
            ),
820 +
        ] {
821 +
            slot.state = state;
822 +
            slot.accepted_hunks = accepted_hunks;
823 +
            slot.total_hunks = 2;
824 +
            let [_, _, _, _, mut hunks] = commit_row_suffix(
825 +
                &slot,
826 +
                DiffPalette::default(),
827 +
                now as u64,
828 +
                stats.insertions + stats.deletions,
829 +
                8,
830 +
            );
831 +
            let mut buffer = Buffer::empty(Rect::new(0, 0, 16, 1));
832 +
            hunks.render(buffer.area(), &mut buffer);
833 +
834 +
            assert_eq!(
835 +
                buffer.get(0, 0).unwrap().style(),
836 +
                Style::default().fg(expected_color)
837 +
            );
838 +
        }
780 839
    }
781 840
782 841
    #[test]
783 842
    fn history_diffstat_scales_against_largest_commit() {
784 843
        let palette = DiffPalette::default();