brain: Comment cards in the hunk view
7d735d9e6871c1e77cc5ec28ffb9248b3cedf879
Render stored comments as bordered cards with author and relative timestamp, and edit drafts inside a matching card. Scroll the isolated hunk while browsing or editing, restart the highlighter per view, and mark accepted hunks with the diff palette's positive color. Assisted-by: Claude:fable-5
1 parent
94cd6ba5
crates/brain/src/comments.rs
+8 -0
| 23 | 23 | pub struct Comment { |
|
| 24 | 24 | /// Stable local comment id. |
|
| 25 | 25 | pub id: CommentId, |
|
| 26 | 26 | /// Author shown for this comment. |
|
| 27 | 27 | pub author: String, |
|
| 28 | + | /// Unix timestamp when the comment was authored. |
|
| 29 | + | pub timestamp: u64, |
|
| 28 | 30 | /// Comment body entered by the reviewer. |
|
| 29 | 31 | pub body: String, |
|
| 30 | 32 | } |
|
| 31 | 33 | ||
| 32 | 34 | /// Stable local identifier for one review comment. |
| 162 | 164 | rows.push('\n'); |
|
| 163 | 165 | } |
|
| 164 | 166 | rows |
|
| 165 | 167 | } |
|
| 166 | 168 | ||
| 169 | + | /// Returns the author name used for newly authored comments. |
|
| 170 | + | pub fn author(&self) -> &str { |
|
| 171 | + | &self.author |
|
| 172 | + | } |
|
| 173 | + | ||
| 167 | 174 | /// Returns comments attached to `target`'s patch id, in storage order. |
|
| 168 | 175 | fn matching(&self, target: &CommentTarget) -> Vec<Comment> { |
|
| 169 | 176 | self.records |
|
| 170 | 177 | .iter() |
|
| 171 | 178 | .filter(|record| record.patch_id == target.patch_id) |
| 229 | 236 | impl From<&CommentRecord> for Comment { |
|
| 230 | 237 | fn from(record: &CommentRecord) -> Self { |
|
| 231 | 238 | Self { |
|
| 232 | 239 | id: record.id.clone(), |
|
| 233 | 240 | author: record.author.clone(), |
|
| 241 | + | timestamp: record.timestamp, |
|
| 234 | 242 | body: record.body.clone(), |
|
| 235 | 243 | } |
|
| 236 | 244 | } |
|
| 237 | 245 | } |
|
| 238 | 246 |
crates/brain/src/decorate.rs
+29 -15
| 7 | 7 | //! it. Accepted hunks are dimmed and marked with a check mark; commented |
|
| 8 | 8 | //! hunks show a comment marker and count. |
|
| 9 | 9 | ||
| 10 | 10 | use std::collections::{BTreeMap, BTreeSet}; |
|
| 11 | 11 | ||
| 12 | - | use rustik_diff::{DiffDecorator, FileDecoration, FileId, HunkDecoration, HunkId}; |
|
| 12 | + | use rustik_diff::{DiffDecorator, DiffPalette, FileDecoration, FileId, HunkDecoration, HunkId}; |
|
| 13 | 13 | use rustik_tui as tui; |
|
| 14 | - | use tui::{Color, Label, Line, Paint, Style}; |
|
| 14 | + | use tui::{Label, Line, Paint, Style, colors}; |
|
| 15 | 15 | ||
| 16 | 16 | use crate::comments::Comments; |
|
| 17 | 17 | use crate::review::Review; |
|
| 18 | 18 | ||
| 19 | 19 | /// Check mark drawn beside accepted hunks and fully accepted files. |
|
| 20 | - | const ACCEPTED_MARK: char = '\u{2713}'; |
|
| 20 | + | const ACCEPTED_MARK: char = '\u{2714}'; |
|
| 21 | 21 | /// Marker drawn in the corner of a hunk that has comments. |
|
| 22 | - | const COMMENT_MARK: char = '\u{1F5E8}'; |
|
| 22 | + | const COMMENT_MARK: char = '\u{1F5E9}'; |
|
| 23 | 23 | ||
| 24 | 24 | /// Immutable decoration snapshot of one commit's review state. |
|
| 25 | 25 | #[derive(Clone, Debug)] |
|
| 26 | 26 | pub struct Decorations { |
|
| 27 | 27 | /// Hunks accepted at snapshot time. |
|
| 28 | 28 | accepted: BTreeSet<HunkId>, |
|
| 29 | 29 | /// Per-hunk comment counts at snapshot time. |
|
| 30 | 30 | comments: BTreeMap<HunkId, usize>, |
|
| 31 | 31 | /// Original hunk ids for each file in the commit. |
|
| 32 | 32 | file_hunks: BTreeMap<FileId, Vec<HunkId>>, |
|
| 33 | + | /// Diff palette the markers are drawn with. |
|
| 34 | + | palette: DiffPalette, |
|
| 33 | 35 | } |
|
| 34 | 36 | ||
| 35 | 37 | impl Decorations { |
|
| 36 | 38 | /// Captures the current review and comment state for `file_hunks`. |
|
| 37 | 39 | pub fn snapshot( |
|
| 38 | 40 | review: &Review, |
|
| 39 | 41 | comments: &Comments, |
|
| 40 | 42 | file_hunks: &BTreeMap<FileId, Vec<HunkId>>, |
|
| 43 | + | palette: DiffPalette, |
|
| 41 | 44 | ) -> Self { |
|
| 42 | 45 | Self { |
|
| 43 | 46 | accepted: review.accepted().clone(), |
|
| 44 | 47 | comments: comments.counts(), |
|
| 45 | 48 | file_hunks: file_hunks.clone(), |
|
| 49 | + | palette, |
|
| 46 | 50 | } |
|
| 47 | 51 | } |
|
| 48 | 52 | } |
|
| 49 | 53 | ||
| 50 | 54 | impl DiffDecorator for Decorations { |
|
| 51 | 55 | fn file(&self, file: FileId) -> FileDecoration { |
|
| 52 | 56 | let accepted = self.file_hunks.get(&file).is_some_and(|hunks| { |
|
| 53 | 57 | !hunks.is_empty() && hunks.iter().all(|hunk| self.accepted.contains(hunk)) |
|
| 54 | 58 | }); |
|
| 59 | + | let comment_count = self |
|
| 60 | + | .file_hunks |
|
| 61 | + | .get(&file) |
|
| 62 | + | .into_iter() |
|
| 63 | + | .flatten() |
|
| 64 | + | .filter_map(|hunk| self.comments.get(hunk)) |
|
| 65 | + | .sum::<usize>(); |
|
| 55 | 66 | if accepted { |
|
| 56 | 67 | FileDecoration { |
|
| 57 | - | status: Some(Paint::new(ACCEPTED_MARK).fg(Color::Green)), |
|
| 68 | + | status: Some(Paint::new(ACCEPTED_MARK).fg(self.palette.positive_fg)), |
|
| 69 | + | ..FileDecoration::default() |
|
| 70 | + | } |
|
| 71 | + | } else if comment_count > 0 { |
|
| 72 | + | FileDecoration { |
|
| 73 | + | annotations: vec![Line::new( |
|
| 74 | + | Label::new(&format!("{comment_count} {COMMENT_MARK}")).fg(colors::rgb::DIM), |
|
| 75 | + | )], |
|
| 58 | 76 | ..FileDecoration::default() |
|
| 59 | 77 | } |
|
| 60 | 78 | } else { |
|
| 61 | 79 | FileDecoration::default() |
|
| 62 | 80 | } |
| 68 | 86 | if !accepted && comments == 0 { |
|
| 69 | 87 | return HunkDecoration::default(); |
|
| 70 | 88 | } |
|
| 71 | 89 | ||
| 72 | 90 | let mut corner = Line::blank(); |
|
| 73 | - | if comments > 0 { |
|
| 74 | - | let marker = if comments > 1 { |
|
| 75 | - | format!("{COMMENT_MARK} {comments}") |
|
| 76 | - | } else { |
|
| 77 | - | COMMENT_MARK.to_string() |
|
| 78 | - | }; |
|
| 79 | - | corner = corner.item(Label::new(&marker).fg(Color::Yellow)); |
|
| 80 | - | } |
|
| 81 | 91 | if accepted { |
|
| 82 | - | if comments > 0 { |
|
| 92 | + | corner = corner.item(Label::from_char(ACCEPTED_MARK).fg(self.palette.positive_fg)); |
|
| 93 | + | } |
|
| 94 | + | if comments > 0 { |
|
| 95 | + | if accepted { |
|
| 83 | 96 | corner = corner.item(Label::space()); |
|
| 84 | 97 | } |
|
| 85 | - | corner = corner.item(Label::from_char(ACCEPTED_MARK).fg(Color::Green)); |
|
| 98 | + | corner = |
|
| 99 | + | corner.item(Label::new(&format!("{comments} {COMMENT_MARK}")).fg(colors::rgb::DIM)); |
|
| 86 | 100 | } |
|
| 87 | 101 | ||
| 88 | 102 | HunkDecoration { |
|
| 89 | 103 | corner, |
|
| 90 | 104 | style: accepted.then(|| Style::default().dim()), |
crates/brain/src/main.rs
+2 -1
| 18 | 18 | use std::env; |
|
| 19 | 19 | use std::ffi::{OsStr, OsString}; |
|
| 20 | 20 | use std::io::{self, Write as _}; |
|
| 21 | 21 | use std::process::ExitCode; |
|
| 22 | 22 | ||
| 23 | - | use rustik_diff::{Builtins, DiffOptions, FileTreeMode}; |
|
| 23 | + | use rustik_diff::{Builtins, DiffOptions, FileTreeMode, HunkFocus}; |
|
| 24 | 24 | use rustik_repo::Rev; |
|
| 25 | 25 | use rustik_tui::Terminal; |
|
| 26 | 26 | use rustik_tui_termion as tui_termion; |
|
| 27 | 27 | ||
| 28 | 28 | use crate::comments::ReviewComments; |
| 71 | 71 | .unwrap_or_else(|| cwd.display().to_string()); |
|
| 72 | 72 | let options = DiffOptions { |
|
| 73 | 73 | diff_palette: assets.palette, |
|
| 74 | 74 | file_tree: FileTreeMode::Auto, |
|
| 75 | 75 | file_tree_root: root, |
|
| 76 | + | hunk_focus: HunkFocus::DimUnfocused, |
|
| 76 | 77 | ..DiffOptions::default() |
|
| 77 | 78 | }; |
|
| 78 | 79 | ||
| 79 | 80 | // Commit diffs are decoded lazily when first displayed; only the commit |
|
| 80 | 81 | // list and its stats are fetched up front. Commits that introduce no |
crates/brain/src/session.rs
+158 -60
| 22 | 22 | mod status; |
|
| 23 | 23 | ||
| 24 | 24 | use commit::{CommitReview, CommitSlot}; |
|
| 25 | 25 | use event::{ContextFetch, Resize, TerminalEvent, Update, context_change}; |
|
| 26 | 26 | use hunk::{CommentEdit, HunkView}; |
|
| 27 | - | use status::{Status, comment_block_lines, commit_row, viewer_area}; |
|
| 27 | + | use status::{Status, commit_row, viewer_area}; |
|
| 28 | 28 | ||
| 29 | 29 | /// Terminal backend used by the interactive viewer. |
|
| 30 | 30 | type BrainTerminal = Terminal<tui_termion::TermionBackend<tui_termion::term::TerminalScreen>>; |
|
| 31 | 31 | ||
| 32 | 32 | /// Active view in the session. |
| 176 | 176 | Resize::Changed |
|
| 177 | 177 | } else { |
|
| 178 | 178 | Resize::Stable |
|
| 179 | 179 | }; |
|
| 180 | 180 | let control = match self.mode { |
|
| 181 | - | Mode::Hunk(_) => self.handle_hunk_key(event)?, |
|
| 181 | + | Mode::Hunk(_) => { |
|
| 182 | + | let area = viewer_area(self.terminal.area()); |
|
| 183 | + | self.handle_hunk_key(event, area)? |
|
| 184 | + | } |
|
| 182 | 185 | Mode::Commits => self.handle_commits_key(event)?, |
|
| 183 | 186 | Mode::Diff => { |
|
| 184 | 187 | if let Some(change) = context_change(&event) { |
|
| 185 | 188 | // Context expansion is asynchronous and may not |
|
| 186 | 189 | // redraw immediately, so it bypasses the ordinary |
| 196 | 199 | } |
|
| 197 | 200 | }; |
|
| 198 | 201 | Ok(Update::from_control(control, resize)) |
|
| 199 | 202 | } |
|
| 200 | 203 | TerminalEvent::Highlight(generation, HighlightOutput::Highlights(results)) => { |
|
| 201 | - | if generation == self.generation { |
|
| 202 | - | // Always populate the cache so returning to the diff is |
|
| 203 | - | // instant, but only repaint when the diff is on screen. |
|
| 204 | - | let changed = self.commits[self.current] |
|
| 205 | - | .loaded |
|
| 206 | - | .as_mut() |
|
| 207 | - | .is_some_and(|loaded| loaded.viewer.apply_highlights(results)); |
|
| 208 | - | if changed && matches!(self.mode, Mode::Diff) { |
|
| 209 | - | self.draw_frame()?; |
|
| 210 | - | } |
|
| 204 | + | if generation == self.generation |
|
| 205 | + | && self |
|
| 206 | + | .active_viewer() |
|
| 207 | + | .is_some_and(|viewer| viewer.apply_highlights(results)) |
|
| 208 | + | { |
|
| 209 | + | self.draw_frame()?; |
|
| 211 | 210 | } |
|
| 212 | 211 | Ok(Update::Continue) |
|
| 213 | 212 | } |
|
| 214 | 213 | TerminalEvent::Highlight(generation, HighlightOutput::Context(update)) => { |
|
| 215 | 214 | if generation != self.generation { |
| 249 | 248 | Event::Key(Key::Char('p')) => self.apply(DiffAction::PreviousHunk, area), |
|
| 250 | 249 | Event::Key(Key::Char('c')) => { |
|
| 251 | 250 | self.enter_hunk_view(area)?; |
|
| 252 | 251 | Ok(Control::Redraw) |
|
| 253 | 252 | } |
|
| 253 | + | Event::Key(Key::Enter) => { |
|
| 254 | + | self.enter_hunk_view(area)?; |
|
| 255 | + | Ok(Control::Redraw) |
|
| 256 | + | } |
|
| 254 | 257 | Event::Key(Key::Char(']')) => self.switch(1), |
|
| 255 | 258 | Event::Key(Key::Char('[')) => self.switch(-1), |
|
| 256 | 259 | Event::Key(Key::Tab) => { |
|
| 257 | - | self.next_tab(); |
|
| 260 | + | self.next_tab()?; |
|
| 258 | 261 | Ok(Control::Redraw) |
|
| 259 | 262 | } |
|
| 260 | 263 | _ => Ok(self.current()?.viewer.handle_input(event, area)), |
|
| 261 | 264 | } |
|
| 262 | 265 | } |
| 320 | 323 | } |
|
| 321 | 324 | self.mode = Mode::Diff; |
|
| 322 | 325 | return Ok(Control::Redraw); |
|
| 323 | 326 | } |
|
| 324 | 327 | Event::Key(Key::Tab) => { |
|
| 325 | - | self.next_tab(); |
|
| 328 | + | self.next_tab()?; |
|
| 326 | 329 | return Ok(Control::Redraw); |
|
| 327 | 330 | } |
|
| 328 | 331 | Event::Resize(_) => return Ok(Control::Redraw), |
|
| 329 | 332 | _ => return Ok(Control::Continue), |
|
| 330 | 333 | }; |
| 349 | 352 | return Ok(()); |
|
| 350 | 353 | }; |
|
| 351 | 354 | let Some(patch) = loaded.hunk_patches.get(&hunk).cloned() else { |
|
| 352 | 355 | return Ok(()); |
|
| 353 | 356 | }; |
|
| 354 | - | if loaded.review.is_accepted(hunk) { |
|
| 357 | + | let accepted = loaded.review.is_accepted(hunk); |
|
| 358 | + | if accepted { |
|
| 355 | 359 | self.brain.reverse_patch(&patch)?; |
|
| 356 | 360 | } else { |
|
| 357 | 361 | self.brain.apply_patch(&patch)?; |
|
| 358 | 362 | } |
|
| 359 | 363 | self.reconcile_reviews()?; |
|
| 364 | + | if !accepted { |
|
| 365 | + | self.current()?.viewer.apply(DiffAction::NextHunk, area); |
|
| 366 | + | } |
|
| 360 | 367 | ||
| 361 | 368 | Ok(()) |
|
| 362 | 369 | } |
|
| 363 | 370 | ||
| 364 | 371 | /// Persists acceptance or rejection of every hunk in the focused file. |
| 392 | 399 | Ok(()) |
|
| 393 | 400 | } |
|
| 394 | 401 | ||
| 395 | 402 | /// Opens the hunk view for the focused hunk to author a comment. |
|
| 396 | 403 | fn enter_hunk_view(&mut self, area: Rect) -> Result<(), Error> { |
|
| 397 | - | let focus = self.current()?.viewer.state().focus; |
|
| 398 | - | let Some(DiffFocus::Hunk(hunk)) = focus else { |
|
| 399 | - | // Focus the first hunk so a later press has a target to comment on. |
|
| 404 | + | if !matches!( |
|
| 405 | + | self.current()?.viewer.state().focus, |
|
| 406 | + | Some(DiffFocus::Hunk(_)) |
|
| 407 | + | ) { |
|
| 400 | 408 | self.current()?.viewer.apply(DiffAction::NextHunk, area); |
|
| 409 | + | } |
|
| 410 | + | let Some(DiffFocus::Hunk(hunk)) = self.current()?.viewer.state().focus else { |
|
| 401 | 411 | return Ok(()); |
|
| 402 | 412 | }; |
|
| 403 | 413 | // Resume a parked draft on the same hunk instead of discarding it. |
|
| 404 | - | if let Some(parked) = self.parked.take_if(|parked| parked.hunk == hunk) { |
|
| 414 | + | if let Some(mut parked) = self.parked.take_if(|parked| parked.hunk == hunk) { |
|
| 415 | + | if !parked.is_editing() { |
|
| 416 | + | parked.start_new(); |
|
| 417 | + | } |
|
| 405 | 418 | self.mode = Mode::Hunk(parked); |
|
| 419 | + | self.restart_highlighter()?; |
|
| 406 | 420 | return Ok(()); |
|
| 407 | 421 | } |
|
| 408 | 422 | self.parked = None; |
|
| 409 | 423 | let mut options = self.options.clone(); |
|
| 410 | 424 | options.file_tree = FileTreeMode::Hidden; |
| 417 | 431 | return Ok(()); |
|
| 418 | 432 | }; |
|
| 419 | 433 | let document = DiffDocument::for_hunks(file, [focused.clone()]); |
|
| 420 | 434 | let viewer = DiffViewer::new(document, options); |
|
| 421 | 435 | ||
| 422 | - | self.mode = Mode::Hunk(Box::new(HunkView::new(hunk, viewer))); |
|
| 436 | + | let mut view = HunkView::new(hunk, viewer, self.comments.author().to_owned()); |
|
| 437 | + | view.start_new(); |
|
| 438 | + | self.mode = Mode::Hunk(Box::new(view)); |
|
| 439 | + | self.restart_highlighter()?; |
|
| 423 | 440 | ||
| 424 | 441 | Ok(()) |
|
| 425 | 442 | } |
|
| 426 | 443 | ||
| 427 | 444 | /// Switches to the next review tab, parking any open hunk view. |
|
| 428 | - | fn next_tab(&mut self) { |
|
| 445 | + | fn next_tab(&mut self) -> Result<(), Error> { |
|
| 429 | 446 | self.mode = match mem::replace(&mut self.mode, Mode::Diff) { |
|
| 430 | 447 | Mode::Commits => Mode::Diff, |
|
| 431 | 448 | Mode::Diff => { |
|
| 432 | 449 | if let Some(parked) = self.parked.take() { |
|
| 433 | 450 | Mode::Hunk(parked) |
| 440 | 457 | self.parked = Some(view); |
|
| 441 | 458 | self.list.select(self.current, self.commits.len()); |
|
| 442 | 459 | Mode::Commits |
|
| 443 | 460 | } |
|
| 444 | 461 | }; |
|
| 462 | + | self.restart_highlighter()?; |
|
| 463 | + | Ok(()) |
|
| 445 | 464 | } |
|
| 446 | 465 | ||
| 447 | 466 | /// Routes one key to the comment editor while the hunk view is open. |
|
| 448 | - | fn handle_hunk_key(&mut self, event: Event<()>) -> Result<Control, Error> { |
|
| 467 | + | fn handle_hunk_key(&mut self, event: Event<()>, area: Rect) -> Result<Control, Error> { |
|
| 449 | 468 | let comment_count = self.hunk_comment_count(); |
|
| 469 | + | let hunk_area = self.active_viewer_area(area); |
|
| 450 | 470 | if let Mode::Hunk(view) = &mut self.mode |
|
| 451 | 471 | && !view.is_editing() |
|
| 452 | 472 | { |
|
| 453 | 473 | match event { |
|
| 454 | 474 | Event::Key(Key::Esc) => { |
|
| 455 | 475 | self.mode = Mode::Diff; |
|
| 456 | 476 | return Ok(Control::Redraw); |
|
| 457 | 477 | } |
|
| 458 | 478 | Event::Key(Key::Tab) => { |
|
| 459 | - | self.next_tab(); |
|
| 479 | + | self.next_tab()?; |
|
| 460 | 480 | return Ok(Control::Redraw); |
|
| 461 | 481 | } |
|
| 462 | 482 | Event::Key(Key::Char('c') | Key::Enter) => { |
|
| 463 | 483 | view.start_new(); |
|
| 464 | 484 | return Ok(Control::Redraw); |
| 477 | 497 | } |
|
| 478 | 498 | Event::Key(Key::Char('d')) => { |
|
| 479 | 499 | self.delete_selected_comment()?; |
|
| 480 | 500 | return Ok(Control::Redraw); |
|
| 481 | 501 | } |
|
| 502 | + | _ if Self::hunk_scroll_event(&event) => { |
|
| 503 | + | return Ok(view.viewer.handle_input(event, hunk_area)); |
|
| 504 | + | } |
|
| 482 | 505 | Event::Resize(_) => return Ok(Control::Redraw), |
|
| 483 | 506 | _ => return Ok(Control::Continue), |
|
| 484 | 507 | } |
|
| 485 | 508 | } |
|
| 486 | 509 | match event { |
| 489 | 512 | view.cancel_edit(); |
|
| 490 | 513 | } |
|
| 491 | 514 | return Ok(Control::Redraw); |
|
| 492 | 515 | } |
|
| 493 | 516 | Event::Key(Key::Tab) => { |
|
| 494 | - | self.next_tab(); |
|
| 517 | + | self.next_tab()?; |
|
| 495 | 518 | return Ok(Control::Redraw); |
|
| 496 | 519 | } |
|
| 497 | 520 | // Ctrl+Enter submits; the enhanced keyboard protocol reports it as |
|
| 498 | 521 | // a control Return distinct from a plain Enter. |
|
| 499 | 522 | Event::Key(Key::Ctrl('\r')) => { |
| 505 | 528 | let Mode::Hunk(view) = &mut self.mode else { |
|
| 506 | 529 | return Ok(Control::Continue); |
|
| 507 | 530 | }; |
|
| 508 | 531 | let changed = match event { |
|
| 509 | 532 | Event::Key(Key::Enter) => view.newline(), |
|
| 533 | + | _ if Self::non_text_hunk_scroll_event(&event) => { |
|
| 534 | + | view.viewer.handle_input(event, hunk_area) == Control::Redraw |
|
| 535 | + | } |
|
| 510 | 536 | Event::Key(key) => view.input(key), |
|
| 511 | - | Event::Resize(_) => true, |
|
| 537 | + | Event::Resize(_) => { |
|
| 538 | + | view.viewer.apply(DiffAction::Resize, hunk_area) == ViewChange::Changed |
|
| 539 | + | } |
|
| 512 | 540 | _ => false, |
|
| 513 | 541 | }; |
|
| 514 | 542 | Ok(if changed { |
|
| 515 | 543 | Control::Redraw |
|
| 516 | 544 | } else { |
| 528 | 556 | return Ok(()); |
|
| 529 | 557 | }; |
|
| 530 | 558 | let Some(loaded) = &mut self.commits[self.current].loaded else { |
|
| 531 | 559 | return Ok(()); |
|
| 532 | 560 | }; |
|
| 561 | + | let added = matches!(target, CommentEdit::Add); |
|
| 533 | 562 | let changed = match target { |
|
| 534 | 563 | CommentEdit::Add => loaded.comments.author(&mut self.comments, hunk, body), |
|
| 535 | 564 | CommentEdit::Edit(id) => loaded.comments.edit(&mut self.comments, hunk, &id, body), |
|
| 536 | 565 | }; |
|
| 537 | 566 | if changed { |
|
| 538 | 567 | self.brain.write_comments(&self.comments.encode())?; |
|
| 539 | 568 | loaded.refresh_decorations(); |
|
| 569 | + | if added { |
|
| 570 | + | let comments = loaded.comments.for_hunk(hunk).len(); |
|
| 571 | + | view.select_comment(comments.saturating_sub(1)); |
|
| 572 | + | } |
|
| 540 | 573 | } |
|
| 541 | 574 | Ok(()) |
|
| 542 | 575 | } |
|
| 543 | 576 | ||
| 544 | 577 | /// Returns the number of comments on the active hunk. |
| 550 | 583 | .loaded |
|
| 551 | 584 | .as_ref() |
|
| 552 | 585 | .map_or(0, |loaded| loaded.comments.for_hunk(view.hunk).len()) |
|
| 553 | 586 | } |
|
| 554 | 587 | ||
| 588 | + | /// Returns rendered comment rows for the active hunk view. |
|
| 589 | + | fn hunk_comment_rows(&self) -> usize { |
|
| 590 | + | let Mode::Hunk(view) = &self.mode else { |
|
| 591 | + | return 0; |
|
| 592 | + | }; |
|
| 593 | + | view.comment_rows(self.hunk_comment_count()) |
|
| 594 | + | } |
|
| 595 | + | ||
| 596 | + | /// Returns whether an event should scroll the nested hunk diff viewer. |
|
| 597 | + | fn hunk_scroll_event(event: &Event<()>) -> bool { |
|
| 598 | + | if Self::non_text_hunk_scroll_event(event) { |
|
| 599 | + | return true; |
|
| 600 | + | } |
|
| 601 | + | matches!(event, Event::Key(Key::Char('j' | 'k' | 'g' | 'G' | ' '))) |
|
| 602 | + | } |
|
| 603 | + | ||
| 604 | + | /// Returns whether an event should scroll without being inserted as text. |
|
| 605 | + | fn non_text_hunk_scroll_event(event: &Event<()>) -> bool { |
|
| 606 | + | matches!( |
|
| 607 | + | event, |
|
| 608 | + | Event::Key( |
|
| 609 | + | Key::Down |
|
| 610 | + | | Key::Up |
|
| 611 | + | | Key::PageDown |
|
| 612 | + | | Key::PageUp |
|
| 613 | + | | Key::Ctrl('d' | 'u') |
|
| 614 | + | | Key::Home |
|
| 615 | + | | Key::End |
|
| 616 | + | ) | Event::Mouse(Mouse::Press { |
|
| 617 | + | button: MouseButton::WheelDown | MouseButton::WheelUp, |
|
| 618 | + | .. |
|
| 619 | + | }) |
|
| 620 | + | ) |
|
| 621 | + | } |
|
| 622 | + | ||
| 555 | 623 | /// Starts editing the selected hunk comment. |
|
| 556 | 624 | fn edit_selected_comment(&mut self) { |
|
| 557 | 625 | let Mode::Hunk(view) = &mut self.mode else { |
|
| 558 | 626 | return; |
|
| 559 | 627 | }; |
| 596 | 664 | /// Every activation increments `generation`. Highlight and context results |
|
| 597 | 665 | /// carry that value back through the event channel, which prevents output |
|
| 598 | 666 | /// from an older commit viewer from mutating the newly active viewer. |
|
| 599 | 667 | fn activate(&mut self) -> Result<(), Error> { |
|
| 600 | 668 | self.refresh_visibility()?; |
|
| 601 | - | self.generation += 1; |
|
| 602 | - | ||
| 603 | - | let generation = self.generation; |
|
| 604 | - | let tx = self.events_tx.clone(); |
|
| 605 | - | let repo = Some(self.backend.repo(&self.commits[self.current].commit)); |
|
| 606 | - | let loaded = self.current()?; |
|
| 607 | - | let highlighter = spawn_highlighter(loaded.viewer.document(), repo, move |output| { |
|
| 608 | - | let _ = tx.send(TerminalEvent::Highlight(generation, output)); |
|
| 609 | - | }); |
|
| 610 | - | self.highlighter = Some(highlighter); |
|
| 611 | - | self.context_fetch = ContextFetch::Idle; |
|
| 669 | + | self.restart_highlighter()?; |
|
| 612 | 670 | ||
| 613 | 671 | Ok(()) |
|
| 614 | 672 | } |
|
| 615 | 673 | ||
| 616 | 674 | /// Rebuilds the current commit's files to match accepted-hunk visibility. |
|
| 617 | 675 | /// |
|
| 618 | 676 | /// The original decoded files are kept in `CommitReview::files`; the viewer |
|
| 619 | - | /// receives a filtered copy when accepted hunks are hidden. Collapsed-file |
|
| 620 | - | /// state is preserved across the replacement and extended for fully |
|
| 621 | - | /// accepted files. |
|
| 677 | + | /// receives a filtered copy when accepted hunks are hidden. |
|
| 622 | 678 | fn refresh_visibility(&mut self) -> Result<(), Error> { |
|
| 623 | 679 | let visibility = self.visibility; |
|
| 624 | 680 | let loaded = self.current()?; |
|
| 625 | - | let mut collapsed = loaded.viewer.state().collapsed_files.clone(); |
|
| 626 | - | collapsed.extend(loaded.accepted_files()); |
|
| 627 | 681 | ||
| 628 | 682 | let files: Vec<FileDiff> = if visibility.show_accepted { |
|
| 629 | 683 | loaded.files.clone() |
|
| 630 | 684 | } else { |
|
| 631 | 685 | loaded |
| 638 | 692 | file |
|
| 639 | 693 | }) |
|
| 640 | 694 | .collect() |
|
| 641 | 695 | }; |
|
| 642 | 696 | loaded.viewer.replace_file_diffs(files); |
|
| 643 | - | loaded.viewer.set_collapsed_files(collapsed); |
|
| 644 | 697 | ||
| 645 | 698 | Ok(()) |
|
| 646 | 699 | } |
|
| 647 | 700 | ||
| 648 | 701 | /// Reconciles per-commit review state from the hidden tree. |
| 674 | 727 | } |
|
| 675 | 728 | ||
| 676 | 729 | /// Draws a frame, requesting highlights for visible rows before it and |
|
| 677 | 730 | /// lookahead rows after it. |
|
| 678 | 731 | fn redraw(&mut self) -> io::Result<()> { |
|
| 679 | - | let area = viewer_area(self.terminal.area()); |
|
| 680 | - | if let Some(loaded) = &mut self.commits[self.current].loaded { |
|
| 681 | - | let requests = loaded.viewer.visible_highlight_requests(area); |
|
| 682 | - | self.queue_highlights(requests); |
|
| 683 | - | } |
|
| 732 | + | let area = self.active_viewer_area(viewer_area(self.terminal.area())); |
|
| 733 | + | let requests = self |
|
| 734 | + | .active_viewer() |
|
| 735 | + | .map_or_else(Vec::new, |viewer| viewer.visible_highlight_requests(area)); |
|
| 736 | + | self.queue_highlights(requests); |
|
| 684 | 737 | self.draw_frame()?; |
|
| 685 | - | if let Some(loaded) = &mut self.commits[self.current].loaded { |
|
| 686 | - | let requests = loaded.viewer.lookahead_highlight_requests(area); |
|
| 687 | - | self.queue_highlights(requests); |
|
| 688 | - | } |
|
| 738 | + | let requests = self |
|
| 739 | + | .active_viewer() |
|
| 740 | + | .map_or_else(Vec::new, |viewer| viewer.lookahead_highlight_requests(area)); |
|
| 741 | + | self.queue_highlights(requests); |
|
| 689 | 742 | Ok(()) |
|
| 690 | 743 | } |
|
| 691 | 744 | ||
| 745 | + | /// Returns the diff viewer shown by the active mode, if any. |
|
| 746 | + | fn active_viewer(&mut self) -> Option<&mut DiffViewer> { |
|
| 747 | + | match &mut self.mode { |
|
| 748 | + | Mode::Hunk(view) => Some(&mut view.viewer), |
|
| 749 | + | Mode::Diff => self.commits[self.current] |
|
| 750 | + | .loaded |
|
| 751 | + | .as_mut() |
|
| 752 | + | .map(|loaded| &mut loaded.viewer), |
|
| 753 | + | Mode::Commits => None, |
|
| 754 | + | } |
|
| 755 | + | } |
|
| 756 | + | ||
| 757 | + | /// Returns the area the active mode's diff viewer occupies within `area`. |
|
| 758 | + | fn active_viewer_area(&self, area: Rect) -> Rect { |
|
| 759 | + | match &self.mode { |
|
| 760 | + | Mode::Hunk(view) => view.viewer_area(area, self.hunk_comment_rows()), |
|
| 761 | + | _ => area, |
|
| 762 | + | } |
|
| 763 | + | } |
|
| 764 | + | ||
| 692 | 765 | /// Queues highlight work, cancelling rows the worker cannot accept. |
|
| 693 | 766 | /// |
|
| 694 | 767 | /// The worker may drop requests when its queue is full or stale. Dropped |
|
| 695 | 768 | /// rows are removed from the viewer's pending cache so they can be requested |
|
| 696 | 769 | /// again on a later redraw. |
| 701 | 774 | let dropped = self |
|
| 702 | 775 | .highlighter |
|
| 703 | 776 | .as_ref() |
|
| 704 | 777 | .and_then(|highlighter| highlighter.highlight(requests)); |
|
| 705 | 778 | if let Some(dropped) = dropped |
|
| 706 | - | && let Some(loaded) = &mut self.commits[self.current].loaded |
|
| 779 | + | && let Some(viewer) = self.active_viewer() |
|
| 707 | 780 | { |
|
| 708 | - | loaded.viewer.cancel_highlights(dropped); |
|
| 781 | + | viewer.cancel_highlights(dropped); |
|
| 709 | 782 | } |
|
| 710 | 783 | } |
|
| 711 | 784 | ||
| 785 | + | /// Restarts asynchronous syntax highlighting for the active generation. |
|
| 786 | + | fn restart_highlighter(&mut self) -> Result<(), Error> { |
|
| 787 | + | self.generation += 1; |
|
| 788 | + | ||
| 789 | + | let generation = self.generation; |
|
| 790 | + | let tx = self.events_tx.clone(); |
|
| 791 | + | let highlighter = if let Mode::Hunk(view) = &self.mode { |
|
| 792 | + | spawn_highlighter(view.viewer.document(), None, move |output| { |
|
| 793 | + | let _ = tx.send(TerminalEvent::Highlight(generation, output)); |
|
| 794 | + | }) |
|
| 795 | + | } else { |
|
| 796 | + | let repo = Some(self.backend.repo(&self.commits[self.current].commit)); |
|
| 797 | + | let loaded = self.current()?; |
|
| 798 | + | spawn_highlighter(loaded.viewer.document(), repo, move |output| { |
|
| 799 | + | let _ = tx.send(TerminalEvent::Highlight(generation, output)); |
|
| 800 | + | }) |
|
| 801 | + | }; |
|
| 802 | + | self.highlighter = Some(highlighter); |
|
| 803 | + | self.context_fetch = ContextFetch::Idle; |
|
| 804 | + | ||
| 805 | + | Ok(()) |
|
| 806 | + | } |
|
| 807 | + | ||
| 712 | 808 | /// Requests context refetch work for one context change. |
|
| 713 | 809 | /// |
|
| 714 | 810 | /// Only one context request is in flight per active commit. Requests are |
|
| 715 | 811 | /// tied to the current highlighter generation by `activate`, so stale |
|
| 716 | 812 | /// context updates are ignored in [`Session::update`]. |
| 737 | 833 | fn draw_frame(&mut self) -> io::Result<()> { |
|
| 738 | 834 | let mut footer = self.footer(); |
|
| 739 | 835 | let current = self.current; |
|
| 740 | 836 | match &mut self.mode { |
|
| 741 | 837 | Mode::Hunk(view) => { |
|
| 742 | - | let comments = self.commits[current] |
|
| 838 | + | let mut comments = self.commits[current] |
|
| 743 | 839 | .loaded |
|
| 744 | 840 | .as_ref() |
|
| 745 | - | .map_or(&[][..], |loaded| loaded.comments.for_hunk(view.hunk)); |
|
| 746 | - | view.clamp_selection(comments.len()); |
|
| 747 | - | let selected = view.selected(); |
|
| 748 | - | let mut comment_lines = comment_block_lines(comments, selected); |
|
| 841 | + | .map_or_else(Vec::new, |loaded| loaded.comments.for_hunk(view.hunk).to_vec()); |
|
| 749 | 842 | self.terminal.draw(|frame| { |
|
| 750 | 843 | let (body, footer_area) = Status::areas(frame.area); |
|
| 751 | - | frame.render_stateful_widget(&mut **view, body, &mut comment_lines); |
|
| 844 | + | frame.render_stateful_widget(&mut **view, body, &mut comments); |
|
| 752 | 845 | frame.render_widget(&mut footer, footer_area); |
|
| 753 | 846 | })?; |
|
| 754 | 847 | } |
|
| 755 | 848 | Mode::Commits => { |
|
| 756 | - | let rows: Vec<Line> = self.commits.iter().map(commit_row).collect(); |
|
| 849 | + | let palette = self.options.diff_palette; |
|
| 850 | + | let rows: Vec<Line> = self |
|
| 851 | + | .commits |
|
| 852 | + | .iter() |
|
| 853 | + | .map(|slot| commit_row(slot, palette)) |
|
| 854 | + | .collect(); |
|
| 757 | 855 | self.terminal.draw(|frame| { |
|
| 758 | 856 | let (body, footer_area) = Status::areas(frame.area); |
|
| 759 | 857 | frame.render_stateful_widget(List::new(rows), body, &mut self.list); |
|
| 760 | 858 | frame.render_widget(&mut footer, footer_area); |
|
| 761 | 859 | })?; |
crates/brain/src/session/commit.rs
+2 -21
| 1 | 1 | //! Per-commit review state for the session. |
|
| 2 | 2 | ||
| 3 | - | use std::collections::{BTreeMap, BTreeSet}; |
|
| 3 | + | use std::collections::BTreeMap; |
|
| 4 | 4 | use std::mem; |
|
| 5 | 5 | ||
| 6 | 6 | use rustik_diff::{DiffDocument, DiffOptions, DiffViewer, Encode as _, FileDiff, FileId, HunkId}; |
|
| 7 | 7 | use rustik_repo::PatchId; |
|
| 8 | 8 |
| 132 | 132 | file.hunks = hunks; |
|
| 133 | 133 | } |
|
| 134 | 134 | document |
|
| 135 | 135 | } |
|
| 136 | 136 | ||
| 137 | - | /// Returns whether every original hunk of `file` has been accepted. |
|
| 138 | - | fn accepts_file(&self, file: &FileDiff) -> bool { |
|
| 139 | - | self.review |
|
| 140 | - | .accepts_all(file.hunks.iter().map(|hunk| hunk.id)) |
|
| 141 | - | } |
|
| 142 | - | ||
| 143 | - | /// Returns files whose original hunks have all been accepted. |
|
| 144 | - | pub(super) fn accepted_files(&self) -> BTreeSet<FileId> { |
|
| 145 | - | self.files |
|
| 146 | - | .iter() |
|
| 147 | - | .filter(|file| self.accepts_file(file)) |
|
| 148 | - | .map(|file| file.id) |
|
| 149 | - | .collect() |
|
| 150 | - | } |
|
| 151 | - | ||
| 152 | 137 | /// Reinstalls a fresh decoration snapshot on the viewer. |
|
| 153 | 138 | /// |
|
| 154 | 139 | /// Decorations are immutable snapshots, so this must run after every |
|
| 155 | 140 | /// change to acceptance or comment state for the viewer to observe it. |
|
| 156 | 141 | pub(super) fn refresh_decorations(&mut self) { |
|
| 157 | 142 | self.viewer.set_decorator(Decorations::snapshot( |
|
| 158 | 143 | &self.review, |
|
| 159 | 144 | &self.comments, |
|
| 160 | 145 | &self.file_hunks, |
|
| 146 | + | self.viewer.options().diff_palette, |
|
| 161 | 147 | )); |
|
| 162 | 148 | } |
|
| 163 | 149 | } |
|
| 164 | 150 | ||
| 165 | 151 | #[cfg(test)] |
|
| 166 | 152 | mod tests { |
|
| 167 | 153 | use super::*; |
|
| 168 | 154 | use rustik_diff::Decode as _; |
|
| 169 | 155 | ||
| 170 | - | /// Builds a two-file review for accepted-file tests. |
|
| 171 | - | fn review_with_two_files() -> CommitReview { |
|
| 172 | - | let document = DiffDocument::decode_str( |
|
| 173 | - | "\ |
|
| 174 | - |
a.rs
+0 -8
| 1 | - | +++ b/a.rs |
|
| 2 | - | @@ -1 +1 @@ |
|
| 3 | - | -old |
|
| 4 | - | +new |
|
| 5 | - | @@ -4 +4 @@ |
|
| 6 | - | -before |
|
| 7 | - | +after |
|
| 8 | - |
b.rs
+0 -53
| 1 | - | +++ b/b.rs |
|
| 2 | - | @@ -1 +1 @@ |
|
| 3 | - | -left |
|
| 4 | - | +right |
|
| 5 | - | ", |
|
| 6 | - | ) |
|
| 7 | - | .unwrap(); |
|
| 8 | - | let files = document.files.clone(); |
|
| 9 | - | let file_hunks = files |
|
| 10 | - | .iter() |
|
| 11 | - | .map(|file| { |
|
| 12 | - | ( |
|
| 13 | - | file.id, |
|
| 14 | - | file.hunks.iter().map(|hunk| hunk.id).collect::<Vec<_>>(), |
|
| 15 | - | ) |
|
| 16 | - | }) |
|
| 17 | - | .collect(); |
|
| 18 | - | let viewer = DiffViewer::new(document, DiffOptions::default()); |
|
| 19 | - | ||
| 20 | - | CommitReview { |
|
| 21 | - | viewer, |
|
| 22 | - | review: Review::new(), |
|
| 23 | - | comments: Comments::new(&ReviewComments::parse("").unwrap(), BTreeMap::new()), |
|
| 24 | - | hunk_patches: BTreeMap::new(), |
|
| 25 | - | patch_ids: BTreeMap::new(), |
|
| 26 | - | files, |
|
| 27 | - | file_hunks, |
|
| 28 | - | } |
|
| 29 | - | } |
|
| 30 | - | ||
| 31 | - | #[test] |
|
| 32 | - | fn accepted_files_require_every_hunk_in_that_file() { |
|
| 33 | - | let mut review = review_with_two_files(); |
|
| 34 | - | ||
| 35 | - | review.review.replace(BTreeSet::from([HunkId::new(0)])); |
|
| 36 | - | assert!(review.accepted_files().is_empty()); |
|
| 37 | - | ||
| 38 | - | review |
|
| 39 | - | .review |
|
| 40 | - | .replace(BTreeSet::from([HunkId::new(0), HunkId::new(1)])); |
|
| 41 | - | assert_eq!(review.accepted_files(), BTreeSet::from([FileId::new(0)])); |
|
| 42 | - | ||
| 43 | - | review.review.replace(BTreeSet::from([ |
|
| 44 | - | HunkId::new(0), |
|
| 45 | - | HunkId::new(1), |
|
| 46 | - | HunkId::new(2), |
|
| 47 | - | ])); |
|
| 48 | - | assert_eq!( |
|
| 49 | - | review.accepted_files(), |
|
| 50 | - | BTreeSet::from([FileId::new(0), FileId::new(1)]) |
|
| 51 | - | ); |
|
| 52 | - | } |
|
| 53 | - | ||
| 54 | 1 | #[test] |
|
| 55 | 2 | fn review_hunks_split_by_change_group() { |
|
| 56 | 3 | let document = DiffDocument::decode_str( |
|
| 57 | 4 | "\ |
|
| 58 | 5 |
a.rs
+0 -0
crates/brain/src/session/hunk.rs
+181 -47
| 1 | 1 | //! Hunk comment editor view. |
|
| 2 | 2 | ||
| 3 | 3 | use rustik_diff::{DiffViewer, HunkId}; |
|
| 4 | 4 | use rustik_tui::layout::Layout; |
|
| 5 | 5 | use rustik_tui::{ |
|
| 6 | - | Buffer, Constraint, Direction, Length, Line, RenderResult, Size, StatefulWidget, TextField, |
|
| 7 | - | TextInput, |
|
| 6 | + | Buffer, Color, Constraint, Direction, Length, Line, Rect, RenderResult, Size, StatefulWidget, |
|
| 7 | + | Style, TextField, TextInput, Widget, |
|
| 8 | 8 | }; |
|
| 9 | 9 | ||
| 10 | - | use super::status::heading; |
|
| 10 | + | use super::status::{ |
|
| 11 | + | COMMENT_CARD_CHROME_COLS, COMMENT_CARD_CHROME_ROWS, COMMENT_CARD_ROWS, comment_age, |
|
| 12 | + | comment_card_lines, comment_editor_lines, heading, |
|
| 13 | + | }; |
|
| 11 | 14 | use crate::comments::{Comment, CommentId}; |
|
| 12 | 15 | ||
| 13 | 16 | /// Maximum rows the comment input grows to. |
|
| 14 | 17 | const MAX_INPUT_ROWS: usize = 6; |
|
| 15 | 18 | /// Maximum rows used to list a hunk's existing comments. |
| 54 | 57 | pub(super) struct HunkView { |
|
| 55 | 58 | /// Hunk being commented on. |
|
| 56 | 59 | pub(super) hunk: HunkId, |
|
| 57 | 60 | /// Viewer rendering the single hunk in isolation. |
|
| 58 | 61 | pub(super) viewer: DiffViewer, |
|
| 62 | + | /// Author shown for newly authored draft comments. |
|
| 63 | + | author: String, |
|
| 59 | 64 | /// Current comment mode. |
|
| 60 | 65 | mode: HunkMode, |
|
| 61 | 66 | } |
|
| 62 | 67 | ||
| 63 | 68 | impl HunkView { |
|
| 64 | 69 | /// Creates a hunk comment view. |
|
| 65 | - | pub(super) fn new(hunk: HunkId, viewer: DiffViewer) -> Self { |
|
| 70 | + | pub(super) fn new(hunk: HunkId, viewer: DiffViewer, author: String) -> Self { |
|
| 66 | 71 | Self { |
|
| 67 | 72 | hunk, |
|
| 68 | 73 | viewer, |
|
| 74 | + | author, |
|
| 69 | 75 | mode: HunkMode::Browsing { selected: 0 }, |
|
| 70 | 76 | } |
|
| 71 | 77 | } |
|
| 72 | 78 | ||
| 73 | 79 | /// Returns whether the view is editing a comment. |
| 161 | 167 | let HunkMode::Browsing { selected } = &mut self.mode else { |
|
| 162 | 168 | return; |
|
| 163 | 169 | }; |
|
| 164 | 170 | *selected = (*selected).min(comments.saturating_sub(1)); |
|
| 165 | 171 | } |
|
| 172 | + | ||
| 173 | + | /// Selects a stored comment directly. |
|
| 174 | + | pub(super) fn select_comment(&mut self, comment: usize) { |
|
| 175 | + | let HunkMode::Browsing { selected } = &mut self.mode else { |
|
| 176 | + | return; |
|
| 177 | + | }; |
|
| 178 | + | *selected = comment; |
|
| 179 | + | } |
|
| 180 | + | ||
| 181 | + | /// Returns the area occupied by the isolated hunk viewer. |
|
| 182 | + | pub(super) fn viewer_area(&self, area: Rect, comment_rows: usize) -> Rect { |
|
| 183 | + | self.areas(area, comment_rows)[0] |
|
| 184 | + | } |
|
| 185 | + | ||
| 186 | + | /// Returns rendered rows needed for `comments` stored cards and the editor. |
|
| 187 | + | /// |
|
| 188 | + | /// A comment being edited is rendered as the editor card in its place, so |
|
| 189 | + | /// it does not count towards the stored cards. |
|
| 190 | + | pub(super) fn comment_rows(&self, comments: usize) -> usize { |
|
| 191 | + | let stored = match &self.mode { |
|
| 192 | + | HunkMode::Editing { |
|
| 193 | + | target: CommentEdit::Edit(_), |
|
| 194 | + | .. |
|
| 195 | + | } => comments.saturating_sub(1), |
|
| 196 | + | _ => comments, |
|
| 197 | + | }; |
|
| 198 | + | (stored * COMMENT_CARD_ROWS).min(MAX_COMMENT_ROWS) + self.editor_rows() |
|
| 199 | + | } |
|
| 200 | + | ||
| 201 | + | /// Returns the number of table-body rows needed by the active editor. |
|
| 202 | + | fn editor_body_rows(&self) -> usize { |
|
| 203 | + | match &self.mode { |
|
| 204 | + | HunkMode::Editing { input, .. } => { |
|
| 205 | + | input.value().split('\n').count().clamp(1, MAX_INPUT_ROWS) |
|
| 206 | + | } |
|
| 207 | + | HunkMode::Browsing { .. } => 0, |
|
| 208 | + | } |
|
| 209 | + | } |
|
| 210 | + | ||
| 211 | + | /// Returns the number of rendered rows needed by the active editor. |
|
| 212 | + | fn editor_rows(&self) -> usize { |
|
| 213 | + | if self.is_editing() { |
|
| 214 | + | self.editor_body_rows() + COMMENT_CARD_CHROME_ROWS |
|
| 215 | + | } else { |
|
| 216 | + | 0 |
|
| 217 | + | } |
|
| 218 | + | } |
|
| 219 | + | ||
| 220 | + | /// Returns the hunk, comment, spacer, and input areas. |
|
| 221 | + | fn areas(&self, area: Rect, comment_rows: usize) -> [Rect; 4] { |
|
| 222 | + | let hint_h = 1.min(area.rows); |
|
| 223 | + | let available = area.rows.saturating_sub(hint_h); |
|
| 224 | + | let comments_h = comment_rows.min(available); |
|
| 225 | + | let hunk_available = available.saturating_sub(comments_h); |
|
| 226 | + | let hunk_h = self |
|
| 227 | + | .viewer |
|
| 228 | + | .size(Constraint::max(Size::new(area.cols, hunk_available))) |
|
| 229 | + | .rows |
|
| 230 | + | .min(hunk_available); |
|
| 231 | + | ||
| 232 | + | Layout::new(Direction::Vertical) |
|
| 233 | + | .child(Length::Fixed(hunk_h)) |
|
| 234 | + | .child(Length::Fixed(comments_h)) |
|
| 235 | + | .child(Length::Fill(1)) |
|
| 236 | + | .child(Length::Fixed(hint_h)) |
|
| 237 | + | .areas(area) |
|
| 238 | + | } |
|
| 239 | + | } |
|
| 240 | + | ||
| 241 | + | impl HunkView { |
|
| 242 | + | /// Returns lines for every comment card, swapping the edited card for the |
|
| 243 | + | /// editor, and the editor card's row offset within them. |
|
| 244 | + | /// |
|
| 245 | + | /// Cards are windowed so the selected card stays visible with one card of |
|
| 246 | + | /// context above it. Stored cards are capped at [`MAX_COMMENT_ROWS`]; a |
|
| 247 | + | /// new-draft editor always follows them. |
|
| 248 | + | fn comment_lines(&self, comments: &[Comment], cols: usize) -> (Vec<Line>, Option<usize>) { |
|
| 249 | + | let selected = self.selected(); |
|
| 250 | + | let first = selected.saturating_sub(1); |
|
| 251 | + | let editor_body_rows = self.editor_body_rows(); |
|
| 252 | + | let mut lines = Vec::new(); |
|
| 253 | + | let mut editor_row = None; |
|
| 254 | + | let mut stored_rows = 0; |
|
| 255 | + | ||
| 256 | + | for (index, comment) in comments.iter().enumerate().skip(first) { |
|
| 257 | + | let edited = matches!( |
|
| 258 | + | &self.mode, |
|
| 259 | + | HunkMode::Editing { target: CommentEdit::Edit(id), .. } if *id == comment.id |
|
| 260 | + | ); |
|
| 261 | + | if edited { |
|
| 262 | + | editor_row = Some(lines.len()); |
|
| 263 | + | lines.extend(comment_editor_lines( |
|
| 264 | + | &comment.author, |
|
| 265 | + | &comment_age(comment), |
|
| 266 | + | editor_body_rows, |
|
| 267 | + | cols, |
|
| 268 | + | )); |
|
| 269 | + | } else if stored_rows < MAX_COMMENT_ROWS { |
|
| 270 | + | let card = comment_card_lines( |
|
| 271 | + | comment, |
|
| 272 | + | !self.is_editing() && index == selected, |
|
| 273 | + | cols, |
|
| 274 | + | ); |
|
| 275 | + | let take = card.len().min(MAX_COMMENT_ROWS - stored_rows); |
|
| 276 | + | stored_rows += take; |
|
| 277 | + | lines.extend(card.into_iter().take(take)); |
|
| 278 | + | } |
|
| 279 | + | } |
|
| 280 | + | if matches!( |
|
| 281 | + | &self.mode, |
|
| 282 | + | HunkMode::Editing { |
|
| 283 | + | target: CommentEdit::Add, |
|
| 284 | + | .. |
|
| 285 | + | } |
|
| 286 | + | ) { |
|
| 287 | + | editor_row = Some(lines.len()); |
|
| 288 | + | lines.extend(comment_editor_lines( |
|
| 289 | + | &self.author, |
|
| 290 | + | "just now", |
|
| 291 | + | editor_body_rows, |
|
| 292 | + | cols, |
|
| 293 | + | )); |
|
| 294 | + | } |
|
| 295 | + | ||
| 296 | + | (lines, editor_row) |
|
| 297 | + | } |
|
| 166 | 298 | } |
|
| 167 | 299 | ||
| 168 | 300 | impl StatefulWidget for HunkView { |
|
| 169 | - | type State = Vec<Line>; |
|
| 301 | + | type State = Vec<Comment>; |
|
| 170 | 302 | ||
| 171 | 303 | fn size(&self, parent: Constraint) -> Size { |
|
| 172 | 304 | parent.bounded_size() |
|
| 173 | 305 | } |
|
| 174 | 306 |
| 179 | 311 | comments: &mut Self::State, |
|
| 180 | 312 | ) -> RenderResult { |
|
| 181 | 313 | if area.is_empty() { |
|
| 182 | 314 | return RenderResult::default(); |
|
| 183 | 315 | } |
|
| 184 | - | let input_rows = match &self.mode { |
|
| 185 | - | HunkMode::Editing { input, .. } => { |
|
| 186 | - | input.value().split('\n').count().clamp(1, MAX_INPUT_ROWS) |
|
| 187 | - | } |
|
| 188 | - | HunkMode::Browsing { .. } => 0, |
|
| 189 | - | }; |
|
| 190 | - | let input_h = if self.is_editing() { |
|
| 191 | - | (input_rows + 1).min(area.rows) |
|
| 192 | - | } else { |
|
| 193 | - | 1.min(area.rows) |
|
| 194 | - | }; |
|
| 195 | - | let comments_h = if comments.is_empty() { |
|
| 196 | - | 0 |
|
| 197 | - | } else { |
|
| 198 | - | (comments.len().min(MAX_COMMENT_ROWS) + 1).min(area.rows.saturating_sub(input_h)) |
|
| 199 | - | }; |
|
| 200 | - | let [hunk_area, comments_area, input_area] = Layout::new(Direction::Vertical) |
|
| 201 | - | .child(Length::Fill(1)) |
|
| 202 | - | .child(Length::Fixed(comments_h)) |
|
| 203 | - | .child(Length::Fixed(input_h)) |
|
| 204 | - | .areas(area); |
|
| 316 | + | self.clamp_selection(comments.len()); |
|
| 317 | + | let [hunk_area, comments_area, _, hint_area] = |
|
| 318 | + | self.areas(area, self.comment_rows(comments.len())); |
|
| 319 | + | let mut result = RenderResult::default(); |
|
| 205 | 320 | ||
| 206 | 321 | self.viewer.render(hunk_area, buf); |
|
| 207 | 322 | ||
| 208 | - | if !comments_area.is_empty() { |
|
| 209 | - | let (heading_area, body_area) = comments_area.split_top(1); |
|
| 323 | + | let (lines, editor_row) = self.comment_lines(comments, comments_area.cols); |
|
| 324 | + | Line::renders( |
|
| 325 | + | lines.into_iter().take(comments_area.rows), |
|
| 326 | + | comments_area, |
|
| 327 | + | buf, |
|
| 328 | + | ); |
|
| 210 | 329 | ||
| 211 | - | buf.set_line( |
|
| 212 | - | heading_area.x, |
|
| 213 | - | heading_area.y, |
|
| 214 | - | &heading("comments", area.cols), |
|
| 215 | - | ); |
|
| 216 | - | Line::renders( |
|
| 217 | - | comments.iter().take(comments_h - 1).cloned(), |
|
| 218 | - | body_area, |
|
| 219 | - | buf, |
|
| 330 | + | // The editable body sits below the edited card's top border and header |
|
| 331 | + | // row, inset past the side borders. |
|
| 332 | + | let editor_body_rows = self.editor_body_rows(); |
|
| 333 | + | if let (Some(row), HunkMode::Editing { input, .. }) = (editor_row, &mut self.mode) { |
|
| 334 | + | let card_rows = comments_area.rows.saturating_sub(row); |
|
| 335 | + | let body_area = Rect::new( |
|
| 336 | + | comments_area.x.saturating_add(COMMENT_CARD_CHROME_COLS), |
|
| 337 | + | comments_area.y + row + COMMENT_CARD_CHROME_ROWS - 1, |
|
| 338 | + | comments_area |
|
| 339 | + | .cols |
|
| 340 | + | .saturating_sub(2 * COMMENT_CARD_CHROME_COLS), |
|
| 341 | + | editor_body_rows.min(card_rows.saturating_sub(COMMENT_CARD_CHROME_ROWS)), |
|
| 220 | 342 | ); |
|
| 343 | + | if !body_area.is_empty() { |
|
| 344 | + | result = TextField::new() |
|
| 345 | + | .style(Style::default().fg(Color::White)) |
|
| 346 | + | .placeholder("Write a comment...") |
|
| 347 | + | .render_with_state(body_area, buf, input); |
|
| 348 | + | } |
|
| 221 | 349 | } |
|
| 222 | - | let (heading_area, input_area) = input_area.split_top(1); |
|
| 223 | 350 | ||
| 224 | 351 | let hint = if self.is_editing() { |
|
| 225 | 352 | EDIT_HINT |
|
| 226 | 353 | } else { |
|
| 227 | 354 | BROWSE_HINT |
|
| 228 | 355 | }; |
|
| 229 | - | buf.set_line(heading_area.x, heading_area.y, &heading(hint, area.cols)); |
|
| 230 | - | ||
| 231 | - | if let HunkMode::Editing { input, .. } = &mut self.mode |
|
| 232 | - | && !input_area.is_empty() |
|
| 233 | - | { |
|
| 234 | - | return TextField::new() |
|
| 235 | - | .placeholder("Write a comment...") |
|
| 236 | - | .render_with_state(input_area, buf, input); |
|
| 356 | + | if !hint_area.is_empty() { |
|
| 357 | + | buf.set_line(hint_area.x, hint_area.y, &heading(hint, area.cols)); |
|
| 237 | 358 | } |
|
| 238 | - | RenderResult::default() |
|
| 359 | + | result |
|
| 360 | + | } |
|
| 361 | + | } |
|
| 362 | + | ||
| 363 | + | #[cfg(test)] |
|
| 364 | + | mod tests { |
|
| 365 | + | use rustik_diff::{Decode as _, DiffDocument, DiffOptions, FileTreeMode}; |
|
| 366 | + | ||
| 367 | + | use super::*; |
|
| 368 | + | ||
| 369 | + | fn hunk_view() -> HunkView { |
|
| 370 | + | let document = DiffDocument::decode_str( |
|
| 371 | + | "\ |
|
| 372 | + |
a.txt
+0 -0
crates/brain/src/session/status.rs
+157 -22
| 1 | 1 | //! Status and chrome rendering for the review session. |
|
| 2 | 2 | ||
| 3 | - | use rustik_tui::{Buffer, Color, Constraint, HStack, Label, Line, Rect, Size, Style, Widget}; |
|
| 3 | + | use std::time::{SystemTime, UNIX_EPOCH}; |
|
| 4 | + | ||
| 5 | + | use rustik_diff::DiffPalette; |
|
| 6 | + | use rustik_tui::{ |
|
| 7 | + | Border, Buffer, Color, Constraint, HStack, Label, Line, Rect, Size, Style, Table, TableOptions, |
|
| 8 | + | Widget, colors, |
|
| 9 | + | }; |
|
| 4 | 10 | ||
| 5 | 11 | use crate::comments::Comment; |
|
| 6 | 12 | use crate::review::Visibility; |
|
| 7 | 13 | ||
| 8 | 14 | use super::commit::CommitSlot; |
| 11 | 17 | const FOOTER_ROWS: usize = 1; |
|
| 12 | 18 | /// Foreground color for dim footer text. |
|
| 13 | 19 | const FOOTER_FG: Color = Color::Fixed(240); |
|
| 14 | 20 | /// Background color for the footer row. |
|
| 15 | 21 | const FOOTER_BG: Color = Color::Fixed(233); |
|
| 16 | - | /// Marker prefixing a stored comment in the hunk view. |
|
| 17 | - | const COMMENT_PREFIX: &str = "\u{1F5E8}"; |
|
| 22 | + | /// Border color for the selected comment in the hunk view. |
|
| 23 | + | const SELECTED_COMMENT_BORDER: Color = Color::Fixed(239); |
|
| 18 | 24 | /// Marker shown beside a commit whose hunks have all been accepted. |
|
| 19 | - | const REVIEWED_MARK: char = '\u{2713}'; |
|
| 25 | + | const REVIEWED_MARK: char = '\u{2714}'; |
|
| 20 | 26 | /// Marker shown when accepted hunks are visible in the footer. |
|
| 21 | 27 | const APPROVED_HUNKS_MODE_ICON: &str = "\u{2714}"; |
|
| 22 | 28 | /// Marker shown when full-file mode is visible in the footer. |
|
| 23 | 29 | const FULL_FILE_MODE_ICON: &str = "\u{2195}"; |
|
| 30 | + | /// Rendered rows in one stored comment card. |
|
| 31 | + | pub(super) const COMMENT_CARD_ROWS: usize = 4; |
|
| 32 | + | /// Rows of card chrome around a comment body: the top border, the header row, |
|
| 33 | + | /// and the bottom border. |
|
| 34 | + | pub(super) const COMMENT_CARD_CHROME_ROWS: usize = 3; |
|
| 35 | + | /// Columns of card chrome on each side of a comment body: the border and its |
|
| 36 | + | /// padding. |
|
| 37 | + | pub(super) const COMMENT_CARD_CHROME_COLS: usize = 2; |
|
| 24 | 38 | ||
| 25 | 39 | /// Footer status describing the current commit and review progress. |
|
| 26 | 40 | #[derive(Debug)] |
|
| 27 | 41 | pub(super) struct Status { |
|
| 28 | 42 | /// One-based position and total commit count. |
| 107 | 121 | .padded(cols) |
|
| 108 | 122 | .truncated(cols, "") |
|
| 109 | 123 | } |
|
| 110 | 124 | ||
| 111 | 125 | /// Renders one commit-list row: hash, summary, and diff statistics. |
|
| 112 | - | pub(super) fn commit_row(slot: &CommitSlot) -> Line { |
|
| 126 | + | pub(super) fn commit_row(slot: &CommitSlot, palette: DiffPalette) -> Line { |
|
| 113 | 127 | let stats = slot.commit.stats; |
|
| 114 | 128 | let short = slot.commit.id.short(); |
|
| 115 | 129 | let status = if slot.reviewed { |
|
| 116 | - | Label::from_char(REVIEWED_MARK).fg(Color::Green) |
|
| 130 | + | Label::from_char(REVIEWED_MARK).fg(palette.positive_fg) |
|
| 117 | 131 | } else { |
|
| 118 | 132 | Label::space() |
|
| 119 | 133 | }; |
|
| 120 | 134 | Line::spaced([ |
|
| 121 | 135 | status, |
| 125 | 139 | Label::new(&format!("-{}", stats.deletions)).fg(Color::Red), |
|
| 126 | 140 | Label::new(&format!("{} files", stats.files_changed)).fg(FOOTER_FG), |
|
| 127 | 141 | ]) |
|
| 128 | 142 | } |
|
| 129 | 143 | ||
| 130 | - | /// Renders a hunk's stored comments as styled lines for the hunk view. |
|
| 131 | - | pub(super) fn comment_block_lines(comments: &[Comment], selected: usize) -> Vec<Line> { |
|
| 132 | - | let mut lines = Vec::new(); |
|
| 133 | - | for (index, comment) in comments.iter().enumerate() { |
|
| 134 | - | if index > 0 { |
|
| 135 | - | lines.push(Line::blank()); |
|
| 136 | - | } |
|
| 137 | - | let style = (index == selected).then(|| Style::default().bg(Color::Fixed(236))); |
|
| 138 | - | for (row, body) in comment.body.split('\n').enumerate() { |
|
| 139 | - | let prefix = if row == 0 { COMMENT_PREFIX } else { " " }; |
|
| 140 | - | let mut line = Line::new(Label::new(&format!("{prefix} {body}"))); |
|
| 141 | - | if let Some(style) = style { |
|
| 142 | - | line = line.style(style); |
|
| 143 | - | } |
|
| 144 | - | lines.push(line); |
|
| 144 | + | /// Renders one stored comment as a bordered card. |
|
| 145 | + | pub(super) fn comment_card_lines(comment: &Comment, selected: bool, cols: usize) -> Vec<Line> { |
|
| 146 | + | let border = if selected { |
|
| 147 | + | SELECTED_COMMENT_BORDER |
|
| 148 | + | } else { |
|
| 149 | + | colors::FAINT |
|
| 150 | + | }; |
|
| 151 | + | let body = Line::new(Label::new(&comment.body.replace('\n', " ")).fg(Color::White)); |
|
| 152 | + | let header = comment_header( |
|
| 153 | + | &comment.author, |
|
| 154 | + | &comment_age(comment), |
|
| 155 | + | comment_table_content_width(cols).max(body.width()), |
|
| 156 | + | ); |
|
| 157 | + | let mut table = Table::<1, Line>::new(TableOptions { |
|
| 158 | + | border: Some(Border::new(border)), |
|
| 159 | + | ..TableOptions::bordered() |
|
| 160 | + | }); |
|
| 161 | + | table.header([header]); |
|
| 162 | + | table.push([body]); |
|
| 163 | + | table.lines(Constraint::tight(cols)) |
|
| 164 | + | } |
|
| 165 | + | ||
| 166 | + | /// Renders an editable comment card with a blank body of `rows` lines. |
|
| 167 | + | pub(super) fn comment_editor_lines(author: &str, age: &str, rows: usize, cols: usize) -> Vec<Line> { |
|
| 168 | + | let body = Line::blank().padded(comment_table_content_width(cols)); |
|
| 169 | + | let mut table = Table::<1, Line>::new(TableOptions { |
|
| 170 | + | border: Some(Border::new(SELECTED_COMMENT_BORDER)), |
|
| 171 | + | ..TableOptions::bordered() |
|
| 172 | + | }); |
|
| 173 | + | table.header([comment_header( |
|
| 174 | + | author, |
|
| 175 | + | age, |
|
| 176 | + | comment_table_content_width(cols), |
|
| 177 | + | )]); |
|
| 178 | + | for _ in 0..rows.max(1) { |
|
| 179 | + | table.push([body.clone()]); |
|
| 180 | + | } |
|
| 181 | + | table.lines(Constraint::tight(cols)) |
|
| 182 | + | } |
|
| 183 | + | ||
| 184 | + | /// Returns a comment's age as a human relative timestamp. |
|
| 185 | + | pub(super) fn comment_age(comment: &Comment) -> String { |
|
| 186 | + | relative_time(comment.timestamp, current_timestamp()) |
|
| 187 | + | } |
|
| 188 | + | ||
| 189 | + | /// Returns the comment-card content width: the rendered width minus the |
|
| 190 | + | /// border and padding columns on both sides. |
|
| 191 | + | fn comment_table_content_width(cols: usize) -> usize { |
|
| 192 | + | cols.saturating_sub(2 * COMMENT_CARD_CHROME_COLS) |
|
| 193 | + | } |
|
| 194 | + | ||
| 195 | + | /// Returns a comment-table header from displayable fields. |
|
| 196 | + | fn comment_header(author: &str, timestamp: &str, body_width: usize) -> Line { |
|
| 197 | + | Line::spaced([ |
|
| 198 | + | Label::new(author).style(Style::default().fg(Color::White).bold()), |
|
| 199 | + | Label::new(timestamp).fg(colors::fixed::DIM), |
|
| 200 | + | ]) |
|
| 201 | + | .padded(body_width) |
|
| 202 | + | } |
|
| 203 | + | ||
| 204 | + | /// Returns a human relative timestamp in the largest whole unit. |
|
| 205 | + | fn relative_time(timestamp: u64, now: u64) -> String { |
|
| 206 | + | /// Time units from largest to smallest, as seconds and unit name. |
|
| 207 | + | const UNITS: [(u64, &str); 5] = [ |
|
| 208 | + | (31_536_000, "year"), |
|
| 209 | + | (2_592_000, "month"), |
|
| 210 | + | (86_400, "day"), |
|
| 211 | + | (3_600, "hour"), |
|
| 212 | + | (60, "minute"), |
|
| 213 | + | ]; |
|
| 214 | + | let elapsed = now.saturating_sub(timestamp); |
|
| 215 | + | for (seconds, unit) in UNITS { |
|
| 216 | + | match elapsed / seconds { |
|
| 217 | + | 0 => continue, |
|
| 218 | + | 1 => return format!("1 {unit} ago"), |
|
| 219 | + | count => return format!("{count} {unit}s ago"), |
|
| 145 | 220 | } |
|
| 146 | 221 | } |
|
| 147 | - | lines |
|
| 222 | + | "just now".to_owned() |
|
| 223 | + | } |
|
| 224 | + | ||
| 225 | + | /// Returns the current Unix timestamp. |
|
| 226 | + | fn current_timestamp() -> u64 { |
|
| 227 | + | SystemTime::now() |
|
| 228 | + | .duration_since(UNIX_EPOCH) |
|
| 229 | + | .unwrap_or_default() |
|
| 230 | + | .as_secs() |
|
| 148 | 231 | } |
|
| 149 | 232 | ||
| 150 | 233 | #[cfg(test)] |
|
| 151 | 234 | mod tests { |
|
| 152 | 235 | use super::*; |
| 169 | 252 | assert_eq!( |
|
| 170 | 253 | Status::areas(Rect::new(0, 0, 80, 1)), |
|
| 171 | 254 | (Rect::new(0, 0, 80, 0), Rect::new(0, 0, 80, 1)) |
|
| 172 | 255 | ); |
|
| 173 | 256 | } |
|
| 257 | + | ||
| 258 | + | #[test] |
|
| 259 | + | fn comment_card_lines_render_a_bordered_comment_card() { |
|
| 260 | + | let comment = Comment { |
|
| 261 | + | id: 1.into(), |
|
| 262 | + | author: "alice".to_owned(), |
|
| 263 | + | timestamp: current_timestamp(), |
|
| 264 | + | body: "first\nsecond".to_owned(), |
|
| 265 | + | }; |
|
| 266 | + | ||
| 267 | + | let lines = comment_card_lines(&comment, true, 40); |
|
| 268 | + | let text = lines |
|
| 269 | + | .iter() |
|
| 270 | + | .map(ToString::to_string) |
|
| 271 | + | .collect::<Vec<_>>() |
|
| 272 | + | .join("\n"); |
|
| 273 | + | ||
| 274 | + | assert_eq!(lines.len(), COMMENT_CARD_ROWS); |
|
| 275 | + | assert!(text.contains("alice"), "text: {text:?}"); |
|
| 276 | + | assert!(text.contains("just now"), "text: {text:?}"); |
|
| 277 | + | assert!(text.contains("first second"), "text: {text:?}"); |
|
| 278 | + | assert!(!text.contains('\u{1F5E8}'), "text: {text:?}"); |
|
| 279 | + | } |
|
| 280 | + | ||
| 281 | + | #[test] |
|
| 282 | + | fn unselected_comment_cards_render_a_faint_border() { |
|
| 283 | + | let comment = Comment { |
|
| 284 | + | id: 1.into(), |
|
| 285 | + | author: "alice".to_owned(), |
|
| 286 | + | timestamp: current_timestamp(), |
|
| 287 | + | body: "first".to_owned(), |
|
| 288 | + | }; |
|
| 289 | + | ||
| 290 | + | let mut lines = comment_card_lines(&comment, false, 40); |
|
| 291 | + | let mut buffer = Buffer::empty(Rect::new(0, 0, 40, lines.len())); |
|
| 292 | + | ||
| 293 | + | Line::renders(lines.drain(..), buffer.area(), &mut buffer); |
|
| 294 | + | ||
| 295 | + | assert_eq!(buffer.get(0, 0).unwrap().style().fg_color(), colors::FAINT); |
|
| 296 | + | } |
|
| 297 | + | ||
| 298 | + | #[test] |
|
| 299 | + | fn relative_time_uses_largest_whole_unit() { |
|
| 300 | + | let now = 2_000_000_000; |
|
| 301 | + | ||
| 302 | + | assert_eq!(relative_time(now, now), "just now"); |
|
| 303 | + | assert_eq!(relative_time(now - 60, now), "1 minute ago"); |
|
| 304 | + | assert_eq!(relative_time(now - 3_600, now), "1 hour ago"); |
|
| 305 | + | assert_eq!(relative_time(now - 86_400, now), "1 day ago"); |
|
| 306 | + | assert_eq!(relative_time(now - 2_592_000, now), "1 month ago"); |
|
| 307 | + | assert_eq!(relative_time(now - 31_536_000, now), "1 year ago"); |
|
| 308 | + | } |
|
| 174 | 309 | } |