brain: Add history status output
ec3a646af1005d53c5ffa3de2312804270ff1302
Expose the interactive history table through a regular CLI command so review state can be inspected without opening the terminal interface. Assisted-by: Codex:gpt-5.6
1 parent
5c60c517
crates/brain/src/main.rs
+57 -11
| 1 | 1 | //! Binary entry point for the `brain` code-review TUI. |
|
| 2 | 2 | //! |
|
| 3 | 3 | //! `brain diff <target>` opens an interactive review of the changes from the |
|
| 4 | 4 | //! target branch's base commit. An explicit base can be supplied as |
|
| 5 | 5 | //! `brain diff <base> <target>`. |
|
| 6 | + | //! `brain status <base> <target>` prints the same commit history without |
|
| 7 | + | //! opening the interactive viewer. |
|
| 6 | 8 | ||
| 7 | 9 | // `serde` is the local alias for the `microserde` crate; the derive macro |
|
| 8 | 10 | // emits `microserde::*` paths so the original crate name must remain in scope. |
|
| 9 | 11 | extern crate serde as microserde; |
|
| 10 | 12 |
| 37 | 39 | ||
| 38 | 40 | /// Terminal backend used by the interactive viewer. |
|
| 39 | 41 | type BrainTerminal = Terminal<tui_termion::TermionBackend<tui_termion::term::TerminalScreen>>; |
|
| 40 | 42 | ||
| 41 | 43 | /// Command-line usage text. |
|
| 42 | - | const USAGE: &str = |
|
| 43 | - | "usage: brain [status [--all]]\n brain [--reset|--rebase] diff [<base>] <target>"; |
|
| 44 | + | const USAGE: &str = "usage: brain [status [--all]]\n brain status <base> <target>\n brain [--reset|--rebase] diff [<base>] <target>"; |
|
| 44 | 45 | /// Default output width used when terminal width is unknown. |
|
| 45 | 46 | const DEFAULT_STATUS_WIDTH: usize = 140; |
|
| 46 | 47 | /// Marker shown for approved review state: ✔. |
|
| 47 | 48 | const APPROVED_STATUS_ICON: char = '✔'; |
|
| 48 | 49 | /// Marker shown for in-review state: ●. |
| 68 | 69 | lexopt::Parser::from_iter([OsString::from("brain")].into_iter().chain(args)); |
|
| 69 | 70 | let mut state = ReviewStateLoad::Existing; |
|
| 70 | 71 | let mut command = None; |
|
| 71 | 72 | ||
| 72 | 73 | while let Some(arg) = parser.next()? { |
|
| 73 | - | if let Long("all") = arg |
|
| 74 | - | && let Some(Command::Status { scope }) = &mut command |
|
| 75 | - | { |
|
| 76 | - | *scope = StatusScope::All; |
|
| 77 | - | continue; |
|
| 78 | - | } |
|
| 79 | 74 | if command.is_some() { |
|
| 80 | 75 | return Err(Error::Usage(format!( |
|
| 81 | 76 | "unexpected argument: {arg:?}\n{USAGE}" |
|
| 82 | 77 | ))); |
|
| 83 | 78 | } |
|
| 84 | 79 | match arg { |
|
| 85 | 80 | Long("reset") => state = ReviewStateLoad::Fresh, |
|
| 86 | 81 | Long("rebase") => state = ReviewStateLoad::Rebase, |
|
| 87 | 82 | Value(command_name) if command_name == "status" => { |
|
| 88 | - | command = Some(Command::Status { |
|
| 89 | - | scope: StatusScope::Reviews, |
|
| 90 | - | }); |
|
| 83 | + | command = match parser.next()? { |
|
| 84 | + | Some(Long("all")) => Some(Command::Status { |
|
| 85 | + | scope: StatusScope::All, |
|
| 86 | + | }), |
|
| 87 | + | Some(Value(base)) => { |
|
| 88 | + | let base = parse_revision("base", base)?; |
|
| 89 | + | let target = parse_revision("target", parser.value()?)?; |
|
| 90 | + | Some(Command::RangeStatus { base, target }) |
|
| 91 | + | } |
|
| 92 | + | Some(_) => return Err(Error::Usage(USAGE.to_owned())), |
|
| 93 | + | None => Some(Command::Status { |
|
| 94 | + | scope: StatusScope::Reviews, |
|
| 95 | + | }), |
|
| 96 | + | }; |
|
| 91 | 97 | } |
|
| 92 | 98 | Value(command_name) if command_name == "diff" => { |
|
| 93 | 99 | let first = parse_revision("target", parser.value()?)?; |
|
| 94 | 100 | let (base, target) = match parser.next()? { |
|
| 95 | 101 | Some(Value(target)) => { |
| 128 | 134 | /// Show review status for the selected branch scope. |
|
| 129 | 135 | Status { |
|
| 130 | 136 | /// Branch scope included in status output. |
|
| 131 | 137 | scope: StatusScope, |
|
| 132 | 138 | }, |
|
| 139 | + | /// Print the commit history status for `base..target`. |
|
| 140 | + | RangeStatus { |
|
| 141 | + | /// Exclusive lower bound of the reviewed range. |
|
| 142 | + | base: Rev, |
|
| 143 | + | /// Inclusive upper bound of the reviewed range. |
|
| 144 | + | target: Rev, |
|
| 145 | + | }, |
|
| 133 | 146 | /// Review the commits in `base..target`. |
|
| 134 | 147 | Diff { |
|
| 135 | 148 | /// Exclusive lower bound of the reviewed range, when supplied. |
|
| 136 | 149 | base: Option<Rev>, |
|
| 137 | 150 | /// Inclusive upper bound of the reviewed range. |
| 217 | 230 | /// Parses arguments and dispatches the requested command. |
|
| 218 | 231 | fn run(args: Vec<OsString>) -> Result<(), Error> { |
|
| 219 | 232 | let args = Args::parse(args)?; |
|
| 220 | 233 | match args.command { |
|
| 221 | 234 | Command::Status { scope } => run_status(scope), |
|
| 235 | + | Command::RangeStatus { base, target } => run_range_status(base, target), |
|
| 222 | 236 | Command::Diff { base, target } => run_diff(args.state, base, target), |
|
| 223 | 237 | } |
|
| 224 | 238 | } |
|
| 225 | 239 | ||
| 226 | 240 | /// Loads the review range and runs the interactive diff session. |
| 302 | 316 | ||
| 303 | 317 | result?; |
|
| 304 | 318 | cursor.map_err(Error::Io) |
|
| 305 | 319 | } |
|
| 306 | 320 | ||
| 321 | + | /// Prints the history view for the commits in `base..target`. |
|
| 322 | + | fn run_range_status(base: Rev, target: Rev) -> Result<(), Error> { |
|
| 323 | + | let cwd = env::current_dir()?; |
|
| 324 | + | let backend = <dyn backend::Backend>::from_path(&cwd, base, target)?; |
|
| 325 | + | let brain = backend.brain()?; |
|
| 326 | + | let commits = backend.commits()?; |
|
| 327 | + | if commits.is_empty() { |
|
| 328 | + | return Err(Error::Empty); |
|
| 329 | + | } |
|
| 330 | + | let width = status_width(); |
|
| 331 | + | let table = |
|
| 332 | + | session::history_output_table(commits, &*backend, &brain, DiffPalette::default(), width)?; |
|
| 333 | + | let mut stdout = io::stdout().lock(); |
|
| 334 | + | for line in table.lines(Constraint::tight(width)) { |
|
| 335 | + | writeln!(stdout, "{}", line.styled())?; |
|
| 336 | + | } |
|
| 337 | + | Ok(()) |
|
| 338 | + | } |
|
| 339 | + | ||
| 307 | 340 | /// Prints the stored review status table. |
|
| 308 | 341 | fn run_status(scope: StatusScope) -> Result<(), Error> { |
|
| 309 | 342 | let cwd = env::current_dir()?; |
|
| 310 | 343 | let mut statuses = backend::statuses(&cwd)?; |
|
| 311 | 344 | if scope == StatusScope::Reviews { |
| 530 | 563 | }, |
|
| 531 | 564 | } |
|
| 532 | 565 | ); |
|
| 533 | 566 | } |
|
| 534 | 567 | ||
| 568 | + | #[test] |
|
| 569 | + | fn parses_range_status_command() { |
|
| 570 | + | let parsed = Args::parse(args(&["status", "main", "HEAD"])).unwrap(); |
|
| 571 | + | ||
| 572 | + | assert_eq!( |
|
| 573 | + | parsed.command, |
|
| 574 | + | Command::RangeStatus { |
|
| 575 | + | base: Rev::try_from("main").unwrap(), |
|
| 576 | + | target: Rev::try_from("HEAD").unwrap(), |
|
| 577 | + | } |
|
| 578 | + | ); |
|
| 579 | + | } |
|
| 580 | + | ||
| 535 | 581 | #[test] |
|
| 536 | 582 | fn parses_reset_flag_before_diff_command() { |
|
| 537 | 583 | let parsed = Args::parse(args(&["--reset", "diff", "main", "HEAD"])).unwrap(); |
|
| 538 | 584 | ||
| 539 | 585 | assert_eq!(parsed.state, ReviewStateLoad::Fresh); |
crates/brain/src/session.rs
+103 -79
| 127 | 127 | rx: events_rx, |
|
| 128 | 128 | } = events; |
|
| 129 | 129 | ||
| 130 | 130 | let range = backend.range_label(); |
|
| 131 | 131 | let (mut slots, history_status) = if commits.len() > 1 { |
|
| 132 | - | let mut reviewed = Vec::with_capacity(commits.len()); |
|
| 133 | - | for commit in commits { |
|
| 134 | - | let document = backend.diff_document(commit.id)?.split_hunks(); |
|
| 135 | - | let identities = brain.hunk_identities(&document, commit.id)?; |
|
| 136 | - | reviewed.push((commit, document, identities)); |
|
| 137 | - | } |
|
| 138 | - | if reviewed.is_empty() { |
|
| 139 | - | return Err(Error::Empty); |
|
| 140 | - | } |
|
| 141 | - | let status_inputs = reviewed |
|
| 142 | - | .iter() |
|
| 143 | - | .map(|(commit, document, identities)| (commit.id, document, identities)) |
|
| 144 | - | .collect::<Vec<_>>(); |
|
| 145 | - | let mut history = HistoryStatus::for_commits(&brain, &status_inputs); |
|
| 146 | - | let slots = reviewed |
|
| 147 | - | .into_iter() |
|
| 148 | - | .map(|(commit, document, identities)| { |
|
| 149 | - | let status = history.commits.entry(commit.id).or_default(); |
|
| 150 | - | let progress = status.progress; |
|
| 151 | - | let state = status.state; |
|
| 152 | - | let remaining_stats = status.stats; |
|
| 153 | - | CommitSlot { |
|
| 154 | - | commit, |
|
| 155 | - | progress, |
|
| 156 | - | state, |
|
| 157 | - | remaining_stats, |
|
| 158 | - | document: Some(document), |
|
| 159 | - | identities: Some(identities), |
|
| 160 | - | loaded: None, |
|
| 161 | - | } |
|
| 162 | - | }) |
|
| 163 | - | .collect::<Vec<_>>(); |
|
| 164 | - | (slots, Some(history)) |
|
| 132 | + | let (slots, status) = reviewed_slots(commits, &*backend, &brain)?; |
|
| 133 | + | (slots, Some(status)) |
|
| 165 | 134 | } else { |
|
| 166 | 135 | let slots = commits |
|
| 167 | 136 | .into_iter() |
|
| 168 | 137 | .map(|commit| CommitSlot { |
|
| 169 | 138 | remaining_stats: commit.stats, |
| 236 | 205 | } |
|
| 237 | 206 | } |
|
| 238 | 207 | Ok(()) |
|
| 239 | 208 | } |
|
| 240 | 209 | ||
| 210 | + | /// Returns the interactive history table for regular terminal output. |
|
| 211 | + | pub(crate) fn history_output_table( |
|
| 212 | + | commits: Vec<Commit>, |
|
| 213 | + | backend: &dyn Backend, |
|
| 214 | + | brain: &Brain, |
|
| 215 | + | palette: rustik_diff::DiffPalette, |
|
| 216 | + | width: usize, |
|
| 217 | + | ) -> Result<Table<8, Line>, Error> { |
|
| 218 | + | let (mut slots, _) = reviewed_slots(commits, backend, brain)?; |
|
| 219 | + | slots.reverse(); |
|
| 220 | + | Ok(history_table(&slots, palette, width)) |
|
| 221 | + | } |
|
| 222 | + | ||
| 223 | + | /// Builds reviewed commit slots and their aggregate history status. |
|
| 224 | + | fn reviewed_slots( |
|
| 225 | + | commits: Vec<Commit>, |
|
| 226 | + | backend: &dyn Backend, |
|
| 227 | + | brain: &Brain, |
|
| 228 | + | ) -> Result<(Vec<CommitSlot>, HistoryStatus), Error> { |
|
| 229 | + | let mut reviewed = Vec::with_capacity(commits.len()); |
|
| 230 | + | for commit in commits { |
|
| 231 | + | let document = backend.diff_document(commit.id)?.split_hunks(); |
|
| 232 | + | let identities = brain.hunk_identities(&document, commit.id)?; |
|
| 233 | + | reviewed.push((commit, document, identities)); |
|
| 234 | + | } |
|
| 235 | + | if reviewed.is_empty() { |
|
| 236 | + | return Err(Error::Empty); |
|
| 237 | + | } |
|
| 238 | + | let status_inputs = reviewed |
|
| 239 | + | .iter() |
|
| 240 | + | .map(|(commit, document, identities)| (commit.id, document, identities)) |
|
| 241 | + | .collect::<Vec<_>>(); |
|
| 242 | + | let mut history = HistoryStatus::for_commits(brain, &status_inputs); |
|
| 243 | + | let slots = reviewed |
|
| 244 | + | .into_iter() |
|
| 245 | + | .map(|(commit, document, identities)| { |
|
| 246 | + | let status = history.commits.entry(commit.id).or_default(); |
|
| 247 | + | CommitSlot { |
|
| 248 | + | progress: status.progress, |
|
| 249 | + | state: status.state, |
|
| 250 | + | remaining_stats: status.stats, |
|
| 251 | + | commit, |
|
| 252 | + | document: Some(document), |
|
| 253 | + | identities: Some(identities), |
|
| 254 | + | loaded: None, |
|
| 255 | + | } |
|
| 256 | + | }) |
|
| 257 | + | .collect(); |
|
| 258 | + | Ok((slots, history)) |
|
| 259 | + | } |
|
| 260 | + | ||
| 261 | + | /// Builds the commit table shared by interactive and regular history output. |
|
| 262 | + | fn history_table( |
|
| 263 | + | commits: &[CommitSlot], |
|
| 264 | + | palette: rustik_diff::DiffPalette, |
|
| 265 | + | width: usize, |
|
| 266 | + | ) -> Table<8, Line> { |
|
| 267 | + | let now = current_timestamp(); |
|
| 268 | + | let last = commits.len() - 1; |
|
| 269 | + | let largest_change_count = commits |
|
| 270 | + | .iter() |
|
| 271 | + | .map(|slot| { |
|
| 272 | + | let stats = slot.remaining_stats(); |
|
| 273 | + | stats.insertions + stats.deletions |
|
| 274 | + | }) |
|
| 275 | + | .max() |
|
| 276 | + | .unwrap_or(0); |
|
| 277 | + | let diffstat_width = (width / HISTORY_DIFFSTAT_WIDTH_DIVISOR) |
|
| 278 | + | .clamp(HISTORY_DIFFSTAT_MIN_WIDTH, HISTORY_DIFFSTAT_MAX_WIDTH); |
|
| 279 | + | let mut table = Table::<8, Line>::new(TableOptions { |
|
| 280 | + | spacing: 2, |
|
| 281 | + | ..TableOptions::default() |
|
| 282 | + | }) |
|
| 283 | + | .with_layout([ |
|
| 284 | + | Column::auto(), |
|
| 285 | + | Column::auto(), |
|
| 286 | + | Column::flex(1), |
|
| 287 | + | Column::collapse(2), |
|
| 288 | + | Column::collapse(3), |
|
| 289 | + | Column::collapse(1), |
|
| 290 | + | Column::auto(), |
|
| 291 | + | Column::collapse(4), |
|
| 292 | + | ]); |
|
| 293 | + | for (index, slot) in commits.iter().enumerate() { |
|
| 294 | + | let graph = match (index, last) { |
|
| 295 | + | (0, 0) => "──◉︎", |
|
| 296 | + | (0, _) => "╭─◉︎", |
|
| 297 | + | (index, last) if index == last => "╰─◉︎", |
|
| 298 | + | _ => "├─◉︎", |
|
| 299 | + | }; |
|
| 300 | + | let [graph, id, summary] = commit_row(slot, graph); |
|
| 301 | + | let [author, age, branches, progress, diffstat] = |
|
| 302 | + | commit_row_suffix(slot, palette, now, largest_change_count, diffstat_width); |
|
| 303 | + | table.push([ |
|
| 304 | + | graph, id, summary, author, age, branches, progress, diffstat, |
|
| 305 | + | ]); |
|
| 306 | + | } |
|
| 307 | + | table |
|
| 308 | + | } |
|
| 309 | + | ||
| 241 | 310 | /// Commit-local hunk location used by acceptance and undo state. |
|
| 242 | 311 | type HunkLocation = (usize, rustik_diff::HunkId); |
|
| 243 | 312 | ||
| 244 | 313 | /// Viewer state around one optimistic acceptance. |
|
| 245 | 314 | struct NavigationRollback { |
| 1402 | 1471 | frame.render_widget(&mut chrome, chrome_area); |
|
| 1403 | 1472 | })?; |
|
| 1404 | 1473 | } |
|
| 1405 | 1474 | Mode::Commits => { |
|
| 1406 | 1475 | let palette = self.options.diff_palette; |
|
| 1407 | - | let now = current_timestamp(); |
|
| 1408 | - | let last = self.commits.len() - 1; |
|
| 1409 | - | let largest_change_count = self |
|
| 1410 | - | .commits |
|
| 1411 | - | .iter() |
|
| 1412 | - | .map(|slot| { |
|
| 1413 | - | let stats = slot.remaining_stats(); |
|
| 1414 | - | stats.insertions + stats.deletions |
|
| 1415 | - | }) |
|
| 1416 | - | .max() |
|
| 1417 | - | .unwrap_or(0); |
|
| 1418 | 1476 | self.terminal.draw(|frame| { |
|
| 1419 | 1477 | let (body, chrome_area) = Chrome::areas(frame.area); |
|
| 1420 | 1478 | let cols = ScrollView::body_cols(body); |
|
| 1421 | - | let diffstat_width = (cols / HISTORY_DIFFSTAT_WIDTH_DIVISOR) |
|
| 1422 | - | .clamp(HISTORY_DIFFSTAT_MIN_WIDTH, HISTORY_DIFFSTAT_MAX_WIDTH); |
|
| 1423 | - | let mut table = Table::<8, Line>::new(TableOptions { |
|
| 1424 | - | spacing: 2, |
|
| 1425 | - | ..TableOptions::default() |
|
| 1426 | - | }) |
|
| 1427 | - | .with_layout([ |
|
| 1428 | - | Column::auto(), |
|
| 1429 | - | Column::auto(), |
|
| 1430 | - | Column::flex(1), |
|
| 1431 | - | Column::collapse(2), |
|
| 1432 | - | Column::collapse(3), |
|
| 1433 | - | Column::collapse(1), |
|
| 1434 | - | Column::auto(), |
|
| 1435 | - | Column::collapse(4), |
|
| 1436 | - | ]); |
|
| 1437 | - | for (index, slot) in self.commits.iter().enumerate() { |
|
| 1438 | - | let graph = match (index, last) { |
|
| 1439 | - | (0, 0) => "──◉︎", |
|
| 1440 | - | (0, _) => "╭─◉︎", |
|
| 1441 | - | (index, last) if index == last => "╰─◉︎", |
|
| 1442 | - | _ => "├─◉︎", |
|
| 1443 | - | }; |
|
| 1444 | - | let [graph, id, summary] = commit_row(slot, graph); |
|
| 1445 | - | let [author, age, branches, progress, diffstat] = commit_row_suffix( |
|
| 1446 | - | slot, |
|
| 1447 | - | palette, |
|
| 1448 | - | now, |
|
| 1449 | - | largest_change_count, |
|
| 1450 | - | diffstat_width, |
|
| 1451 | - | ); |
|
| 1452 | - | table.push([ |
|
| 1453 | - | graph, id, summary, author, age, branches, progress, diffstat, |
|
| 1454 | - | ]); |
|
| 1455 | - | } |
|
| 1479 | + | let table = history_table(&self.commits, palette, cols); |
|
| 1456 | 1480 | let rows = table.lines(Constraint::tight(cols)); |
|
| 1457 | 1481 | let selected = Style::default().bg(SELECTED_COMMIT_BG); |
|
| 1458 | 1482 | let list = List::new(rows) |
|
| 1459 | 1483 | .selected_styles(selected, selected) |
|
| 1460 | 1484 | .scrollbar(DiffViewer::scrollbar_style()); |
a.rs
+0 -0