brain,diff: Highlight recoverable errors
d03a156f14b8a77f971233320cc4a27f392ac85d
Show recoverable failures without leaving stale footer actions behind. Keep diff input failures in the TUI until the next successful input. Assisted-by: Codex:gpt-5.6-sol
1 parent
732061dc
crates/brain/src/session/status.rs
+58 -22
| 163 | 163 | } |
|
| 164 | 164 | ||
| 165 | 165 | /// Returns the bottom status row content. |
|
| 166 | 166 | fn bar(&self, cols: usize) -> HStack<'_> { |
|
| 167 | 167 | if let Some(error) = &self.error { |
|
| 168 | + | let message = format!(" {error} "); |
|
| 168 | 169 | return HStack::default().child( |
|
| 169 | - | Line::new(Label::new(error).fg(self.palette.negative_fg)) |
|
| 170 | - | .truncated(cols, FOOTER_ELLIPSIS), |
|
| 170 | + | Line::blank() |
|
| 171 | + | .item( |
|
| 172 | + | Label::new(" error ") |
|
| 173 | + | .style(Style::default().fg(Color::White).bg(Color::Red).bold()), |
|
| 174 | + | ) |
|
| 175 | + | .item( |
|
| 176 | + | Label::new(&message) |
|
| 177 | + | .style(Style::default().fg(FOOTER_VALUE_FG).bg(FOOTER_BG)), |
|
| 178 | + | ) |
|
| 179 | + | .truncated(cols, ""), |
|
| 171 | 180 | ); |
|
| 172 | 181 | } |
|
| 173 | 182 | let right = self.right_status(); |
|
| 174 | 183 | let left_width = cols |
|
| 175 | 184 | .saturating_sub(right.width()) |
| 317 | 326 | return; |
|
| 318 | 327 | } |
|
| 319 | 328 | let (status_area, action_area) = Self::rows(area); |
|
| 320 | 329 | self.status.render(status_area, buf); |
|
| 321 | 330 | if !action_area.is_empty() { |
|
| 322 | - | self.action_line(action_area.cols).render(action_area, buf); |
|
| 331 | + | let mut line = if self.status.error.is_some() { |
|
| 332 | + | Status::background(action_area.cols) |
|
| 333 | + | } else { |
|
| 334 | + | self.action_line(action_area.cols) |
|
| 335 | + | }; |
|
| 336 | + | line.render(action_area, buf); |
|
| 323 | 337 | } |
|
| 324 | 338 | } |
|
| 325 | 339 | } |
|
| 326 | 340 | ||
| 327 | 341 | /// Formats a changed-file count with the correct singular or plural noun. |
| 577 | 591 | .any(|(_, label)| *label == "save") |
|
| 578 | 592 | ); |
|
| 579 | 593 | } |
|
| 580 | 594 | ||
| 581 | 595 | #[test] |
|
| 582 | - | fn status_renders_recoverable_error() { |
|
| 583 | - | let palette = DiffPalette::default(); |
|
| 584 | - | let mut status = Status { |
|
| 585 | - | mode: "commit", |
|
| 586 | - | context: StatusContext::Commit { |
|
| 587 | - | short: "1234567".to_owned(), |
|
| 588 | - | summary: "summary".to_owned(), |
|
| 596 | + | fn chrome_error_replaces_status_and_actions() { |
|
| 597 | + | let mut chrome = Chrome { |
|
| 598 | + | status: Status { |
|
| 599 | + | mode: "commit", |
|
| 600 | + | context: StatusContext::Commit { |
|
| 601 | + | short: "1234567".to_owned(), |
|
| 602 | + | summary: "summary".to_owned(), |
|
| 603 | + | }, |
|
| 604 | + | progress: ReviewProgress::default(), |
|
| 605 | + | stats: DiffStat::default(), |
|
| 606 | + | palette: DiffPalette::default(), |
|
| 607 | + | visibility: Visibility::default(), |
|
| 608 | + | error: Some("patch does not apply".to_owned()), |
|
| 589 | 609 | }, |
|
| 590 | - | progress: ReviewProgress::default(), |
|
| 591 | - | stats: DiffStat::default(), |
|
| 592 | - | palette, |
|
| 593 | - | visibility: Visibility::default(), |
|
| 594 | - | error: Some("patch does not apply".to_owned()), |
|
| 610 | + | actions: ActionContext::Commit, |
|
| 595 | 611 | }; |
|
| 596 | - | let mut buffer = Buffer::empty(Rect::new(0, 0, 40, 1)); |
|
| 612 | + | let mut buffer = Buffer::empty(Rect::new(0, 0, 40, 2)); |
|
| 597 | 613 | ||
| 598 | - | status.render(buffer.area(), &mut buffer); |
|
| 614 | + | chrome.render(buffer.area(), &mut buffer); |
|
| 599 | 615 | ||
| 600 | - | let text = (0..buffer.area().cols) |
|
| 616 | + | let status = (0..buffer.area().cols) |
|
| 601 | 617 | .filter_map(|x| buffer.get(x, 0)) |
|
| 602 | 618 | .map(|cell| cell.glyph()) |
|
| 603 | 619 | .collect::<String>(); |
|
| 604 | - | assert!(text.contains("patch does not apply")); |
|
| 605 | - | assert_eq!( |
|
| 606 | - | buffer.get(0, 0).unwrap().style().fg_color(), |
|
| 607 | - | palette.negative_fg |
|
| 620 | + | let actions = (0..buffer.area().cols) |
|
| 621 | + | .filter_map(|x| buffer.get(x, 1)) |
|
| 622 | + | .map(|cell| cell.glyph()) |
|
| 623 | + | .collect::<String>(); |
|
| 624 | + | let badge_style = Style::default().fg(Color::White).bg(Color::Red).bold(); |
|
| 625 | + | let message_style = Style::default().fg(FOOTER_VALUE_FG).bg(FOOTER_BG); |
|
| 626 | + | ||
| 627 | + | assert!(status.contains(" error patch does not apply")); |
|
| 628 | + | assert!(!status.contains("brain")); |
|
| 629 | + | assert!(actions.chars().all(|glyph| glyph == ' ')); |
|
| 630 | + | assert!((0..7).all(|x| buffer.get(x, 0).unwrap().style() == badge_style)); |
|
| 631 | + | assert_eq!(buffer.get(7, 0).unwrap().style(), message_style); |
|
| 632 | + | assert!( |
|
| 633 | + | (0..buffer.area().cols) |
|
| 634 | + | .all(|x| buffer.get(x, 1).unwrap().style().bg_color() == FOOTER_BG) |
|
| 608 | 635 | ); |
|
| 636 | + | ||
| 637 | + | let mut narrow = Buffer::empty(Rect::new(0, 0, 10, 2)); |
|
| 638 | + | chrome.render(narrow.area(), &mut narrow); |
|
| 639 | + | let status = (0..narrow.area().cols) |
|
| 640 | + | .filter_map(|x| narrow.get(x, 0)) |
|
| 641 | + | .map(|cell| cell.glyph()) |
|
| 642 | + | .collect::<String>(); |
|
| 643 | + | ||
| 644 | + | assert_eq!(status, " error pa"); |
|
| 609 | 645 | } |
|
| 610 | 646 | ||
| 611 | 647 | #[test] |
|
| 612 | 648 | fn status_renders_right_padding() { |
|
| 613 | 649 | let mut status = Status { |
crates/diff/src/bin/rustik-diff/session.rs
+80 -6
| 57 | 57 | highlighter, |
|
| 58 | 58 | context_pending: false, |
|
| 59 | 59 | debug, |
|
| 60 | 60 | debug_status: None, |
|
| 61 | 61 | visible_started: None, |
|
| 62 | + | error: None, |
|
| 62 | 63 | }; |
|
| 63 | 64 | session.redraw()?; |
|
| 64 | 65 | ||
| 65 | 66 | while let Ok(event) = events_rx.recv() { |
|
| 66 | 67 | let mut redraw = false; |
| 93 | 94 | debug: bool, |
|
| 94 | 95 | /// Latest highlight timing shown in the debug footer. |
|
| 95 | 96 | debug_status: Option<String>, |
|
| 96 | 97 | /// Time the in-flight visible highlight batch was requested, for debug. |
|
| 97 | 98 | visible_started: Option<Instant>, |
|
| 99 | + | /// Most recent recoverable input error shown in the status bar. |
|
| 100 | + | error: Option<String>, |
|
| 98 | 101 | } |
|
| 99 | 102 | ||
| 100 | 103 | impl TerminalSession<'_> { |
|
| 101 | 104 | /// Applies one event and records whether the session should redraw. |
|
| 102 | 105 | fn update(&mut self, event: TerminalEvent, redraw: &mut bool) -> Result<Control, Error> { |
|
| 103 | 106 | match event { |
|
| 104 | 107 | TerminalEvent::Input(event) => { |
|
| 105 | - | let event = event?; |
|
| 108 | + | let event = match event { |
|
| 109 | + | Ok(event) => { |
|
| 110 | + | if self.error.take().is_some() { |
|
| 111 | + | *redraw = true; |
|
| 112 | + | } |
|
| 113 | + | event |
|
| 114 | + | } |
|
| 115 | + | Err(error) => { |
|
| 116 | + | self.error = Some(error.to_string().replace('\n', " · ")); |
|
| 117 | + | *redraw = true; |
|
| 118 | + | return Ok(Control::Continue); |
|
| 119 | + | } |
|
| 120 | + | }; |
|
| 106 | 121 | let resized = matches!(event, Event::Resize(_)); |
|
| 107 | 122 | let area = viewer_area(self.terminal.area()); |
|
| 108 | 123 | if let Some(change) = context_change(&event) { |
|
| 109 | 124 | self.request_context(change); |
|
| 110 | 125 | return Ok(Control::Continue); |
| 195 | 210 | } |
|
| 196 | 211 | } |
|
| 197 | 212 | ||
| 198 | 213 | /// Draws one terminal frame without scheduling highlight work. |
|
| 199 | 214 | fn draw_frame(&mut self) -> io::Result<()> { |
|
| 215 | + | let error = self.error.as_deref(); |
|
| 200 | 216 | self.terminal.draw(|frame| { |
|
| 201 | 217 | let body = viewer_area(frame.area); |
|
| 202 | 218 | frame.render_widget(&mut *self.viewer, body); |
|
| 203 | 219 | draw_status( |
|
| 204 | 220 | frame, |
|
| 205 | 221 | self.viewer.document().stats(), |
|
| 206 | 222 | self.viewer.options().diff_palette, |
|
| 207 | 223 | self.debug_status.as_deref(), |
|
| 224 | + | error, |
|
| 208 | 225 | ); |
|
| 209 | 226 | }) |
|
| 210 | 227 | } |
|
| 211 | 228 | } |
|
| 212 | 229 |
| 267 | 284 | fn draw_status( |
|
| 268 | 285 | frame: &mut tui::Frame<'_>, |
|
| 269 | 286 | stats: Stats, |
|
| 270 | 287 | palette: DiffPalette, |
|
| 271 | 288 | debug: Option<&str>, |
|
| 289 | + | error: Option<&str>, |
|
| 272 | 290 | ) { |
|
| 273 | 291 | let area = frame.area; |
|
| 274 | 292 | if area.rows == 0 { |
|
| 275 | 293 | return; |
|
| 276 | 294 | } |
|
| 277 | 295 | let row = Rect::new(area.x, area.bottom().saturating_sub(1), area.cols, 1); |
|
| 278 | 296 | ||
| 279 | - | frame.render_widget(status_line(area.cols, stats, palette, debug), row); |
|
| 297 | + | frame.render_widget(status_line(area.cols, stats, palette, debug, error), row); |
|
| 280 | 298 | } |
|
| 281 | 299 | ||
| 282 | 300 | /// Returns the padded bottom status line. |
|
| 283 | - | fn status_line(cols: usize, stats: Stats, palette: DiffPalette, debug: Option<&str>) -> Line { |
|
| 301 | + | fn status_line( |
|
| 302 | + | cols: usize, |
|
| 303 | + | stats: Stats, |
|
| 304 | + | palette: DiffPalette, |
|
| 305 | + | debug: Option<&str>, |
|
| 306 | + | error: Option<&str>, |
|
| 307 | + | ) -> Line { |
|
| 308 | + | if let Some(error) = error { |
|
| 309 | + | let message = format!(" {error} "); |
|
| 310 | + | return Line::blank() |
|
| 311 | + | .item( |
|
| 312 | + | Label::new(" error ") |
|
| 313 | + | .style(Style::default().fg(Color::White).bg(Color::Red).bold()), |
|
| 314 | + | ) |
|
| 315 | + | .item(Label::new(&message).style(Style::default().fg(FOOTER_VALUE_FG).bg(FOOTER_BG))) |
|
| 316 | + | .style(Style::default().bg(FOOTER_BG)) |
|
| 317 | + | .padded(cols) |
|
| 318 | + | .truncated(cols, ""); |
|
| 319 | + | } |
|
| 284 | 320 | let badge = Line::new( |
|
| 285 | 321 | Label::new(" diff ") |
|
| 286 | 322 | .fg(Color::White) |
|
| 287 | 323 | .bg(APP_BADGE_BG) |
|
| 288 | 324 | .style(Style::default().bold()), |
| 341 | 377 | } |
|
| 342 | 378 | ||
| 343 | 379 | #[test] |
|
| 344 | 380 | fn status_line_fills_footer_width() { |
|
| 345 | 381 | assert_eq!( |
|
| 346 | - | status_line(24, Stats::default(), DiffPalette::default(), None).width(), |
|
| 382 | + | status_line(24, Stats::default(), DiffPalette::default(), None, None).width(), |
|
| 347 | 383 | 24 |
|
| 348 | 384 | ); |
|
| 349 | 385 | } |
|
| 350 | 386 | ||
| 351 | 387 | #[test] |
|
| 352 | 388 | fn status_line_renders_application_badge() { |
|
| 353 | - | let mut line = status_line(24, Stats::default(), DiffPalette::default(), None); |
|
| 389 | + | let mut line = status_line(24, Stats::default(), DiffPalette::default(), None, None); |
|
| 354 | 390 | let mut buffer = tui::Buffer::empty(Rect::new(0, 0, 24, 1)); |
|
| 355 | 391 | let badge_style = Style::default().fg(Color::White).bg(APP_BADGE_BG).bold(); |
|
| 356 | 392 | ||
| 357 | 393 | line.render(buffer.area(), &mut buffer); |
|
| 358 | 394 |
| 363 | 399 | assert!((0..6).all(|x| buffer.get(x, 0).unwrap().style() == badge_style)); |
|
| 364 | 400 | } |
|
| 365 | 401 | ||
| 366 | 402 | #[test] |
|
| 367 | 403 | fn status_line_renders_right_padding() { |
|
| 368 | - | let mut line = status_line(24, Stats::default(), DiffPalette::default(), None); |
|
| 404 | + | let mut line = status_line(24, Stats::default(), DiffPalette::default(), None, None); |
|
| 369 | 405 | let mut buffer = tui::Buffer::empty(Rect::new(0, 0, 24, 1)); |
|
| 370 | 406 | ||
| 371 | 407 | line.render(buffer.area(), &mut buffer); |
|
| 372 | 408 | ||
| 373 | 409 | assert_eq!(buffer.get(22, 0).unwrap().glyph(), "s"); |
|
| 374 | 410 | assert_eq!(buffer.get(23, 0).unwrap().glyph(), " "); |
|
| 375 | 411 | assert_eq!(buffer.get(23, 0).unwrap().style().bg_color(), FOOTER_BG); |
|
| 376 | 412 | } |
|
| 377 | 413 | ||
| 414 | + | #[test] |
|
| 415 | + | fn status_line_renders_recoverable_error() { |
|
| 416 | + | let mut line = status_line( |
|
| 417 | + | 40, |
|
| 418 | + | Stats::default(), |
|
| 419 | + | DiffPalette::default(), |
|
| 420 | + | Some("highlight 1.0ms"), |
|
| 421 | + | Some("input failed"), |
|
| 422 | + | ); |
|
| 423 | + | let mut buffer = tui::Buffer::empty(Rect::new(0, 0, 40, 1)); |
|
| 424 | + | ||
| 425 | + | line.render(buffer.area(), &mut buffer); |
|
| 426 | + | ||
| 427 | + | let text = (0..buffer.area().cols) |
|
| 428 | + | .filter_map(|x| buffer.get(x, 0)) |
|
| 429 | + | .map(|cell| cell.glyph()) |
|
| 430 | + | .collect::<String>(); |
|
| 431 | + | let badge_style = Style::default().fg(Color::White).bg(Color::Red).bold(); |
|
| 432 | + | let message_style = Style::default().fg(FOOTER_VALUE_FG).bg(FOOTER_BG); |
|
| 433 | + | ||
| 434 | + | assert!(text.contains(" error input failed")); |
|
| 435 | + | assert!(!text.contains("diff")); |
|
| 436 | + | assert!(!text.contains("highlight")); |
|
| 437 | + | assert!((0..7).all(|x| buffer.get(x, 0).unwrap().style() == badge_style)); |
|
| 438 | + | assert_eq!(buffer.get(7, 0).unwrap().style(), message_style); |
|
| 439 | + | ||
| 440 | + | let narrow = status_line( |
|
| 441 | + | 10, |
|
| 442 | + | Stats::default(), |
|
| 443 | + | DiffPalette::default(), |
|
| 444 | + | None, |
|
| 445 | + | Some("input failed"), |
|
| 446 | + | ); |
|
| 447 | + | ||
| 448 | + | assert_eq!(narrow.width(), 10); |
|
| 449 | + | assert_eq!(narrow.to_string(), " error in"); |
|
| 450 | + | } |
|
| 451 | + | ||
| 378 | 452 | #[test] |
|
| 379 | 453 | fn resize_requests_redraw_even_when_viewer_continues() { |
|
| 380 | 454 | let resized = true; |
|
| 381 | 455 | let control = DiffViewer::new(Default::default(), Default::default()).handle_input( |
|
| 382 | 456 | Event::Resize(tui::Size::new(80, 24)), |