.cargo/
crates/
brain/
src/
session/
commit.rs
9.3 KiB
event.rs
6.2 KiB
hunk.rs
15.5 KiB
status.rs
31.7 KiB
worker.rs
8.6 KiB
backend.rs
104.7 KiB
comments.rs
22.1 KiB
decorate.rs
6.7 KiB
error.rs
2.6 KiB
main.rs
29.2 KiB
review.rs
2.3 KiB
session.rs
74.7 KiB
Cargo.toml
552 B
diff/
files/
highlight/
repo/
signals/
tui/
tui-core/
tui-termion/
tui-widgets/
scripts/
src/
.gitignore
30 B
.gitsigners
198 B
AGENTS.md
1.9 KiB
CONTRIBUTING
1.7 KiB
Cargo.lock
5.4 KiB
Cargo.toml
1.9 KiB
LICENSE
1.1 KiB
README.md
245 B
STYLE
3.7 KiB
clippy.toml
213 B
crates/brain/src/session/status.rs
raw
| 1 | //! Status and chrome rendering for the review session. |
| 2 | |
| 3 | use std::cmp::Ordering; |
| 4 | use std::time::{SystemTime, UNIX_EPOCH}; |
| 5 | |
| 6 | use rustik_diff::{ |
| 7 | DiffPalette, ReviewProgress, diffstat, file_count, progress_bar, recoverable_error, |
| 8 | }; |
| 9 | use rustik_repo::DiffStat; |
| 10 | use rustik_tui::{ |
| 11 | Border, Buffer, Color, Constraint, HStack, Label, Line, Rect, Size, Style, Table, TableOptions, |
| 12 | Widget, colors, |
| 13 | }; |
| 14 | |
| 15 | use crate::backend::CommitReviewState; |
| 16 | use crate::comments::{COMMENT_MARK, Comment}; |
| 17 | use crate::review::{FileVisibility, HunkVisibility, Visibility}; |
| 18 | |
| 19 | use super::commit::CommitSlot; |
| 20 | |
| 21 | /// Number of rows used by the status bar. |
| 22 | const STATUS_ROWS: usize = 1; |
| 23 | /// Number of rows used by the action bar. |
| 24 | const ACTION_ROWS: usize = 1; |
| 25 | /// Number of rows reserved for the bottom footer when possible. |
| 26 | const CHROME_ROWS: usize = STATUS_ROWS + ACTION_ROWS; |
| 27 | /// Empty columns between status content and the status bar's right edge. |
| 28 | const STATUS_RIGHT_PADDING: usize = 1; |
| 29 | /// Maximum width of a history diffstat graph. |
| 30 | pub(super) const HISTORY_DIFFSTAT_MAX_WIDTH: usize = 32; |
| 31 | /// Columns reserved for the total hunk count in commit history. |
| 32 | const HISTORY_HUNK_COUNT_WIDTH: usize = 4; |
| 33 | /// Foreground color for dim footer text. |
| 34 | const FOOTER_FG: Color = Color::Fixed(240); |
| 35 | /// Foreground color for prominent history values. |
| 36 | const HISTORY_VALUE_FG: Color = Color::Fixed(246); |
| 37 | /// Background color for the footer row, matching file border chrome. |
| 38 | const FOOTER_BG: Color = colors::FAINT; |
| 39 | /// Background color for the brain application badge. |
| 40 | pub(crate) const BRAIN_BADGE_BG: Color = Color::RGB(124, 36, 32); |
| 41 | /// Background color for the active-mode badge. |
| 42 | const MODE_BADGE_BG: Color = Color::RGB(68, 68, 68); |
| 43 | /// Foreground color for the current commit hash. |
| 44 | const FOOTER_HASH_FG: Color = Color::Fixed(250); |
| 45 | /// Foreground color for neutral footer values. |
| 46 | const FOOTER_VALUE_FG: Color = FOOTER_HASH_FG; |
| 47 | /// Foreground color for the current commit summary. |
| 48 | const FOOTER_SUMMARY_FG: Color = Color::Fixed(153); |
| 49 | /// Border color for the selected comment in the hunk view. |
| 50 | const SELECTED_COMMENT_BORDER: Color = Color::Fixed(239); |
| 51 | /// Background color for the selected commit, matching the active file row. |
| 52 | pub(super) const SELECTED_COMMIT_BG: Color = Color::Fixed(235); |
| 53 | /// Marker shown beside a commit whose hunks have all been accepted: ✔. |
| 54 | const REVIEWED_MARK: char = '✔'; |
| 55 | /// Marker shown beside a rejected commit: ✗. |
| 56 | const REJECTED_MARK: char = '✗'; |
| 57 | /// Marker shown beside a dropped commit. |
| 58 | const DROPPED_MARK: char = '-'; |
| 59 | /// Marker shown beside a commit that has not been reviewed. |
| 60 | const PENDING_MARK: char = '○'; |
| 61 | /// Marker shown beside a commit whose review is in progress. |
| 62 | const IN_PROGRESS_MARK: char = '◐'; |
| 63 | /// Marker highlighted when all hunks are visible in the footer: ✔. |
| 64 | const ALL_HUNKS_MODE_ICON: &str = "✔"; |
| 65 | /// Marker shown when full-file mode is visible in the footer: ↕. |
| 66 | const FULL_FILE_MODE_ICON: &str = "↕"; |
| 67 | /// Separator shown between footer status segments: ·. |
| 68 | const FOOTER_SEPARATOR: &str = " · "; |
| 69 | /// Marker used when footer text truncates: …. |
| 70 | const FOOTER_ELLIPSIS: &str = "…"; |
| 71 | /// Frames used by the repository-operation status spinner. |
| 72 | const SPINNER_FRAMES: &[char] = &['|', '/', '-', '\\']; |
| 73 | /// Separator shown between plain action hints. |
| 74 | const ACTION_SEPARATOR: &str = " · "; |
| 75 | /// Key label shown for Tab: ⭾. |
| 76 | const TAB_KEY: &str = "⭾"; |
| 77 | /// Key label shown for Return: ⮐. |
| 78 | const RETURN_KEY: &str = "⮐"; |
| 79 | /// Key label shown for Ctrl-Return. |
| 80 | const CTRL_RETURN_KEY: &str = "ctrl-⮐"; |
| 81 | /// Foreground color for contextual action labels. |
| 82 | const ACTION_LABEL_FG: Color = Color::Fixed(244); |
| 83 | /// Foreground color for contextual action keys. |
| 84 | const ACTION_KEY_FG: Color = Color::Fixed(250); |
| 85 | /// Foreground color for active context-expansion mode. |
| 86 | const FULL_FILE_MODE_FG: Color = colors::rgb::TEAL; |
| 87 | /// Action hints shown in commit-list mode. |
| 88 | const COMMITS_ACTIONS: &[(&str, &str)] = &[ |
| 89 | (TAB_KEY, "commit"), |
| 90 | ("j/k", "select"), |
| 91 | (RETURN_KEY, "open"), |
| 92 | ("r", "reject commit"), |
| 93 | ("d", "drop commit"), |
| 94 | ("q", "quit"), |
| 95 | ]; |
| 96 | /// Action hints shown in commit diff mode. |
| 97 | const COMMIT_ACTIONS: &[(&str, &str)] = &[ |
| 98 | (TAB_KEY, "history"), |
| 99 | ("j/k", "hunk"), |
| 100 | ("n/p", "file"), |
| 101 | ("a", "accept hunk"), |
| 102 | ("r", "reject commit"), |
| 103 | ("d", "drop commit"), |
| 104 | ("u", "undo"), |
| 105 | ("c", "comment"), |
| 106 | ]; |
| 107 | /// Action hints shown while browsing hunk comments. |
| 108 | const HUNK_BROWSE_ACTIONS: &[(&str, &str)] = &[ |
| 109 | (TAB_KEY, "history"), |
| 110 | ("r", "reject commit"), |
| 111 | ("c", "new"), |
| 112 | ("e", "edit"), |
| 113 | ("d", "delete"), |
| 114 | ("n/p", "select"), |
| 115 | ("esc", "commit"), |
| 116 | ]; |
| 117 | /// Action hints shown while editing a hunk comment. |
| 118 | const HUNK_EDIT_ACTIONS: &[(&str, &str)] = &[ |
| 119 | (RETURN_KEY, "newline"), |
| 120 | (CTRL_RETURN_KEY, "save"), |
| 121 | ("esc", "cancel"), |
| 122 | (TAB_KEY, "history"), |
| 123 | ]; |
| 124 | /// Rendered rows in one stored comment card. |
| 125 | pub(super) const COMMENT_CARD_ROWS: usize = 4; |
| 126 | /// Rows of card chrome around a comment body: the top border, the header row, |
| 127 | /// and the bottom border. |
| 128 | pub(super) const COMMENT_CARD_CHROME_ROWS: usize = 3; |
| 129 | /// Columns of card chrome on each side of a comment body: the border and its |
| 130 | /// padding. |
| 131 | pub(super) const COMMENT_CARD_CHROME_COLS: usize = 2; |
| 132 | |
| 133 | /// Context shown on the left side of the footer status. |
| 134 | #[derive(Debug)] |
| 135 | pub(super) enum StatusContext { |
| 136 | /// Selected commit identity and summary. |
| 137 | Commit { |
| 138 | /// Abbreviated commit hash. |
| 139 | short: String, |
| 140 | /// First line of the commit message. |
| 141 | summary: String, |
| 142 | }, |
| 143 | /// Reviewed revision range. |
| 144 | Range(String), |
| 145 | } |
| 146 | |
| 147 | /// Footer status describing the current review context and progress. |
| 148 | #[derive(Debug)] |
| 149 | pub(super) struct Status { |
| 150 | /// Active review mode name. |
| 151 | pub(super) mode: &'static str, |
| 152 | /// Review context displayed for the active mode. |
| 153 | pub(super) context: StatusContext, |
| 154 | /// Accepted-line progress for the active review context. |
| 155 | pub(super) progress: ReviewProgress, |
| 156 | /// Aggregated diff size for the active review context. |
| 157 | pub(super) stats: DiffStat, |
| 158 | /// Diff palette used for stat and review colors. |
| 159 | pub(super) palette: DiffPalette, |
| 160 | /// Visibility modes for the active review view. |
| 161 | pub(super) visibility: Visibility, |
| 162 | /// Most recent recoverable session error. |
| 163 | pub(super) error: Option<String>, |
| 164 | /// Active repository operation and animation frame. |
| 165 | pub(super) loading: Option<(&'static str, usize)>, |
| 166 | } |
| 167 | |
| 168 | impl Widget for Status { |
| 169 | fn size(&self, parent: Constraint) -> Size { |
| 170 | Size::new(parent.max.cols, STATUS_ROWS).constrain(parent) |
| 171 | } |
| 172 | |
| 173 | fn render(&mut self, area: Rect, buf: &mut Buffer) { |
| 174 | if area.is_empty() { |
| 175 | return; |
| 176 | } |
| 177 | Self::background(area.cols).render(area, buf); |
| 178 | self.bar(area.cols).render(area, buf); |
| 179 | } |
| 180 | } |
| 181 | |
| 182 | impl Status { |
| 183 | /// Returns the full-width status row background. |
| 184 | fn background(cols: usize) -> Line { |
| 185 | Line::new(Label::space().bg(FOOTER_BG)).padded(cols) |
| 186 | } |
| 187 | |
| 188 | /// Returns the bottom status row content. |
| 189 | fn bar(&self, cols: usize) -> HStack<'_> { |
| 190 | if let Some(error) = &self.error { |
| 191 | return HStack::default() |
| 192 | .child(recoverable_error(error, FOOTER_VALUE_FG, FOOTER_BG).truncated(cols, "")); |
| 193 | } |
| 194 | if let Some((operation, frame)) = self.loading { |
| 195 | let spinner = SPINNER_FRAMES[frame % SPINNER_FRAMES.len()]; |
| 196 | return HStack::default().child( |
| 197 | Line::blank() |
| 198 | .item(Label::from_char(spinner).fg(FOOTER_VALUE_FG)) |
| 199 | .item(Label::space()) |
| 200 | .item(Label::new(operation).fg(FOOTER_VALUE_FG)) |
| 201 | .style(Style::default().bg(FOOTER_BG)) |
| 202 | .truncated(cols, FOOTER_ELLIPSIS), |
| 203 | ); |
| 204 | } |
| 205 | let right = self.right_status(); |
| 206 | let left_width = cols |
| 207 | .saturating_sub(right.width()) |
| 208 | .saturating_sub(STATUS_RIGHT_PADDING) |
| 209 | .saturating_sub(1); |
| 210 | |
| 211 | HStack::default() |
| 212 | .child(self.left_status().truncated(left_width, FOOTER_ELLIPSIS)) |
| 213 | .spacer() |
| 214 | .child(right) |
| 215 | .child(Line::new(Label::space().bg(FOOTER_BG)).padded(STATUS_RIGHT_PADDING)) |
| 216 | } |
| 217 | |
| 218 | /// Returns the left status section with application, mode, and review context. |
| 219 | fn left_status(&self) -> Line { |
| 220 | let context = match &self.context { |
| 221 | StatusContext::Commit { short, summary } => Line::blank() |
| 222 | .item(Label::new(short).fg(FOOTER_HASH_FG)) |
| 223 | .item(Label::space()) |
| 224 | .item(Label::new(summary).fg(FOOTER_SUMMARY_FG)), |
| 225 | StatusContext::Range(range) => Line::new(Label::new(range).fg(FOOTER_SUMMARY_FG)), |
| 226 | }; |
| 227 | Line::blank() |
| 228 | .item( |
| 229 | Label::new(" brain ") |
| 230 | .fg(Color::White) |
| 231 | .bg(BRAIN_BADGE_BG) |
| 232 | .style(Style::default().bold()), |
| 233 | ) |
| 234 | .item( |
| 235 | Label::new(&format!(" {} ", self.mode)) |
| 236 | .fg(Color::White) |
| 237 | .bg(MODE_BADGE_BG) |
| 238 | .style(Style::default().bold()), |
| 239 | ) |
| 240 | .item(Label::space()) |
| 241 | .extend(context) |
| 242 | .style(Style::default().bg(FOOTER_BG)) |
| 243 | } |
| 244 | |
| 245 | /// Returns the right status section with review state and diff statistics. |
| 246 | fn right_status(&self) -> Line { |
| 247 | let all_hunks_fg = match self.visibility.hunks { |
| 248 | HunkVisibility::Pending => FOOTER_FG, |
| 249 | HunkVisibility::All => self.palette.positive_fg, |
| 250 | }; |
| 251 | let full_file_fg = match self.visibility.file { |
| 252 | FileVisibility::Context => FOOTER_FG, |
| 253 | FileVisibility::Full => FULL_FILE_MODE_FG, |
| 254 | }; |
| 255 | let mut status = Line::blank().extend(progress_bar(self.progress, self.palette, FOOTER_FG)); |
| 256 | if !matches!(self.context, StatusContext::Commit { .. }) || !self.progress.is_complete() { |
| 257 | status = status |
| 258 | .item(Label::space()) |
| 259 | .extend(diffstat(self.stats.into(), self.palette)) |
| 260 | .item(Label::new(FOOTER_SEPARATOR).fg(FOOTER_FG)) |
| 261 | .item(Label::new(&file_count(self.stats.files_changed)).fg(FOOTER_VALUE_FG)); |
| 262 | } |
| 263 | |
| 264 | status |
| 265 | .item(Label::new(FOOTER_SEPARATOR).fg(FOOTER_FG)) |
| 266 | .item(Label::new(ALL_HUNKS_MODE_ICON).fg(all_hunks_fg)) |
| 267 | .item(Label::space()) |
| 268 | .item(Label::new(FULL_FILE_MODE_ICON).fg(full_file_fg)) |
| 269 | .style(Style::default().bg(FOOTER_BG)) |
| 270 | } |
| 271 | } |
| 272 | |
| 273 | /// Context that determines which action hints are shown. |
| 274 | #[derive(Clone, Copy, Debug, Eq, PartialEq)] |
| 275 | pub(super) enum ActionContext { |
| 276 | /// Commit-list mode. |
| 277 | Commits, |
| 278 | /// Commit diff mode. |
| 279 | Commit, |
| 280 | /// Hunk comments are being browsed. |
| 281 | HunkBrowse, |
| 282 | /// A hunk comment is being edited. |
| 283 | HunkEdit, |
| 284 | } |
| 285 | |
| 286 | impl ActionContext { |
| 287 | /// Returns the action hints for this context. |
| 288 | fn actions(self) -> &'static [(&'static str, &'static str)] { |
| 289 | match self { |
| 290 | Self::Commits => COMMITS_ACTIONS, |
| 291 | Self::Commit => COMMIT_ACTIONS, |
| 292 | Self::HunkBrowse => HUNK_BROWSE_ACTIONS, |
| 293 | Self::HunkEdit => HUNK_EDIT_ACTIONS, |
| 294 | } |
| 295 | } |
| 296 | } |
| 297 | |
| 298 | /// Bottom chrome containing the status bar and contextual action bar. |
| 299 | #[derive(Debug)] |
| 300 | pub(super) struct Chrome { |
| 301 | /// Footer status for the active review context. |
| 302 | pub(super) status: Status, |
| 303 | /// Active action context. |
| 304 | pub(super) actions: ActionContext, |
| 305 | } |
| 306 | |
| 307 | impl Chrome { |
| 308 | /// Returns the content and bottom chrome areas for a full session frame. |
| 309 | pub(super) fn areas(area: Rect) -> (Rect, Rect) { |
| 310 | area.split_bottom(CHROME_ROWS) |
| 311 | } |
| 312 | |
| 313 | /// Returns the status and action areas inside the chrome area. |
| 314 | fn rows(area: Rect) -> (Rect, Rect) { |
| 315 | area.split_top(STATUS_ROWS) |
| 316 | } |
| 317 | |
| 318 | /// Returns the rendered action row. |
| 319 | fn action_line(&self, cols: usize) -> Line { |
| 320 | let mut line = Line::blank(); |
| 321 | let mut first = true; |
| 322 | for (key, label) in self.actions.actions() { |
| 323 | if !first { |
| 324 | line = line.item(Label::new(ACTION_SEPARATOR).fg(FOOTER_FG)); |
| 325 | } |
| 326 | first = false; |
| 327 | line = line |
| 328 | .item( |
| 329 | Label::new(key) |
| 330 | .fg(ACTION_KEY_FG) |
| 331 | .style(Style::default().bold()), |
| 332 | ) |
| 333 | .item(Label::space()) |
| 334 | .item(Label::new(label).fg(ACTION_LABEL_FG)); |
| 335 | } |
| 336 | line.truncated(cols.saturating_sub(2), "").inset(cols) |
| 337 | } |
| 338 | } |
| 339 | |
| 340 | impl Widget for Chrome { |
| 341 | fn size(&self, parent: Constraint) -> Size { |
| 342 | Size::new(parent.max.cols, CHROME_ROWS).constrain(parent) |
| 343 | } |
| 344 | |
| 345 | fn render(&mut self, area: Rect, buf: &mut Buffer) { |
| 346 | if area.is_empty() { |
| 347 | return; |
| 348 | } |
| 349 | let (status_area, action_area) = Self::rows(area); |
| 350 | self.status.render(status_area, buf); |
| 351 | if !action_area.is_empty() { |
| 352 | let mut line = if self.status.error.is_some() { |
| 353 | Status::background(action_area.cols) |
| 354 | } else { |
| 355 | self.action_line(action_area.cols) |
| 356 | }; |
| 357 | line.render(action_area, buf); |
| 358 | } |
| 359 | } |
| 360 | } |
| 361 | |
| 362 | /// Returns the viewer area above the footer chrome. |
| 363 | pub(super) fn viewer_area(area: Rect) -> Rect { |
| 364 | Chrome::areas(area).0 |
| 365 | } |
| 366 | |
| 367 | /// Returns the state and leading columns for one commit-table row. |
| 368 | pub(super) fn commit_row(slot: &CommitSlot, palette: DiffPalette) -> [Line; 3] { |
| 369 | let dropped = matches!(slot.state, CommitReviewState::Dropped); |
| 370 | let row_style = if dropped { |
| 371 | Style::default().dim() |
| 372 | } else { |
| 373 | Style::default() |
| 374 | }; |
| 375 | let state = match slot.state { |
| 376 | CommitReviewState::Accepted => Label::from_char(REVIEWED_MARK).fg(palette.positive_fg), |
| 377 | CommitReviewState::Rejected => Label::from_char(REJECTED_MARK).fg(palette.negative_fg), |
| 378 | CommitReviewState::Dropped => Label::from_char(DROPPED_MARK).fg(colors::fixed::MUTED), |
| 379 | CommitReviewState::Pending if slot.accepted_hunks > 0 => { |
| 380 | Label::from_char(IN_PROGRESS_MARK).fg(colors::rgb::ORANGE) |
| 381 | } |
| 382 | CommitReviewState::Pending => Label::from_char(PENDING_MARK).fg(colors::fixed::MUTED), |
| 383 | }; |
| 384 | [ |
| 385 | Line::new(state).style(row_style), |
| 386 | Line::new(Label::new(&slot.commit.id.short()).fg(colors::rgb::ORANGE)).style(row_style), |
| 387 | Line::new(slot.commit.summary.as_str()).style(if dropped { |
| 388 | Style::default().dim().strikethrough() |
| 389 | } else { |
| 390 | Style::default() |
| 391 | }), |
| 392 | ] |
| 393 | } |
| 394 | |
| 395 | /// Returns the right-side columns for one commit-table row. |
| 396 | /// |
| 397 | /// Columns are ordered by their wide-layout position: hunk progress, author, |
| 398 | /// age, branches, comment count, and diffstat. |
| 399 | pub(super) fn commit_row_suffix( |
| 400 | slot: &CommitSlot, |
| 401 | palette: DiffPalette, |
| 402 | now: u64, |
| 403 | largest_change_count: usize, |
| 404 | max_graph_width: usize, |
| 405 | ) -> [Line; 6] { |
| 406 | let stats = slot.remaining_stats(); |
| 407 | let diffstat = match slot.state { |
| 408 | CommitReviewState::Pending => { |
| 409 | history_diffstat(stats, largest_change_count, max_graph_width, palette) |
| 410 | } |
| 411 | CommitReviewState::Accepted | CommitReviewState::Rejected | CommitReviewState::Dropped => { |
| 412 | Line::blank() |
| 413 | } |
| 414 | }; |
| 415 | |
| 416 | let row_style = if matches!(slot.state, CommitReviewState::Dropped) { |
| 417 | Style::default().dim() |
| 418 | } else { |
| 419 | Style::default() |
| 420 | }; |
| 421 | let identity_style = if matches!(slot.state, CommitReviewState::Dropped) { |
| 422 | Style::default().dim().strikethrough() |
| 423 | } else { |
| 424 | Style::default() |
| 425 | }; |
| 426 | let percent = slot |
| 427 | .accepted_hunks |
| 428 | .saturating_mul(100) |
| 429 | .checked_div(slot.total_hunks) |
| 430 | .unwrap_or(0); |
| 431 | let hunk_color = match (slot.state, percent) { |
| 432 | (CommitReviewState::Rejected, _) => palette.negative_fg, |
| 433 | (_, 0) => colors::fixed::MUTED, |
| 434 | (_, 100) => palette.positive_fg, |
| 435 | _ => colors::rgb::ORANGE, |
| 436 | }; |
| 437 | let hunks = Line::new( |
| 438 | Label::new(&format!( |
| 439 | "{percent}%/{:<HISTORY_HUNK_COUNT_WIDTH$}", |
| 440 | slot.total_hunks |
| 441 | )) |
| 442 | .fg(hunk_color), |
| 443 | ); |
| 444 | let comments = slot |
| 445 | .loaded |
| 446 | .as_ref() |
| 447 | .map_or(slot.comment_count, |loaded| loaded.comments.len()); |
| 448 | [ |
| 449 | hunks, |
| 450 | Line::new(Label::new(slot.commit.author.as_str()).fg(HISTORY_VALUE_FG)) |
| 451 | .style(identity_style), |
| 452 | Line::new( |
| 453 | Label::from(relative_time( |
| 454 | slot.commit.recency.seconds.into(), |
| 455 | now.into(), |
| 456 | )) |
| 457 | .fg(colors::fixed::MUTED), |
| 458 | ) |
| 459 | .style(identity_style), |
| 460 | Line::new(Label::new(&slot.commit.branches.join(", ")).fg(colors::rgb::TEAL)) |
| 461 | .style(row_style), |
| 462 | Line::new(Label::new(&format!("{COMMENT_MARK} {comments:>2}")).fg(colors::fixed::MUTED)) |
| 463 | .style(row_style), |
| 464 | diffstat.style(row_style), |
| 465 | ] |
| 466 | } |
| 467 | |
| 468 | /// Returns a change count and proportional addition and deletion graph. |
| 469 | fn history_diffstat( |
| 470 | stats: DiffStat, |
| 471 | largest_change_count: usize, |
| 472 | max_graph_width: usize, |
| 473 | palette: DiffPalette, |
| 474 | ) -> Line { |
| 475 | let change_count = stats.insertions + stats.deletions; |
| 476 | let change_kinds = usize::from(stats.insertions > 0) + usize::from(stats.deletions > 0); |
| 477 | let graph_width = if largest_change_count == 0 { |
| 478 | 0 |
| 479 | } else { |
| 480 | change_count |
| 481 | .saturating_mul(max_graph_width) |
| 482 | .div_ceil(largest_change_count) |
| 483 | .clamp(change_kinds, max_graph_width) |
| 484 | }; |
| 485 | let mut addition_width = if change_count == 0 { |
| 486 | 0 |
| 487 | } else { |
| 488 | stats |
| 489 | .insertions |
| 490 | .saturating_mul(graph_width) |
| 491 | .div_ceil(change_count) |
| 492 | .min(graph_width) |
| 493 | }; |
| 494 | if stats.deletions > 0 { |
| 495 | addition_width = addition_width.min(graph_width - 1); |
| 496 | } |
| 497 | let deletion_width = graph_width - addition_width; |
| 498 | let count_width = largest_change_count.max(1).ilog10() as usize + 1; |
| 499 | |
| 500 | Line::new(Label::new(&format!("{change_count:>count_width$}")).fg(HISTORY_VALUE_FG)) |
| 501 | .item(Label::space()) |
| 502 | .item(Label::new(&"+".repeat(addition_width)).fg(palette.positive_fg)) |
| 503 | .item(Label::new(&"-".repeat(deletion_width)).fg(palette.negative_fg)) |
| 504 | } |
| 505 | |
| 506 | /// Renders one stored comment as a bordered card. |
| 507 | pub(super) fn comment_card_lines(comment: &Comment, selected: bool, cols: usize) -> Vec<Line> { |
| 508 | let border = if selected { |
| 509 | SELECTED_COMMENT_BORDER |
| 510 | } else { |
| 511 | colors::FAINT |
| 512 | }; |
| 513 | let body = Line::new(Label::new(&comment.body.replace('\n', " ")).fg(Color::White)); |
| 514 | let header = comment_header( |
| 515 | &comment.author, |
| 516 | &comment_age(comment), |
| 517 | comment_table_content_width(cols).max(body.width()), |
| 518 | ); |
| 519 | let mut table = Table::<1, Line>::new(TableOptions { |
| 520 | border: Some(Border::new(border)), |
| 521 | ..TableOptions::bordered() |
| 522 | }); |
| 523 | table.header([header]); |
| 524 | table.push([body]); |
| 525 | table.lines(Constraint::tight(cols)) |
| 526 | } |
| 527 | |
| 528 | /// Renders an editable comment card with a blank body of `rows` lines. |
| 529 | pub(super) fn comment_editor_lines(author: &str, age: &str, rows: usize, cols: usize) -> Vec<Line> { |
| 530 | let body = Line::blank().padded(comment_table_content_width(cols)); |
| 531 | let mut table = Table::<1, Line>::new(TableOptions { |
| 532 | border: Some(Border::new(SELECTED_COMMENT_BORDER)), |
| 533 | ..TableOptions::bordered() |
| 534 | }); |
| 535 | table.header([comment_header( |
| 536 | author, |
| 537 | age, |
| 538 | comment_table_content_width(cols), |
| 539 | )]); |
| 540 | for _ in 0..rows.max(1) { |
| 541 | table.push([body.clone()]); |
| 542 | } |
| 543 | table.lines(Constraint::tight(cols)) |
| 544 | } |
| 545 | |
| 546 | /// Returns a comment's age as a human relative timestamp. |
| 547 | pub(super) fn comment_age(comment: &Comment) -> String { |
| 548 | relative_time(comment.timestamp.into(), current_timestamp().into()) |
| 549 | } |
| 550 | |
| 551 | /// Returns the comment-card content width: the rendered width minus the |
| 552 | /// border and padding columns on both sides. |
| 553 | fn comment_table_content_width(cols: usize) -> usize { |
| 554 | cols.saturating_sub(2 * COMMENT_CARD_CHROME_COLS) |
| 555 | } |
| 556 | |
| 557 | /// Returns a comment-table header from displayable fields. |
| 558 | fn comment_header(author: &str, timestamp: &str, body_width: usize) -> Line { |
| 559 | Line::spaced([ |
| 560 | Label::new(author).style(Style::default().fg(Color::White).bold()), |
| 561 | Label::new(timestamp).fg(colors::fixed::DIM), |
| 562 | ]) |
| 563 | .padded(body_width) |
| 564 | } |
| 565 | |
| 566 | /// Returns a human relative timestamp in the largest whole unit. |
| 567 | fn relative_time(timestamp: i128, now: i128) -> String { |
| 568 | /// Time units from largest to smallest, as seconds and unit name. |
| 569 | const UNITS: [(u128, &str); 5] = [ |
| 570 | (31_536_000, "year"), |
| 571 | (2_592_000, "month"), |
| 572 | (86_400, "day"), |
| 573 | (3_600, "hour"), |
| 574 | (60, "minute"), |
| 575 | ]; |
| 576 | let direction = timestamp.cmp(&now); |
| 577 | let elapsed = timestamp.abs_diff(now); |
| 578 | |
| 579 | for (seconds, unit) in UNITS { |
| 580 | match (direction, elapsed / seconds) { |
| 581 | (_, 0) => continue, |
| 582 | (Ordering::Greater, 1) => return format!("in 1 {unit}"), |
| 583 | (Ordering::Greater, count) => return format!("in {count} {unit}s"), |
| 584 | (_, 1) => return format!("1 {unit} ago"), |
| 585 | (_, count) => return format!("{count} {unit}s ago"), |
| 586 | } |
| 587 | } |
| 588 | if direction == Ordering::Greater { |
| 589 | "in less than a minute".to_owned() |
| 590 | } else { |
| 591 | "just now".to_owned() |
| 592 | } |
| 593 | } |
| 594 | |
| 595 | /// Returns the current Unix timestamp. |
| 596 | pub(super) fn current_timestamp() -> u64 { |
| 597 | SystemTime::now() |
| 598 | .duration_since(UNIX_EPOCH) |
| 599 | .unwrap_or_default() |
| 600 | .as_secs() |
| 601 | } |
| 602 | |
| 603 | #[cfg(test)] |
| 604 | mod tests { |
| 605 | use super::*; |
| 606 | |
| 607 | use crate::backend::Commit; |
| 608 | use rustik_repo::CommitTime; |
| 609 | |
| 610 | #[test] |
| 611 | fn viewer_area_reserves_footer() { |
| 612 | assert_eq!( |
| 613 | viewer_area(Rect::new(0, 0, 80, 20)), |
| 614 | Rect::new(0, 0, 80, 18) |
| 615 | ); |
| 616 | assert_eq!(viewer_area(Rect::new(0, 0, 80, 1)), Rect::new(0, 0, 80, 0)); |
| 617 | } |
| 618 | |
| 619 | #[test] |
| 620 | fn chrome_areas_reserve_footer() { |
| 621 | assert_eq!( |
| 622 | Chrome::areas(Rect::new(0, 0, 80, 20)), |
| 623 | (Rect::new(0, 0, 80, 18), Rect::new(0, 18, 80, 2)) |
| 624 | ); |
| 625 | assert_eq!( |
| 626 | Chrome::areas(Rect::new(0, 0, 80, 1)), |
| 627 | (Rect::new(0, 0, 80, 0), Rect::new(0, 0, 80, 1)) |
| 628 | ); |
| 629 | } |
| 630 | |
| 631 | #[test] |
| 632 | fn action_context_changes_action_hints() { |
| 633 | assert!( |
| 634 | ActionContext::Commit |
| 635 | .actions() |
| 636 | .iter() |
| 637 | .any(|(_, label)| *label == "accept hunk") |
| 638 | ); |
| 639 | assert!( |
| 640 | ActionContext::Commits |
| 641 | .actions() |
| 642 | .iter() |
| 643 | .any(|(_, label)| *label == "reject commit") |
| 644 | ); |
| 645 | assert!( |
| 646 | ActionContext::HunkBrowse |
| 647 | .actions() |
| 648 | .iter() |
| 649 | .any(|(_, label)| *label == "reject commit") |
| 650 | ); |
| 651 | assert!( |
| 652 | ActionContext::HunkEdit |
| 653 | .actions() |
| 654 | .iter() |
| 655 | .any(|(_, label)| *label == "save") |
| 656 | ); |
| 657 | } |
| 658 | |
| 659 | #[test] |
| 660 | fn chrome_error_replaces_status() { |
| 661 | let mut chrome = Chrome { |
| 662 | status: Status { |
| 663 | mode: "commit", |
| 664 | context: StatusContext::Commit { |
| 665 | short: "1234567".to_owned(), |
| 666 | summary: "summary".to_owned(), |
| 667 | }, |
| 668 | progress: ReviewProgress::default(), |
| 669 | stats: DiffStat::default(), |
| 670 | palette: DiffPalette::default(), |
| 671 | visibility: Visibility::default(), |
| 672 | error: Some("patch does not apply".to_owned()), |
| 673 | loading: None, |
| 674 | }, |
| 675 | actions: ActionContext::Commit, |
| 676 | }; |
| 677 | let mut buffer = Buffer::empty(Rect::new(0, 0, 40, 2)); |
| 678 | |
| 679 | chrome.render(buffer.area(), &mut buffer); |
| 680 | |
| 681 | let status = buffer.line(0); |
| 682 | assert!(status.contains("patch does not apply")); |
| 683 | assert!(!status.contains("brain")); |
| 684 | } |
| 685 | |
| 686 | #[test] |
| 687 | fn completed_commit_status_hides_diff_summary() { |
| 688 | let mut status = Status { |
| 689 | mode: "commit", |
| 690 | context: StatusContext::Commit { |
| 691 | short: "1234567".to_owned(), |
| 692 | summary: "summary".to_owned(), |
| 693 | }, |
| 694 | progress: ReviewProgress { |
| 695 | accepted: 5, |
| 696 | total: 5, |
| 697 | }, |
| 698 | stats: DiffStat::default(), |
| 699 | palette: DiffPalette::default(), |
| 700 | visibility: Visibility::default(), |
| 701 | error: None, |
| 702 | loading: None, |
| 703 | }; |
| 704 | let mut buffer = Buffer::empty(Rect::new(0, 0, 80, 1)); |
| 705 | |
| 706 | status.render(buffer.area(), &mut buffer); |
| 707 | let rendered = (0..buffer.area().cols) |
| 708 | .map(|x| buffer.get(x, 0).unwrap().glyph()) |
| 709 | .collect::<String>(); |
| 710 | |
| 711 | assert!(rendered.ends_with("━━━━━━━━ · ✔ ↕ ")); |
| 712 | } |
| 713 | |
| 714 | #[test] |
| 715 | fn completed_commit_rows_render_final_state() { |
| 716 | let now = 2_000_000_000; |
| 717 | let stats = DiffStat { |
| 718 | files_changed: 1, |
| 719 | insertions: 2, |
| 720 | deletions: 3, |
| 721 | }; |
| 722 | let mut slot = CommitSlot { |
| 723 | commit: Commit { |
| 724 | id: "0123456789abcdef0123456789abcdef01234567".parse().unwrap(), |
| 725 | summary: "Summary".to_owned(), |
| 726 | author: "Alice".to_owned(), |
| 727 | branches: vec!["main".to_owned()], |
| 728 | recency: CommitTime { |
| 729 | seconds: now - 3_600, |
| 730 | }, |
| 731 | stats, |
| 732 | }, |
| 733 | progress: ReviewProgress { |
| 734 | accepted: 0, |
| 735 | total: 1, |
| 736 | }, |
| 737 | accepted_hunks: 0, |
| 738 | total_hunks: 1, |
| 739 | comment_count: 3, |
| 740 | state: CommitReviewState::Pending, |
| 741 | remaining_stats: stats, |
| 742 | document: None, |
| 743 | identities: None, |
| 744 | loaded: None, |
| 745 | }; |
| 746 | |
| 747 | for (state, marker) in [ |
| 748 | (CommitReviewState::Accepted, REVIEWED_MARK), |
| 749 | (CommitReviewState::Rejected, REJECTED_MARK), |
| 750 | ] { |
| 751 | slot.state = state; |
| 752 | assert_eq!( |
| 753 | commit_row(&slot, DiffPalette::default())[0].to_string(), |
| 754 | marker.to_string() |
| 755 | ); |
| 756 | assert!( |
| 757 | commit_row_suffix( |
| 758 | &slot, |
| 759 | DiffPalette::default(), |
| 760 | now as u64, |
| 761 | stats.insertions + stats.deletions, |
| 762 | 8, |
| 763 | )[5] |
| 764 | .to_string() |
| 765 | .is_empty() |
| 766 | ); |
| 767 | } |
| 768 | |
| 769 | slot.state = CommitReviewState::Dropped; |
| 770 | let [hunks, mut author, mut age, _, comments, diffstat] = commit_row_suffix( |
| 771 | &slot, |
| 772 | DiffPalette::default(), |
| 773 | now as u64, |
| 774 | stats.insertions + stats.deletions, |
| 775 | 8, |
| 776 | ); |
| 777 | assert_eq!( |
| 778 | commit_row(&slot, DiffPalette::default())[0].to_string(), |
| 779 | DROPPED_MARK.to_string() |
| 780 | ); |
| 781 | assert!(diffstat.to_string().is_empty()); |
| 782 | assert_eq!(hunks.to_string(), "0%/1 "); |
| 783 | assert_eq!(comments.to_string(), "🗩 3"); |
| 784 | |
| 785 | let mut author_buffer = Buffer::empty(Rect::new(0, 0, slot.commit.author.len(), 1)); |
| 786 | let mut age_buffer = Buffer::empty(Rect::new(0, 0, 16, 1)); |
| 787 | author.render(author_buffer.area(), &mut author_buffer); |
| 788 | age.render(age_buffer.area(), &mut age_buffer); |
| 789 | |
| 790 | assert_eq!( |
| 791 | author_buffer.get(0, 0).unwrap().style(), |
| 792 | Style::default().fg(HISTORY_VALUE_FG).dim().strikethrough() |
| 793 | ); |
| 794 | assert_eq!( |
| 795 | age_buffer.get(0, 0).unwrap().style(), |
| 796 | Style::default() |
| 797 | .fg(colors::fixed::MUTED) |
| 798 | .dim() |
| 799 | .strikethrough() |
| 800 | ); |
| 801 | |
| 802 | slot.state = CommitReviewState::Pending; |
| 803 | assert_eq!( |
| 804 | commit_row(&slot, DiffPalette::default())[0].to_string(), |
| 805 | PENDING_MARK.to_string() |
| 806 | ); |
| 807 | slot.accepted_hunks = 1; |
| 808 | assert_eq!( |
| 809 | commit_row(&slot, DiffPalette::default())[0].to_string(), |
| 810 | IN_PROGRESS_MARK.to_string() |
| 811 | ); |
| 812 | |
| 813 | for (state, accepted_hunks, expected_color) in [ |
| 814 | (CommitReviewState::Pending, 0, colors::fixed::MUTED), |
| 815 | (CommitReviewState::Pending, 1, colors::rgb::ORANGE), |
| 816 | ( |
| 817 | CommitReviewState::Pending, |
| 818 | 2, |
| 819 | DiffPalette::default().positive_fg, |
| 820 | ), |
| 821 | ( |
| 822 | CommitReviewState::Rejected, |
| 823 | 0, |
| 824 | DiffPalette::default().negative_fg, |
| 825 | ), |
| 826 | ] { |
| 827 | slot.state = state; |
| 828 | slot.accepted_hunks = accepted_hunks; |
| 829 | slot.total_hunks = 2; |
| 830 | let [mut hunks, _, _, _, _, _] = commit_row_suffix( |
| 831 | &slot, |
| 832 | DiffPalette::default(), |
| 833 | now as u64, |
| 834 | stats.insertions + stats.deletions, |
| 835 | 8, |
| 836 | ); |
| 837 | let mut buffer = Buffer::empty(Rect::new(0, 0, 16, 1)); |
| 838 | hunks.render(buffer.area(), &mut buffer); |
| 839 | |
| 840 | assert_eq!( |
| 841 | buffer.get(0, 0).unwrap().style(), |
| 842 | Style::default().fg(expected_color) |
| 843 | ); |
| 844 | } |
| 845 | } |
| 846 | |
| 847 | #[test] |
| 848 | fn history_diffstat_scales_against_largest_commit() { |
| 849 | let palette = DiffPalette::default(); |
| 850 | let largest = DiffStat { |
| 851 | files_changed: 1, |
| 852 | insertions: 6, |
| 853 | deletions: 4, |
| 854 | }; |
| 855 | let smaller = DiffStat { |
| 856 | files_changed: 1, |
| 857 | insertions: 2, |
| 858 | deletions: 3, |
| 859 | }; |
| 860 | let deletion = DiffStat { |
| 861 | files_changed: 1, |
| 862 | insertions: 999, |
| 863 | deletions: 1, |
| 864 | }; |
| 865 | let small_mixed = DiffStat { |
| 866 | files_changed: 1, |
| 867 | insertions: 1, |
| 868 | deletions: 1, |
| 869 | }; |
| 870 | |
| 871 | assert_eq!( |
| 872 | history_diffstat(largest, 10, 8, palette).to_string(), |
| 873 | "10 +++++---" |
| 874 | ); |
| 875 | assert_eq!( |
| 876 | history_diffstat(smaller, 10, 8, palette).to_string(), |
| 877 | " 5 ++--" |
| 878 | ); |
| 879 | assert_eq!( |
| 880 | history_diffstat(deletion, 1_000, 8, palette).to_string(), |
| 881 | "1000 +++++++-" |
| 882 | ); |
| 883 | assert_eq!( |
| 884 | history_diffstat(small_mixed, 1_000, 8, palette).to_string(), |
| 885 | " 2 +-" |
| 886 | ); |
| 887 | let wide = history_diffstat(largest, 10, 32, palette).to_string(); |
| 888 | let uncapped = history_diffstat(largest, 10, 64, palette).to_string(); |
| 889 | assert_eq!(wide.chars().count(), 35); |
| 890 | assert!(uncapped.chars().count() > wide.chars().count()); |
| 891 | } |
| 892 | |
| 893 | #[test] |
| 894 | fn comment_card_lines_render_a_bordered_comment_card() { |
| 895 | let comment = Comment { |
| 896 | id: 1.into(), |
| 897 | author: "alice".to_owned(), |
| 898 | timestamp: current_timestamp(), |
| 899 | body: "first\nsecond".to_owned(), |
| 900 | }; |
| 901 | |
| 902 | let lines = comment_card_lines(&comment, true, 40); |
| 903 | let text = lines |
| 904 | .iter() |
| 905 | .map(ToString::to_string) |
| 906 | .collect::<Vec<_>>() |
| 907 | .join("\n"); |
| 908 | |
| 909 | assert_eq!(lines.len(), COMMENT_CARD_ROWS); |
| 910 | assert!(text.contains("alice"), "text: {text:?}"); |
| 911 | assert!(text.contains("just now"), "text: {text:?}"); |
| 912 | assert!(text.contains("first second"), "text: {text:?}"); |
| 913 | assert!(!text.contains('🗨'), "text: {text:?}"); |
| 914 | } |
| 915 | |
| 916 | #[test] |
| 917 | fn relative_time_uses_largest_whole_unit() { |
| 918 | let now = 2_000_000_000; |
| 919 | |
| 920 | assert_eq!(relative_time(now, now), "just now"); |
| 921 | assert_eq!(relative_time(now - 60, now), "1 minute ago"); |
| 922 | assert_eq!(relative_time(now - 3_600, now), "1 hour ago"); |
| 923 | assert_eq!(relative_time(now - 86_400, now), "1 day ago"); |
| 924 | assert_eq!(relative_time(now - 2_592_000, now), "1 month ago"); |
| 925 | assert_eq!(relative_time(now - 31_536_000, now), "1 year ago"); |
| 926 | } |
| 927 | |
| 928 | #[test] |
| 929 | fn relative_time_preserves_future_direction() { |
| 930 | let now = 2_000_000_000; |
| 931 | |
| 932 | assert_eq!(relative_time(now + 1, now), "in less than a minute"); |
| 933 | assert_eq!(relative_time(now + 60, now), "in 1 minute"); |
| 934 | assert_eq!(relative_time(now + 7_200, now), "in 2 hours"); |
| 935 | } |
| 936 | } |