diff: Share status presentation primitives
742c525ac1e5a099da9a945a48965886c3a54012
Use one implementation for diff statistics, changed-file labels, and review progress bars across the diff pager and Brain commands. Assisted-by: Codex:gpt-5.6-sol
1 parent
c25693bd
crates/brain/src/main.rs
+2 -2
| 20 | 20 | use std::ffi::{OsStr, OsString}; |
|
| 21 | 21 | use std::io::{self, Write as _}; |
|
| 22 | 22 | use std::process::ExitCode; |
|
| 23 | 23 | ||
| 24 | 24 | use lexopt::prelude::*; |
|
| 25 | - | use rustik_diff::{Builtins, DiffOptions, DiffPalette, FileTreeMode, HunkFocus}; |
|
| 25 | + | use rustik_diff::{Builtins, DiffOptions, DiffPalette, FileTreeMode, HunkFocus, progress_bar}; |
|
| 26 | 26 | use rustik_repo::{AheadBehind, MergeStatus, Rev}; |
|
| 27 | 27 | use rustik_tui::{ |
|
| 28 | 28 | ActionList, Border, Color, Constraint, Dialog, Event, Key, Label, Line, Size, Style, Table, |
|
| 29 | 29 | TableOptions, Terminal, colors, |
|
| 30 | 30 | }; |
| 314 | 314 | brain_state(status.state), |
|
| 315 | 315 | status_head(status), |
|
| 316 | 316 | review_status(status), |
|
| 317 | 317 | ahead_behind(status.relation), |
|
| 318 | 318 | comment_status(status.comments), |
|
| 319 | - | session::progress_bar(status.progress, palette, colors::fixed::DIM), |
|
| 319 | + | progress_bar(status.progress, palette, colors::fixed::DIM), |
|
| 320 | 320 | ]); |
|
| 321 | 321 | } |
|
| 322 | 322 | table.lines(Constraint::max(Size::new(width, usize::MAX))) |
|
| 323 | 323 | } |
|
| 324 | 324 |
crates/brain/src/session.rs
+1 -1
| 28 | 28 | mod status; |
|
| 29 | 29 | ||
| 30 | 30 | use commit::{CommitReview, CommitSlot}; |
|
| 31 | 31 | use event::{ContextFetch, Resize, TerminalEvent, Update, context_change}; |
|
| 32 | 32 | use hunk::{CommentEdit, HunkView}; |
|
| 33 | + | pub(crate) use status::BRAIN_BADGE_BG; |
|
| 33 | 34 | use status::{ |
|
| 34 | 35 | ActionContext, Chrome, SELECTED_COMMIT_BG, Status, StatusContext, commit_row, |
|
| 35 | 36 | commit_row_suffix, commit_row_suffix_with_author, commit_table_lines, current_timestamp, |
|
| 36 | 37 | viewer_area, |
|
| 37 | 38 | }; |
|
| 38 | - | pub(crate) use status::{BRAIN_BADGE_BG, progress_bar}; |
|
| 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 | 43 | /// Active view in the session. |
crates/brain/src/session/status.rs
+7 -48
| 1 | 1 | //! Status and chrome rendering for the review session. |
|
| 2 | 2 | ||
| 3 | 3 | use std::cmp::Ordering; |
|
| 4 | 4 | use std::time::{SystemTime, UNIX_EPOCH}; |
|
| 5 | 5 | ||
| 6 | - | use rustik_diff::{DiffPalette, ReviewProgress}; |
|
| 6 | + | use rustik_diff::{ |
|
| 7 | + | DiffPalette, ReviewProgress, Stats, diffstat, file_count, progress_bar, recoverable_error, |
|
| 8 | + | }; |
|
| 7 | 9 | use rustik_repo::DiffStat; |
|
| 8 | 10 | use rustik_tui::{ |
|
| 9 | 11 | Border, Buffer, Color, Constraint, HStack, Label, Line, Rect, Size, Style, Table, TableOptions, |
|
| 10 | 12 | Widget, colors, |
|
| 11 | 13 | }; |
| 45 | 47 | const REVIEWED_MARK: char = '✔'; |
|
| 46 | 48 | /// Marker shown when accepted hunks are visible in the footer: ✔. |
|
| 47 | 49 | const APPROVED_HUNKS_MODE_ICON: &str = "✔"; |
|
| 48 | 50 | /// Marker shown when full-file mode is visible in the footer: ↕. |
|
| 49 | 51 | const FULL_FILE_MODE_ICON: &str = "↕"; |
|
| 50 | - | /// Width of the commit review progress bar in the footer. |
|
| 51 | - | const COMMIT_PROGRESS_BAR_WIDTH: usize = 8; |
|
| 52 | - | /// Glyph text used by commit review progress bars: ━. |
|
| 53 | - | const COMMIT_PROGRESS_BAR_GLYPH: &str = "━"; |
|
| 54 | 52 | /// Separator shown between footer status segments: ·. |
|
| 55 | 53 | const FOOTER_SEPARATOR: &str = " · "; |
|
| 56 | 54 | /// Marker used when footer text truncates: …. |
|
| 57 | 55 | const FOOTER_ELLIPSIS: &str = "…"; |
|
| 58 | 56 | /// Separator shown between plain action hints. |
| 163 | 161 | } |
|
| 164 | 162 | ||
| 165 | 163 | /// Returns the bottom status row content. |
|
| 166 | 164 | fn bar(&self, cols: usize) -> HStack<'_> { |
|
| 167 | 165 | if let Some(error) = &self.error { |
|
| 168 | - | let message = format!(" {error} "); |
|
| 169 | - | return HStack::default().child( |
|
| 170 | - | Line::blank() |
|
| 171 | - | .item( |
|
| 172 | - | Label::new(" error ") |
|
| 173 | - | .style(Style::default().fg(Color::White).bg(Color::Red).bold()), |
|
| 174 | - | ) |
|
| 175 | - | .item( |
|
| 176 | - | Label::new(&message) |
|
| 177 | - | .style(Style::default().fg(FOOTER_VALUE_FG).bg(FOOTER_BG)), |
|
| 178 | - | ) |
|
| 179 | - | .truncated(cols, ""), |
|
| 180 | - | ); |
|
| 166 | + | return HStack::default() |
|
| 167 | + | .child(recoverable_error(error, FOOTER_VALUE_FG, FOOTER_BG).truncated(cols, "")); |
|
| 181 | 168 | } |
|
| 182 | 169 | let right = self.right_status(); |
|
| 183 | 170 | let left_width = cols |
|
| 184 | 171 | .saturating_sub(right.width()) |
|
| 185 | 172 | .saturating_sub(STATUS_RIGHT_PADDING) |
| 234 | 221 | let files = file_count(self.stats.files_changed); |
|
| 235 | 222 | ||
| 236 | 223 | Line::blank() |
|
| 237 | 224 | .extend(progress_bar(self.progress, self.palette, FOOTER_FG)) |
|
| 238 | 225 | .item(Label::space()) |
|
| 239 | - | .item(Label::new(&format!("+{}", self.stats.insertions)).fg(self.palette.positive_fg)) |
|
| 240 | - | .item(Label::space()) |
|
| 241 | - | .item(Label::new(&format!("-{}", self.stats.deletions)).fg(self.palette.negative_fg)) |
|
| 226 | + | .extend(diffstat(self.stats.into(), self.palette)) |
|
| 242 | 227 | .item(Label::new(FOOTER_SEPARATOR).fg(FOOTER_FG)) |
|
| 243 | 228 | .item(Label::new(&files).fg(FOOTER_VALUE_FG)) |
|
| 244 | 229 | .item(Label::new(FOOTER_SEPARATOR).fg(FOOTER_FG)) |
|
| 245 | 230 | .item(Label::new(APPROVED_HUNKS_MODE_ICON).fg(accepted_fg)) |
|
| 246 | 231 | .item(Label::space()) |
| 336 | 321 | line.render(action_area, buf); |
|
| 337 | 322 | } |
|
| 338 | 323 | } |
|
| 339 | 324 | } |
|
| 340 | 325 | ||
| 341 | - | /// Formats a changed-file count with the correct singular or plural noun. |
|
| 342 | - | fn file_count(files: usize) -> String { |
|
| 343 | - | let plural = if files == 1 { "" } else { "s" }; |
|
| 344 | - | format!("{files} file{plural}") |
|
| 345 | - | } |
|
| 346 | - | ||
| 347 | - | /// Returns a compact commit review progress bar. |
|
| 348 | - | /// |
|
| 349 | - | /// The empty segment renders in `empty_fg`, chosen by the caller to fit its |
|
| 350 | - | /// background. |
|
| 351 | - | pub(crate) fn progress_bar( |
|
| 352 | - | progress: ReviewProgress, |
|
| 353 | - | palette: DiffPalette, |
|
| 354 | - | empty_fg: Color, |
|
| 355 | - | ) -> Line { |
|
| 356 | - | let filled = progress.filled(COMMIT_PROGRESS_BAR_WIDTH); |
|
| 357 | - | let empty = COMMIT_PROGRESS_BAR_WIDTH - filled; |
|
| 358 | - | ||
| 359 | - | Line::blank() |
|
| 360 | - | .item(Label::new(&COMMIT_PROGRESS_BAR_GLYPH.repeat(filled)).fg(palette.positive_fg)) |
|
| 361 | - | .item(Label::new(&COMMIT_PROGRESS_BAR_GLYPH.repeat(empty)).fg(empty_fg)) |
|
| 362 | - | } |
|
| 363 | - | ||
| 364 | 326 | /// Returns the viewer area above the footer chrome. |
|
| 365 | 327 | pub(super) fn viewer_area(area: Rect) -> Rect { |
|
| 366 | 328 | Chrome::areas(area).0 |
|
| 367 | 329 | } |
|
| 368 | 330 |
| 394 | 356 | .as_ref() |
|
| 395 | 357 | .map_or(slot.remaining_stats, CommitReview::remaining_stats); |
|
| 396 | 358 | let diffstat = if slot.progress.is_complete() { |
|
| 397 | 359 | Line::blank() |
|
| 398 | 360 | } else { |
|
| 399 | - | Line::blank() |
|
| 400 | - | .item(Label::new(&format!("+{}", stats.insertions)).fg(palette.positive_fg)) |
|
| 401 | - | .item(Label::space()) |
|
| 402 | - | .item(Label::new(&format!("-{}", stats.deletions)).fg(palette.negative_fg)) |
|
| 361 | + | diffstat(Stats::from(stats), palette) |
|
| 403 | 362 | }; |
|
| 404 | 363 | let status = if slot.progress.is_complete() { |
|
| 405 | 364 | Label::from_char(REVIEWED_MARK).fg(palette.positive_fg) |
|
| 406 | 365 | } else { |
|
| 407 | 366 | Label::space() |
crates/diff/src/bin/rustik-diff/session.rs
+4 -16
| 5 | 5 | use std::thread; |
|
| 6 | 6 | use std::time::{Duration, Instant}; |
|
| 7 | 7 | ||
| 8 | 8 | use rustik_diff::{ |
|
| 9 | 9 | ContextChange, DiffPalette, DiffViewer, HighlightOutput, HighlightRequest, Highlighter, Stats, |
|
| 10 | - | spawn_highlighter, |
|
| 10 | + | diffstat, file_count, recoverable_error, spawn_highlighter, |
|
| 11 | 11 | }; |
|
| 12 | 12 | use rustik_repo::git; |
|
| 13 | 13 | use rustik_tui as tui; |
|
| 14 | 14 | use rustik_tui::{ |
|
| 15 | 15 | Color, Constraint, Control, Event, HStack, Key, Label, Line, Mouse, MouseButton, Rect, Style, |
| 304 | 304 | palette: DiffPalette, |
|
| 305 | 305 | debug: Option<&str>, |
|
| 306 | 306 | error: Option<&str>, |
|
| 307 | 307 | ) -> Line { |
|
| 308 | 308 | if let Some(error) = error { |
|
| 309 | - | let message = format!(" {error} "); |
|
| 310 | - | return Line::blank() |
|
| 311 | - | .item( |
|
| 312 | - | Label::new(" error ") |
|
| 313 | - | .style(Style::default().fg(Color::White).bg(Color::Red).bold()), |
|
| 314 | - | ) |
|
| 315 | - | .item(Label::new(&message).style(Style::default().fg(FOOTER_VALUE_FG).bg(FOOTER_BG))) |
|
| 316 | - | .style(Style::default().bg(FOOTER_BG)) |
|
| 309 | + | return recoverable_error(error, FOOTER_VALUE_FG, FOOTER_BG) |
|
| 317 | 310 | .padded(cols) |
|
| 318 | 311 | .truncated(cols, ""); |
|
| 319 | 312 | } |
|
| 320 | 313 | let badge = Line::new( |
|
| 321 | 314 | Label::new(" diff ") |
| 339 | 332 | .truncated(cols, "") |
|
| 340 | 333 | } |
|
| 341 | 334 | ||
| 342 | 335 | /// Returns the right-aligned status data for the footer. |
|
| 343 | 336 | fn status_stats(stats: Stats, palette: DiffPalette, debug: Option<&str>) -> Line { |
|
| 344 | - | let files = stats.files_changed; |
|
| 345 | - | let plural = if files == 1 { "" } else { "s" }; |
|
| 346 | - | let mut line = Line::blank() |
|
| 347 | - | .item(Label::new(&format!("+{}", stats.insertions)).fg(palette.positive_fg)) |
|
| 348 | - | .item(Label::space()) |
|
| 349 | - | .item(Label::new(&format!("-{}", stats.deletions)).fg(palette.negative_fg)) |
|
| 337 | + | let mut line = diffstat(stats, palette) |
|
| 350 | 338 | .item(Label::new(FOOTER_SEPARATOR).fg(FOOTER_FG)) |
|
| 351 | - | .item(Label::new(&format!("{files} file{plural}")).fg(FOOTER_VALUE_FG)); |
|
| 339 | + | .item(Label::new(&file_count(stats.files_changed)).fg(FOOTER_VALUE_FG)); |
|
| 352 | 340 | if let Some(debug) = debug { |
|
| 353 | 341 | line = line |
|
| 354 | 342 | .item(Label::new(FOOTER_SEPARATOR).fg(FOOTER_FG)) |
|
| 355 | 343 | .item(Label::new(debug).fg(FOOTER_FG)); |
|
| 356 | 344 | } |
crates/diff/src/document.rs
+11 -0
| 4 | 4 | //! that callers consume. The decoder that produces these values lives in |
|
| 5 | 5 | //! [`crate::decode`]; `document` is intentionally model-only. |
|
| 6 | 6 | ||
| 7 | 7 | pub use rustik_repo::{Oid, OidError}; |
|
| 8 | 8 | ||
| 9 | + | use rustik_repo::DiffStat; |
|
| 9 | 10 | use std::fmt; |
|
| 10 | 11 | use std::io; |
|
| 11 | 12 | use std::num::ParseIntError; |
|
| 12 | 13 | use std::path::PathBuf; |
|
| 13 | 14 |
| 176 | 177 | pub insertions: usize, |
|
| 177 | 178 | /// Total deletion lines across all files. |
|
| 178 | 179 | pub deletions: usize, |
|
| 179 | 180 | } |
|
| 180 | 181 | ||
| 182 | + | impl From<DiffStat> for Stats { |
|
| 183 | + | fn from(stats: DiffStat) -> Self { |
|
| 184 | + | Self { |
|
| 185 | + | files_changed: stats.files_changed, |
|
| 186 | + | insertions: stats.insertions, |
|
| 187 | + | deletions: stats.deletions, |
|
| 188 | + | } |
|
| 189 | + | } |
|
| 190 | + | } |
|
| 191 | + | ||
| 181 | 192 | /// A unified diff hunk and its body lines. |
|
| 182 | 193 | #[derive(Clone, Debug, Eq, PartialEq)] |
|
| 183 | 194 | pub struct Hunk { |
|
| 184 | 195 | /// Stable hunk identifier. |
|
| 185 | 196 | pub id: HunkId, |
a.txt
+0 -0
a
+0 -0
crates/diff/src/lib.rs
+4 -0
| 20 | 20 | mod decorate; |
|
| 21 | 21 | mod document; |
|
| 22 | 22 | mod highlight; |
|
| 23 | 23 | #[cfg(feature = "builtins")] |
|
| 24 | 24 | mod highlighter; |
|
| 25 | + | mod status; |
|
| 25 | 26 | mod viewer; |
|
| 26 | 27 | mod word; |
|
| 27 | 28 | ||
| 28 | 29 | pub mod decode; |
|
| 29 | 30 | pub mod encode; |
| 43 | 44 | BuiltinHighlighter, DiffHighlighter, DiffSide, HighlightKey, HighlightKeyInput, |
|
| 44 | 45 | HighlightRequest, HighlightResult, HighlightSource, |
|
| 45 | 46 | }; |
|
| 46 | 47 | #[cfg(feature = "builtins")] |
|
| 47 | 48 | pub use highlighter::{HighlightOutput, Highlighter, spawn_highlighter}; |
|
| 49 | + | pub use status::{ |
|
| 50 | + | REVIEW_PROGRESS_BAR_GLYPH, diffstat, file_count, progress_bar, recoverable_error, |
|
| 51 | + | }; |
|
| 48 | 52 | pub use viewer::{ |
|
| 49 | 53 | ContextRequest, ContextUpdate, DiffAction, DiffFocus, DiffOptions, DiffPalette, DiffState, |
|
| 50 | 54 | DiffViewer, FileTree, FileTreeMode, FileTreeTarget, HunkFocus, ViewChange, |
|
| 51 | 55 | }; |
crates/diff/src/status.rs
added
+44 -0
| 1 | + | //! Shared diff status presentation primitives. |
|
| 2 | + | ||
| 3 | + | use rustik_tui::{Color, Label, Line, Style}; |
|
| 4 | + | ||
| 5 | + | use crate::{DiffPalette, ReviewProgress, Stats}; |
|
| 6 | + | ||
| 7 | + | /// Glyph used for filled and empty review progress segments. |
|
| 8 | + | pub const REVIEW_PROGRESS_BAR_GLYPH: &str = "━"; |
|
| 9 | + | ||
| 10 | + | /// Returns a recoverable-error status line with the standard error badge. |
|
| 11 | + | pub 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. |
|
| 20 | + | const REVIEW_PROGRESS_BAR_WIDTH: usize = 8; |
|
| 21 | + | ||
| 22 | + | /// Returns a compact addition and deletion summary. |
|
| 23 | + | pub 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. |
|
| 31 | + | pub 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. |
|
| 37 | + | pub 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 | + | } |