brain: Refine history diffstats
620d1a8f407d819007137703cc0fc6085b7306f4
Show remaining change counts with proportional addition and deletion graphs normalized across the visible history. Keep the commit metadata legible while reserving space for review progress and diff statistics. Assisted-by: Codex:gpt-5.6-sol
1 parent
742c525a
crates/brain/src/backend.rs
+0 -0
f
+0 -0
empty
+0 -0
new.rs
+0 -0
image.png
+0 -0
crates/brain/src/session.rs
+39 -10
| 30 | 30 | use commit::{CommitReview, CommitSlot}; |
|
| 31 | 31 | use event::{ContextFetch, Resize, TerminalEvent, Update, context_change}; |
|
| 32 | 32 | use hunk::{CommentEdit, HunkView}; |
|
| 33 | 33 | pub(crate) use status::BRAIN_BADGE_BG; |
|
| 34 | 34 | use status::{ |
|
| 35 | - | ActionContext, Chrome, SELECTED_COMMIT_BG, Status, StatusContext, commit_row, |
|
| 36 | - | commit_row_suffix, commit_row_suffix_with_author, commit_table_lines, current_timestamp, |
|
| 37 | - | viewer_area, |
|
| 35 | + | ActionContext, Chrome, HISTORY_DIFFSTAT_MAX_WIDTH, SELECTED_COMMIT_BG, Status, StatusContext, |
|
| 36 | + | commit_row, commit_row_suffix, commit_row_suffix_with_author, commit_table_lines, |
|
| 37 | + | current_timestamp, viewer_area, |
|
| 38 | 38 | }; |
|
| 39 | 39 | ||
| 40 | 40 | /// Terminal backend used by the interactive viewer. |
|
| 41 | 41 | type BrainTerminal = Terminal<tui_termion::TermionBackend<tui_termion::term::TerminalScreen>>; |
|
| 42 | 42 | ||
| 43 | + | /// Minimum width of a history diffstat graph. |
|
| 44 | + | const HISTORY_DIFFSTAT_MIN_WIDTH: usize = 8; |
|
| 45 | + | /// Fraction of the history panel available to the diffstat graph. |
|
| 46 | + | const HISTORY_DIFFSTAT_WIDTH_DIVISOR: usize = 6; |
|
| 43 | 47 | /// Active view in the session. |
|
| 44 | 48 | enum Mode { |
|
| 45 | 49 | /// The commit list. |
|
| 46 | 50 | Commits, |
|
| 47 | 51 | /// The diff of the current commit. |
| 1086 | 1090 | } |
|
| 1087 | 1091 | Mode::Commits => { |
|
| 1088 | 1092 | let palette = self.options.diff_palette; |
|
| 1089 | 1093 | let now = current_timestamp(); |
|
| 1090 | 1094 | let last = self.commits.len() - 1; |
|
| 1095 | + | let largest_change_count = self |
|
| 1096 | + | .commits |
|
| 1097 | + | .iter() |
|
| 1098 | + | .map(|slot| { |
|
| 1099 | + | let stats = slot.remaining_stats(); |
|
| 1100 | + | stats.insertions + stats.deletions |
|
| 1101 | + | }) |
|
| 1102 | + | .max() |
|
| 1103 | + | .unwrap_or(0); |
|
| 1091 | 1104 | self.terminal.draw(|frame| { |
|
| 1092 | 1105 | let (body, chrome_area) = Chrome::areas(frame.area); |
|
| 1093 | 1106 | let cols = ScrollView::body_cols(body); |
|
| 1107 | + | let diffstat_width = (cols / HISTORY_DIFFSTAT_WIDTH_DIVISOR) |
|
| 1108 | + | .clamp(HISTORY_DIFFSTAT_MIN_WIDTH, HISTORY_DIFFSTAT_MAX_WIDTH); |
|
| 1094 | 1109 | let mut table = Table::<3, Line>::new(TableOptions { |
|
| 1095 | 1110 | spacing: 2, |
|
| 1096 | 1111 | ..TableOptions::default() |
|
| 1097 | 1112 | }); |
|
| 1098 | - | let mut author_suffix = Table::<6, Line>::new(TableOptions { |
|
| 1099 | - | spacing: 2, |
|
| 1100 | - | ..TableOptions::default() |
|
| 1101 | - | }); |
|
| 1102 | - | let mut suffix = Table::<5, Line>::new(TableOptions { |
|
| 1113 | + | let mut author_suffix = Table::<5, Line>::new(TableOptions { |
|
| 1103 | 1114 | spacing: 2, |
|
| 1104 | 1115 | ..TableOptions::default() |
|
| 1105 | 1116 | }); |
|
| 1106 | 1117 | for (index, slot) in self.commits.iter().enumerate() { |
|
| 1107 | 1118 | let graph = match (index, last) { |
| 1109 | 1120 | (0, _) => "╭─◉︎", |
|
| 1110 | 1121 | (index, last) if index == last => "╰─◉︎", |
|
| 1111 | 1122 | _ => "├─◉︎", |
|
| 1112 | 1123 | }; |
|
| 1113 | 1124 | table.push(commit_row(slot, graph)); |
|
| 1114 | - | author_suffix.push(commit_row_suffix_with_author(slot, palette, now)); |
|
| 1115 | - | suffix.push(commit_row_suffix(slot, palette, now)); |
|
| 1125 | + | author_suffix.push(commit_row_suffix_with_author( |
|
| 1126 | + | slot, |
|
| 1127 | + | palette, |
|
| 1128 | + | now, |
|
| 1129 | + | largest_change_count, |
|
| 1130 | + | diffstat_width, |
|
| 1131 | + | )); |
|
| 1116 | 1132 | } |
|
| 1117 | 1133 | let author_suffix_cols = author_suffix.size(Constraint::UNBOUNDED).cols; |
|
| 1118 | 1134 | let rows = if table.size(Constraint::UNBOUNDED).cols + author_suffix_cols + 2 |
|
| 1119 | 1135 | <= cols |
|
| 1120 | 1136 | { |
|
| 1121 | 1137 | commit_table_lines(&table, &author_suffix, cols) |
|
| 1122 | 1138 | } else { |
|
| 1139 | + | let mut suffix = Table::<4, Line>::new(TableOptions { |
|
| 1140 | + | spacing: 2, |
|
| 1141 | + | ..TableOptions::default() |
|
| 1142 | + | }); |
|
| 1143 | + | for slot in &self.commits { |
|
| 1144 | + | suffix.push(commit_row_suffix( |
|
| 1145 | + | slot, |
|
| 1146 | + | palette, |
|
| 1147 | + | now, |
|
| 1148 | + | largest_change_count, |
|
| 1149 | + | diffstat_width, |
|
| 1150 | + | )); |
|
| 1151 | + | } |
|
| 1123 | 1152 | commit_table_lines(&table, &suffix, cols) |
|
| 1124 | 1153 | }; |
|
| 1125 | 1154 | let selected = Style::default().bg(SELECTED_COMMIT_BG); |
|
| 1126 | 1155 | let list = List::new(rows) |
|
| 1127 | 1156 | .selected_styles(selected, selected) |
crates/brain/src/session/commit.rs
+7 -0
| 39 | 39 | self.document |
|
| 40 | 40 | .as_ref() |
|
| 41 | 41 | .or_else(|| self.loaded.as_ref().map(|loaded| &loaded.document)) |
|
| 42 | 42 | .ok_or_else(|| Error::Git("commit slot has no diff document".to_owned())) |
|
| 43 | 43 | } |
|
| 44 | + | ||
| 45 | + | /// Returns the remaining diff statistics in either slot state. |
|
| 46 | + | pub(super) fn remaining_stats(&self) -> DiffStat { |
|
| 47 | + | self.loaded |
|
| 48 | + | .as_ref() |
|
| 49 | + | .map_or(self.remaining_stats, CommitReview::remaining_stats) |
|
| 50 | + | } |
|
| 44 | 51 | } |
|
| 45 | 52 | ||
| 46 | 53 | /// Decoded review state for one commit: its viewer, accepted hunks, and the |
|
| 47 | 54 | /// full document used to restore hidden hunks. |
|
| 48 | 55 | /// |
a.rs
+0 -0
crates/brain/src/session/status.rs
+120 -27
| 2 | 2 | ||
| 3 | 3 | use std::cmp::Ordering; |
|
| 4 | 4 | use std::time::{SystemTime, UNIX_EPOCH}; |
|
| 5 | 5 | ||
| 6 | 6 | use rustik_diff::{ |
|
| 7 | - | DiffPalette, ReviewProgress, Stats, diffstat, file_count, progress_bar, recoverable_error, |
|
| 7 | + | DiffPalette, ReviewProgress, diffstat, file_count, progress_bar, recoverable_error, |
|
| 8 | 8 | }; |
|
| 9 | 9 | use rustik_repo::DiffStat; |
|
| 10 | 10 | use rustik_tui::{ |
|
| 11 | 11 | Border, Buffer, Color, Constraint, HStack, Label, Line, Rect, Size, Style, Table, TableOptions, |
|
| 12 | 12 | Widget, colors, |
|
| 13 | 13 | }; |
|
| 14 | 14 | ||
| 15 | 15 | use crate::comments::Comment; |
|
| 16 | 16 | use crate::review::Visibility; |
|
| 17 | 17 | ||
| 18 | - | use super::commit::{CommitReview, CommitSlot}; |
|
| 18 | + | use super::commit::CommitSlot; |
|
| 19 | 19 | ||
| 20 | 20 | /// Number of rows used by the status bar. |
|
| 21 | 21 | const STATUS_ROWS: usize = 1; |
|
| 22 | 22 | /// Number of rows used by the action bar. |
|
| 23 | 23 | const ACTION_ROWS: usize = 1; |
|
| 24 | 24 | /// Number of rows reserved for the bottom footer when possible. |
|
| 25 | 25 | const CHROME_ROWS: usize = STATUS_ROWS + ACTION_ROWS; |
|
| 26 | 26 | /// Empty columns between status content and the status bar's right edge. |
|
| 27 | 27 | const STATUS_RIGHT_PADDING: usize = 1; |
|
| 28 | + | /// Maximum width of a history diffstat graph. |
|
| 29 | + | pub(super) const HISTORY_DIFFSTAT_MAX_WIDTH: usize = 32; |
|
| 28 | 30 | /// Foreground color for dim footer text. |
|
| 29 | 31 | const FOOTER_FG: Color = Color::Fixed(240); |
|
| 32 | + | /// Foreground color for prominent history values. |
|
| 33 | + | const HISTORY_VALUE_FG: Color = Color::Fixed(246); |
|
| 30 | 34 | /// Background color for the footer row, matching file border chrome. |
|
| 31 | 35 | const FOOTER_BG: Color = colors::FAINT; |
|
| 32 | 36 | /// Background color for the brain application badge. |
|
| 33 | 37 | pub(crate) const BRAIN_BADGE_BG: Color = Color::RGB(124, 36, 32); |
|
| 34 | 38 | /// Background color for the active-mode badge. |
| 336 | 340 | Line::new(slot.commit.summary.as_str()), |
|
| 337 | 341 | ] |
|
| 338 | 342 | } |
|
| 339 | 343 | ||
| 340 | 344 | /// Returns the protected commit columns without an author column. |
|
| 341 | - | pub(super) fn commit_row_suffix(slot: &CommitSlot, palette: DiffPalette, now: u64) -> [Line; 5] { |
|
| 342 | - | let [_, age, branches, files, progress, diffstat] = |
|
| 343 | - | commit_row_suffix_with_author(slot, palette, now); |
|
| 344 | - | ||
| 345 | - | [age, branches, files, progress, diffstat] |
|
| 345 | + | pub(super) fn commit_row_suffix( |
|
| 346 | + | slot: &CommitSlot, |
|
| 347 | + | palette: DiffPalette, |
|
| 348 | + | now: u64, |
|
| 349 | + | largest_change_count: usize, |
|
| 350 | + | max_graph_width: usize, |
|
| 351 | + | ) -> [Line; 4] { |
|
| 352 | + | let [_, age, branches, progress, diffstat] = |
|
| 353 | + | commit_row_suffix_with_author(slot, palette, now, largest_change_count, max_graph_width); |
|
| 354 | + | [age, branches, progress, diffstat] |
|
| 346 | 355 | } |
|
| 347 | 356 | ||
| 348 | 357 | /// Returns the protected commit columns including an author column. |
|
| 349 | 358 | pub(super) fn commit_row_suffix_with_author( |
|
| 350 | 359 | slot: &CommitSlot, |
|
| 351 | 360 | palette: DiffPalette, |
|
| 352 | 361 | now: u64, |
|
| 353 | - | ) -> [Line; 6] { |
|
| 354 | - | let stats = slot |
|
| 355 | - | .loaded |
|
| 356 | - | .as_ref() |
|
| 357 | - | .map_or(slot.remaining_stats, CommitReview::remaining_stats); |
|
| 362 | + | largest_change_count: usize, |
|
| 363 | + | max_graph_width: usize, |
|
| 364 | + | ) -> [Line; 5] { |
|
| 365 | + | let stats = slot.remaining_stats(); |
|
| 358 | 366 | let diffstat = if slot.progress.is_complete() { |
|
| 359 | 367 | Line::blank() |
|
| 360 | 368 | } else { |
|
| 361 | - | diffstat(Stats::from(stats), palette) |
|
| 369 | + | history_diffstat(stats, largest_change_count, max_graph_width, palette) |
|
| 362 | 370 | }; |
|
| 363 | 371 | let status = if slot.progress.is_complete() { |
|
| 364 | 372 | Label::from_char(REVIEWED_MARK).fg(palette.positive_fg) |
|
| 365 | 373 | } else { |
|
| 366 | 374 | Label::space() |
| 370 | 378 | palette, |
|
| 371 | 379 | FOOTER_FG, |
|
| 372 | 380 | )); |
|
| 373 | 381 | ||
| 374 | 382 | [ |
|
| 375 | - | Line::new(Label::new(slot.commit.author.as_str()).fg(colors::fixed::MUTED)), |
|
| 383 | + | Line::new(Label::new(slot.commit.author.as_str()).fg(HISTORY_VALUE_FG)), |
|
| 376 | 384 | Line::new( |
|
| 377 | 385 | Label::from(relative_time( |
|
| 378 | 386 | slot.commit.recency.seconds.into(), |
|
| 379 | 387 | now.into(), |
|
| 380 | 388 | )) |
|
| 381 | - | .fg(colors::fixed::DIM), |
|
| 389 | + | .fg(colors::fixed::MUTED), |
|
| 382 | 390 | ), |
|
| 383 | 391 | Line::new(Label::new(&slot.commit.branches.join(", ")).fg(colors::rgb::TEAL)), |
|
| 384 | - | Line::new(Label::new(&file_count(stats.files_changed)).fg(FOOTER_FG)), |
|
| 385 | 392 | progress, |
|
| 386 | 393 | diffstat, |
|
| 387 | 394 | ] |
|
| 388 | 395 | } |
|
| 389 | 396 | ||
| 397 | + | /// Returns a change count and proportional addition and deletion graph. |
|
| 398 | + | fn history_diffstat( |
|
| 399 | + | stats: DiffStat, |
|
| 400 | + | largest_change_count: usize, |
|
| 401 | + | max_graph_width: usize, |
|
| 402 | + | palette: DiffPalette, |
|
| 403 | + | ) -> Line { |
|
| 404 | + | let change_count = stats.insertions + stats.deletions; |
|
| 405 | + | let change_kinds = usize::from(stats.insertions > 0) + usize::from(stats.deletions > 0); |
|
| 406 | + | let graph_width = if largest_change_count == 0 { |
|
| 407 | + | 0 |
|
| 408 | + | } else { |
|
| 409 | + | change_count |
|
| 410 | + | .saturating_mul(max_graph_width) |
|
| 411 | + | .div_ceil(largest_change_count) |
|
| 412 | + | .clamp(change_kinds, max_graph_width) |
|
| 413 | + | }; |
|
| 414 | + | let mut addition_width = if change_count == 0 { |
|
| 415 | + | 0 |
|
| 416 | + | } else { |
|
| 417 | + | stats |
|
| 418 | + | .insertions |
|
| 419 | + | .saturating_mul(graph_width) |
|
| 420 | + | .div_ceil(change_count) |
|
| 421 | + | .min(graph_width) |
|
| 422 | + | }; |
|
| 423 | + | if stats.deletions > 0 { |
|
| 424 | + | addition_width = addition_width.min(graph_width - 1); |
|
| 425 | + | } |
|
| 426 | + | let deletion_width = graph_width - addition_width; |
|
| 427 | + | let count_width = largest_change_count.max(1).ilog10() as usize + 1; |
|
| 428 | + | ||
| 429 | + | Line::new(Label::new(&format!("{change_count:>count_width$}")).fg(HISTORY_VALUE_FG)) |
|
| 430 | + | .item(Label::space()) |
|
| 431 | + | .item(Label::new(&"+".repeat(addition_width)).fg(palette.positive_fg)) |
|
| 432 | + | .item(Label::new(&"-".repeat(deletion_width)).fg(palette.negative_fg)) |
|
| 433 | + | } |
|
| 434 | + | ||
| 390 | 435 | /// Renders commit rows while reserving the right columns from truncation. |
|
| 391 | 436 | pub(super) fn commit_table_lines<const W: usize, const S: usize>( |
|
| 392 | 437 | metadata: &Table<W, Line>, |
|
| 393 | 438 | suffix: &Table<S, Line>, |
|
| 394 | 439 | cols: usize, |
| 658 | 703 | let mut table = Table::<3, Line>::new(TableOptions { |
|
| 659 | 704 | spacing: 2, |
|
| 660 | 705 | ..TableOptions::default() |
|
| 661 | 706 | }); |
|
| 662 | 707 | table.push(commit_row(&slot, "──◉︎")); |
|
| 663 | - | let mut suffix = Table::<6, Line>::new(TableOptions { |
|
| 708 | + | let mut suffix = Table::<5, Line>::new(TableOptions { |
|
| 664 | 709 | spacing: 2, |
|
| 665 | 710 | ..TableOptions::default() |
|
| 666 | 711 | }); |
|
| 667 | 712 | suffix.push(commit_row_suffix_with_author( |
|
| 668 | 713 | &slot, |
|
| 669 | 714 | DiffPalette::default(), |
|
| 670 | 715 | now as u64, |
|
| 716 | + | stats.insertions + stats.deletions, |
|
| 717 | + | 8, |
|
| 671 | 718 | )); |
|
| 672 | 719 | let wide_cols = |
|
| 673 | 720 | table.size(Constraint::UNBOUNDED).cols + suffix.size(Constraint::UNBOUNDED).cols + 2; |
|
| 674 | 721 | let wide = commit_table_lines(&table, &suffix, wide_cols) |
|
| 675 | 722 | .pop() |
| 680 | 727 | .pop() |
|
| 681 | 728 | .unwrap() |
|
| 682 | 729 | .to_string(); |
|
| 683 | 730 | ||
| 684 | 731 | assert!(wide.contains(&slot.commit.summary), "line: {wide:?}"); |
|
| 685 | - | assert!( |
|
| 686 | - | wide.ends_with("1 file ━━━━━━━━ +2 -3"), |
|
| 687 | - | "line: {wide:?}" |
|
| 688 | - | ); |
|
| 732 | + | assert!(wide.ends_with("━━━━━━━━ 5 ++++----"), "line: {wide:?}"); |
|
| 689 | 733 | assert!(narrow.contains("main"), "line: {narrow:?}"); |
|
| 690 | - | assert!( |
|
| 691 | - | narrow.ends_with("1 file ━━━━━━━━ +2 -3"), |
|
| 692 | - | "line: {narrow:?}" |
|
| 693 | - | ); |
|
| 734 | + | assert!(narrow.ends_with("━━━━━━━━ 5 ++++----"), "line: {narrow:?}"); |
|
| 694 | 735 | ||
| 695 | - | let mut hidden = Table::<5, Line>::new(TableOptions { |
|
| 736 | + | let mut hidden = Table::<4, Line>::new(TableOptions { |
|
| 696 | 737 | spacing: 2, |
|
| 697 | 738 | ..TableOptions::default() |
|
| 698 | 739 | }); |
|
| 699 | - | hidden.push(commit_row_suffix(&slot, DiffPalette::default(), now as u64)); |
|
| 740 | + | hidden.push(commit_row_suffix( |
|
| 741 | + | &slot, |
|
| 742 | + | DiffPalette::default(), |
|
| 743 | + | now as u64, |
|
| 744 | + | stats.insertions + stats.deletions, |
|
| 745 | + | 8, |
|
| 746 | + | )); |
|
| 700 | 747 | assert_eq!( |
|
| 701 | 748 | suffix.size(Constraint::UNBOUNDED).cols, |
|
| 702 | 749 | hidden.size(Constraint::UNBOUNDED).cols + "Alice".len() + 2 |
|
| 703 | 750 | ); |
|
| 704 | 751 | assert!( |
| 708 | 755 | .to_string() |
|
| 709 | 756 | .contains("Alice") |
|
| 710 | 757 | ); |
|
| 711 | 758 | } |
|
| 712 | 759 | ||
| 760 | + | #[test] |
|
| 761 | + | fn history_diffstat_scales_against_largest_commit() { |
|
| 762 | + | let palette = DiffPalette::default(); |
|
| 763 | + | let largest = DiffStat { |
|
| 764 | + | files_changed: 1, |
|
| 765 | + | insertions: 6, |
|
| 766 | + | deletions: 4, |
|
| 767 | + | }; |
|
| 768 | + | let smaller = DiffStat { |
|
| 769 | + | files_changed: 1, |
|
| 770 | + | insertions: 2, |
|
| 771 | + | deletions: 3, |
|
| 772 | + | }; |
|
| 773 | + | let deletion = DiffStat { |
|
| 774 | + | files_changed: 1, |
|
| 775 | + | insertions: 999, |
|
| 776 | + | deletions: 1, |
|
| 777 | + | }; |
|
| 778 | + | let small_mixed = DiffStat { |
|
| 779 | + | files_changed: 1, |
|
| 780 | + | insertions: 1, |
|
| 781 | + | deletions: 1, |
|
| 782 | + | }; |
|
| 783 | + | ||
| 784 | + | assert_eq!( |
|
| 785 | + | history_diffstat(largest, 10, 8, palette).to_string(), |
|
| 786 | + | "10 +++++---" |
|
| 787 | + | ); |
|
| 788 | + | assert_eq!( |
|
| 789 | + | history_diffstat(smaller, 10, 8, palette).to_string(), |
|
| 790 | + | " 5 ++--" |
|
| 791 | + | ); |
|
| 792 | + | assert_eq!( |
|
| 793 | + | history_diffstat(deletion, 1_000, 8, palette).to_string(), |
|
| 794 | + | "1000 +++++++-" |
|
| 795 | + | ); |
|
| 796 | + | assert_eq!( |
|
| 797 | + | history_diffstat(small_mixed, 1_000, 8, palette).to_string(), |
|
| 798 | + | " 2 +-" |
|
| 799 | + | ); |
|
| 800 | + | let wide = history_diffstat(largest, 10, 32, palette).to_string(); |
|
| 801 | + | let uncapped = history_diffstat(largest, 10, 64, palette).to_string(); |
|
| 802 | + | assert_eq!(wide.chars().count(), 35); |
|
| 803 | + | assert!(uncapped.chars().count() > wide.chars().count()); |
|
| 804 | + | } |
|
| 805 | + | ||
| 713 | 806 | #[test] |
|
| 714 | 807 | fn comment_card_lines_render_a_bordered_comment_card() { |
|
| 715 | 808 | let comment = Comment { |
|
| 716 | 809 | id: 1.into(), |
|
| 717 | 810 | author: "alice".to_owned(), |