| 1 | //! `/statusline` multi-select picker. |
| 2 | //! |
| 3 | //! Mirrors codex-rs's `bottom_pane::status_line_setup` ergonomically: a |
| 4 | //! checklist of bottom-chrome items the user can toggle on/off with Space (or |
| 5 | //! Enter), moved through with ↑/↓, applied immediately so the live chrome |
| 6 | //! reflects every change. Enter saves to `~/.deepseek/config.toml` under |
| 7 | //! `tui.status_items`; Esc reverts to the snapshot taken on open. |
| 8 | //! |
| 9 | //! Every row here changes what is on screen (#5950): the metrics line's |
| 10 | //! segments ([`crate::tui::ui::frame::info_segments`]) and the posture bar's |
| 11 | //! mode chip. Between 0.9.12 and that fix the list was persisted and never |
| 12 | //! read, so the checklist was decoration — a new variant belongs in |
| 13 | //! `crates/runtime/src/config.rs` only once something paints it. |
| 14 | |
| 15 | use std::cell::RefCell; |
| 16 | |
| 17 | use crossterm::event::{KeyCode, KeyEvent, KeyModifiers, MouseButton, MouseEvent, MouseEventKind}; |
| 18 | use ratatui::{ |
| 19 | buffer::Buffer, |
| 20 | layout::Rect, |
| 21 | style::Style, |
| 22 | text::{Line, Span}, |
| 23 | widgets::{Padding, Paragraph, Widget}, |
| 24 | }; |
| 25 | |
| 26 | use crate::tui::views::{ |
| 27 | ActionHint, ModalKind, ModalView, SurfaceFrame, ViewAction, ViewEvent, action_footer_lines, |
| 28 | centered_modal_area, render_modal_footer, render_modal_rule, render_panel_scroll_rail, |
| 29 | render_rounded_modal, |
| 30 | }; |
| 31 | use codewhale_config::ProviderKind; |
| 32 | use codewhale_localization::{Locale, MessageId, tr}; |
| 33 | use codewhale_palette as palette; |
| 34 | use codewhale_runtime::config::StatusItem; |
| 35 | use codewhale_tui_kit::menu_style; |
| 36 | use unicode_width::UnicodeWidthStr; |
| 37 | |
| 38 | /// Picker state. We hold both the user's working selection AND the original |
| 39 | /// snapshot so Esc can perfectly revert the live preview. |
| 40 | pub struct StatusPickerView { |
| 41 | /// Every available item, in the order shown to the user. We keep this |
| 42 | /// list ordered so toggles produce a stable on-screen layout that |
| 43 | /// doesn't shuffle as items flip. |
| 44 | rows: Vec<StatusItem>, |
| 45 | /// Indices in `rows` currently checked on (the user's working set). |
| 46 | selected: Vec<bool>, |
| 47 | /// Highlighted row. |
| 48 | cursor: usize, |
| 49 | /// Snapshot of `app.status_items` at open time so Esc reverts cleanly. |
| 50 | original: Vec<StatusItem>, |
| 51 | locale: Locale, |
| 52 | row_hitboxes: RefCell<Vec<(usize, Rect)>>, |
| 53 | } |
| 54 | |
| 55 | impl StatusPickerView { |
| 56 | #[must_use] |
| 57 | pub fn new(active: &[StatusItem], provider: ProviderKind, locale: Locale) -> Self { |
| 58 | let rows: Vec<StatusItem> = StatusItem::all() |
| 59 | .iter() |
| 60 | .filter(|item| item.is_available_for(provider)) |
| 61 | .copied() |
| 62 | .collect(); |
| 63 | let selected: Vec<bool> = rows |
| 64 | .iter() |
| 65 | .map(|item| { |
| 66 | active.contains(item) |
| 67 | || (active.contains(&StatusItem::SessionMetrics) |
| 68 | && matches!(item, StatusItem::Ttft | StatusItem::OutputRate)) |
| 69 | }) |
| 70 | .collect(); |
| 71 | Self { |
| 72 | rows, |
| 73 | selected, |
| 74 | cursor: 0, |
| 75 | original: active.to_vec(), |
| 76 | locale, |
| 77 | row_hitboxes: RefCell::new(Vec::new()), |
| 78 | } |
| 79 | } |
| 80 | |
| 81 | /// Keep hidden preferences and emit all selected items in display order. |
| 82 | fn current_selection(&self) -> Vec<StatusItem> { |
| 83 | StatusItem::all() |
| 84 | .iter() |
| 85 | .copied() |
| 86 | .filter(|item| match self.rows.iter().position(|row| row == item) { |
| 87 | Some(index) => self.selected[index], |
| 88 | None => self.original.contains(item), |
| 89 | }) |
| 90 | .collect() |
| 91 | } |
| 92 | |
| 93 | /// Apply one [`list_nav`](codewhale_tui_kit::list_nav) motion (#6290), returning |
| 94 | /// whether it was consumed. Vertical motions wrap at the ends for |
| 95 | /// Prev/Next and clamp for paging and Home/End; the horizontal axis does |
| 96 | /// not exist on this single-column checklist. The checklist fits on one |
| 97 | /// screen, so a page is the whole list. |
| 98 | fn apply_motion(&mut self, motion: codewhale_tui_kit::list_nav::Motion) -> bool { |
| 99 | let Some(next) = codewhale_tui_kit::list_nav::apply( |
| 100 | self.cursor, |
| 101 | self.rows.len(), |
| 102 | self.rows.len().max(1), |
| 103 | motion, |
| 104 | ) else { |
| 105 | return false; |
| 106 | }; |
| 107 | self.cursor = next; |
| 108 | true |
| 109 | } |
| 110 | |
| 111 | fn toggle_current(&mut self) { |
| 112 | if let Some(slot) = self.selected.get_mut(self.cursor) { |
| 113 | *slot = !*slot; |
| 114 | } |
| 115 | } |
| 116 | |
| 117 | fn live_preview_event(&self) -> ViewEvent { |
| 118 | ViewEvent::StatusItemsUpdated { |
| 119 | items: self.current_selection(), |
| 120 | final_save: false, |
| 121 | } |
| 122 | } |
| 123 | |
| 124 | fn final_event(&self) -> ViewEvent { |
| 125 | ViewEvent::StatusItemsUpdated { |
| 126 | items: self.current_selection(), |
| 127 | final_save: true, |
| 128 | } |
| 129 | } |
| 130 | |
| 131 | fn revert_event(&self) -> ViewEvent { |
| 132 | ViewEvent::StatusItemsUpdated { |
| 133 | items: self.original.clone(), |
| 134 | final_save: false, |
| 135 | } |
| 136 | } |
| 137 | } |
| 138 | |
| 139 | impl ModalView for StatusPickerView { |
| 140 | fn kind(&self) -> ModalKind { |
| 141 | ModalKind::StatusPicker |
| 142 | } |
| 143 | |
| 144 | fn as_any_mut(&mut self) -> &mut dyn std::any::Any { |
| 145 | self |
| 146 | } |
| 147 | |
| 148 | fn handle_key(&mut self, key: KeyEvent) -> ViewAction { |
| 149 | // Movement keys come from the shared vocabulary (#6290): `j`/`k`, |
| 150 | // Home/End and the page keys mean here what they mean on every other |
| 151 | // list. This match owns only the checklist's own verbs. |
| 152 | if let Some(motion) = codewhale_tui_kit::list_nav::motion(&key) |
| 153 | && self.apply_motion(motion) |
| 154 | { |
| 155 | return ViewAction::None; |
| 156 | } |
| 157 | match key.code { |
| 158 | KeyCode::Esc => { |
| 159 | // Roll the live preview back to the snapshot so Esc means |
| 160 | // "take me back to where I was." |
| 161 | ViewAction::EmitAndClose(self.revert_event()) |
| 162 | } |
| 163 | KeyCode::Enter => ViewAction::EmitAndClose(self.final_event()), |
| 164 | KeyCode::Char(' ') | KeyCode::Char('x') | KeyCode::Char('X') => { |
| 165 | self.toggle_current(); |
| 166 | ViewAction::Emit(self.live_preview_event()) |
| 167 | } |
| 168 | KeyCode::Char('a') | KeyCode::Char('A') |
| 169 | if !key.modifiers.contains(KeyModifiers::CONTROL) => |
| 170 | { |
| 171 | // Quality-of-life: 'a' selects all so the user can quickly |
| 172 | // see every chip available before paring back. |
| 173 | self.selected.fill(true); |
| 174 | ViewAction::Emit(self.live_preview_event()) |
| 175 | } |
| 176 | KeyCode::Char('n') | KeyCode::Char('N') => { |
| 177 | // 'n' clears all so the user can build up from scratch. |
| 178 | self.selected.fill(false); |
| 179 | ViewAction::Emit(self.live_preview_event()) |
| 180 | } |
| 181 | _ => ViewAction::None, |
| 182 | } |
| 183 | } |
| 184 | |
| 185 | fn handle_mouse(&mut self, mouse: MouseEvent) -> ViewAction { |
| 186 | match mouse.kind { |
| 187 | MouseEventKind::ScrollUp => { |
| 188 | self.apply_motion(codewhale_tui_kit::list_nav::Motion::Prev); |
| 189 | } |
| 190 | MouseEventKind::ScrollDown => { |
| 191 | self.apply_motion(codewhale_tui_kit::list_nav::Motion::Next); |
| 192 | } |
| 193 | MouseEventKind::Down(MouseButton::Left) => { |
| 194 | let clicked = self.row_hitboxes.borrow().iter().find_map(|(index, rect)| { |
| 195 | rect.contains(ratatui::layout::Position::new(mouse.column, mouse.row)) |
| 196 | .then_some(*index) |
| 197 | }); |
| 198 | if let Some(index) = clicked { |
| 199 | self.cursor = index; |
| 200 | self.toggle_current(); |
| 201 | return ViewAction::Emit(self.live_preview_event()); |
| 202 | } |
| 203 | } |
| 204 | _ => {} |
| 205 | } |
| 206 | ViewAction::None |
| 207 | } |
| 208 | |
| 209 | fn render(&self, area: Rect, buf: &mut Buffer) { |
| 210 | let hints = [ |
| 211 | ActionHint::new( |
| 212 | "Space", |
| 213 | tr(self.locale, MessageId::StatusPickerActionToggle), |
| 214 | ), |
| 215 | ActionHint::new("a", tr(self.locale, MessageId::StatusPickerActionAll)), |
| 216 | ActionHint::new("n", tr(self.locale, MessageId::StatusPickerActionNone)), |
| 217 | ActionHint::new("Enter", tr(self.locale, MessageId::StatusPickerActionSave)), |
| 218 | ActionHint::new("Esc", tr(self.locale, MessageId::StatusPickerActionCancel)), |
| 219 | ]; |
| 220 | // Two header lines + one row per StatusItem + the action footer rows |
| 221 | // as they wrap at the popup's inner width, plus the border rows, and |
| 222 | // the padding rows above and below when the whole list fits. |
| 223 | // centered_modal_area clamps this to the frame and lets the scroll |
| 224 | // offset absorb any remaining overflow. |
| 225 | let popup_width = centered_modal_area(area, 64, 8, 40, 8).width; |
| 226 | let footer_rows = action_footer_lines(&hints, popup_width.saturating_sub(4)) |
| 227 | .len() |
| 228 | .max(1) as u16; |
| 229 | let unpadded_height = (self.rows.len() as u16) |
| 230 | .saturating_add(4) |
| 231 | .saturating_add(footer_rows); |
| 232 | let pad = u16::from(unpadded_height.saturating_add(2) <= area.height.saturating_sub(2)); |
| 233 | let popup_area = |
| 234 | centered_modal_area(area, 64, unpadded_height.saturating_add(2 * pad), 40, 8); |
| 235 | |
| 236 | let enabled_count = self.selected.iter().filter(|&&on| on).count(); |
| 237 | let (inner, _) = render_rounded_modal( |
| 238 | area, |
| 239 | popup_area, |
| 240 | buf, |
| 241 | SurfaceFrame::new(tr(self.locale, MessageId::StatusPickerTitle)) |
| 242 | .right_pill( |
| 243 | format!("{enabled_count}/{}", self.rows.len()), |
| 244 | Style::default().fg(palette::TEXT_MUTED), |
| 245 | ) |
| 246 | .padding(Padding::new(1, 1, pad, pad)), |
| 247 | ); |
| 248 | |
| 249 | let content = render_modal_footer(inner, buf, &hints); |
| 250 | |
| 251 | self.row_hitboxes.borrow_mut().clear(); |
| 252 | let visible_rows = content.height.saturating_sub(2) as usize; |
| 253 | let row_start = visible_row_start(self.rows.len(), self.cursor, visible_rows); |
| 254 | let row_width = content |
| 255 | .width |
| 256 | .saturating_sub(u16::from(visible_rows < self.rows.len())); |
| 257 | |
| 258 | if content.height >= 1 { |
| 259 | Paragraph::new(Line::from(Span::styled( |
| 260 | tr(self.locale, MessageId::StatusPickerInstruction), |
| 261 | Style::default().fg(palette::TEXT_MUTED), |
| 262 | ))) |
| 263 | .render(Rect::new(content.x, content.y, content.width, 1), buf); |
| 264 | } |
| 265 | if content.height >= 2 { |
| 266 | render_modal_rule(Rect::new(content.x, content.y + 1, content.width, 1), buf); |
| 267 | } |
| 268 | |
| 269 | let list_area = Rect { |
| 270 | y: content.y.saturating_add(2), |
| 271 | height: content.height.saturating_sub(2), |
| 272 | ..content |
| 273 | }; |
| 274 | let mut lines: Vec<Line> = Vec::with_capacity(visible_rows); |
| 275 | |
| 276 | for (idx, item) in self |
| 277 | .rows |
| 278 | .iter() |
| 279 | .enumerate() |
| 280 | .skip(row_start) |
| 281 | .take(visible_rows) |
| 282 | { |
| 283 | self.row_hitboxes.borrow_mut().push(( |
| 284 | idx, |
| 285 | Rect::new( |
| 286 | list_area.x, |
| 287 | list_area.y + (idx - row_start) as u16, |
| 288 | row_width, |
| 289 | 1, |
| 290 | ), |
| 291 | )); |
| 292 | let checked = *self.selected.get(idx).unwrap_or(&false); |
| 293 | let is_cursor = idx == self.cursor; |
| 294 | let mark = if checked { "[✓]" } else { "[ ]" }; |
| 295 | |
| 296 | let row_style = if is_cursor { |
| 297 | menu_style::selected_row_style() |
| 298 | } else if checked { |
| 299 | Style::default().fg(palette::TEXT_PRIMARY) |
| 300 | } else { |
| 301 | Style::default().fg(palette::TEXT_MUTED) |
| 302 | }; |
| 303 | let mark_style = if is_cursor { |
| 304 | menu_style::selected_row_style() |
| 305 | } else if checked { |
| 306 | Style::default().fg(palette::STATUS_SUCCESS) |
| 307 | } else { |
| 308 | Style::default().fg(palette::TEXT_DIM) |
| 309 | }; |
| 310 | let hint_style = if is_cursor { |
| 311 | menu_style::selected_row_bg_style().fg(palette::SELECTION_TEXT) |
| 312 | } else { |
| 313 | Style::default().fg(palette::TEXT_DIM) |
| 314 | }; |
| 315 | let pointer = codewhale_tui_kit::glyphs::selection_marker(is_cursor); |
| 316 | |
| 317 | if is_cursor { |
| 318 | let selected_style = menu_style::selected_row_style(); |
| 319 | let line = |
| 320 | status_row_text(pointer, mark, item, self.locale, usize::from(row_width)); |
| 321 | lines.push(Line::from(Span::styled(line, selected_style))); |
| 322 | } else { |
| 323 | let label = item.label(); |
| 324 | let hint = item.hint(self.locale); |
| 325 | let prefix = format!(" {pointer} {mark} {label} ("); |
| 326 | let truncated_hint = codewhale_tui_kit::ui_text::semantic_truncate_between_affixes( |
| 327 | &prefix, |
| 328 | hint.as_ref(), |
| 329 | ")", |
| 330 | usize::from(row_width), |
| 331 | ); |
| 332 | lines.push(Line::from(vec![ |
| 333 | Span::styled(format!(" {pointer} "), row_style), |
| 334 | Span::styled(mark.to_string(), mark_style), |
| 335 | Span::styled(" ", row_style), |
| 336 | Span::styled(label.to_string(), row_style), |
| 337 | Span::styled(" ", row_style), |
| 338 | Span::styled(format!("({})", truncated_hint), hint_style), |
| 339 | ])); |
| 340 | } |
| 341 | } |
| 342 | |
| 343 | Paragraph::new(lines).render(list_area, buf); |
| 344 | render_panel_scroll_rail( |
| 345 | list_area, |
| 346 | buf, |
| 347 | self.rows.len(), |
| 348 | row_start, |
| 349 | visible_rows, |
| 350 | true, |
| 351 | ); |
| 352 | } |
| 353 | } |
| 354 | |
| 355 | fn visible_row_start(total_rows: usize, cursor: usize, visible_rows: usize) -> usize { |
| 356 | if total_rows == 0 || visible_rows == 0 || total_rows <= visible_rows { |
| 357 | return 0; |
| 358 | } |
| 359 | let max_start = total_rows - visible_rows; |
| 360 | cursor |
| 361 | .saturating_add(1) |
| 362 | .saturating_sub(visible_rows) |
| 363 | .min(max_start) |
| 364 | } |
| 365 | |
| 366 | fn status_row_text( |
| 367 | pointer: &str, |
| 368 | mark: &str, |
| 369 | item: &StatusItem, |
| 370 | locale: Locale, |
| 371 | width: usize, |
| 372 | ) -> String { |
| 373 | let prefix = format!(" {pointer} {mark} {} (", item.label()); |
| 374 | let mut text = codewhale_tui_kit::ui_text::semantic_truncate_with_affixes( |
| 375 | &prefix, |
| 376 | item.hint(locale).as_ref(), |
| 377 | ")", |
| 378 | width, |
| 379 | ); |
| 380 | let current_width = text.width(); |
| 381 | if current_width < width { |
| 382 | text.push_str(&" ".repeat(width - current_width)); |
| 383 | } |
| 384 | text |
| 385 | } |
| 386 | |
| 387 | #[cfg(test)] |
| 388 | mod tests { |
| 389 | use super::*; |
| 390 | use codewhale_localization::Locale; |
| 391 | |
| 392 | #[test] |
| 393 | fn opens_with_active_items_pre_selected() { |
| 394 | let active = StatusItem::default_footer(); |
| 395 | let view = StatusPickerView::new(&active, ProviderKind::Deepseek, Locale::En); |
| 396 | assert_eq!(view.current_selection(), active); |
| 397 | } |
| 398 | |
| 399 | #[test] |
| 400 | fn legacy_metrics_can_be_split_and_cancel_restores_the_saved_pair() { |
| 401 | let original = vec![StatusItem::SessionMetrics]; |
| 402 | let mut view = StatusPickerView::new(&original, ProviderKind::Stepfun, Locale::En); |
| 403 | assert_eq!( |
| 404 | view.current_selection(), |
| 405 | vec![StatusItem::Ttft, StatusItem::OutputRate] |
| 406 | ); |
| 407 | view.cursor = view |
| 408 | .rows |
| 409 | .iter() |
| 410 | .position(|item| *item == StatusItem::OutputRate) |
| 411 | .unwrap(); |
| 412 | view.handle_key(KeyEvent::new(KeyCode::Char(' '), KeyModifiers::NONE)); |
| 413 | assert_eq!(view.current_selection(), vec![StatusItem::Ttft]); |
| 414 | match view.handle_key(KeyEvent::new(KeyCode::Esc, KeyModifiers::NONE)) { |
| 415 | ViewAction::EmitAndClose(ViewEvent::StatusItemsUpdated { items, final_save }) => { |
| 416 | assert_eq!(items, original); |
| 417 | assert!(!final_save); |
| 418 | } |
| 419 | action => panic!("unexpected cancel: {action:?}"), |
| 420 | } |
| 421 | } |
| 422 | |
| 423 | #[test] |
| 424 | fn mouse_toggles_the_painted_row_after_scrolling_a_short_picker() { |
| 425 | let mut view = StatusPickerView::new( |
| 426 | &StatusItem::default_footer(), |
| 427 | ProviderKind::Stepfun, |
| 428 | Locale::En, |
| 429 | ); |
| 430 | view.handle_key(KeyEvent::new(KeyCode::End, KeyModifiers::NONE)); |
| 431 | let area = Rect::new(0, 0, 40, 12); |
| 432 | view.render(area, &mut Buffer::empty(area)); |
| 433 | let (index, rect) = *view.row_hitboxes.borrow().last().expect("visible row"); |
| 434 | let was_selected = view.selected[index]; |
| 435 | let action = view.handle_mouse(MouseEvent { |
| 436 | kind: MouseEventKind::Down(MouseButton::Left), |
| 437 | column: rect.x + 2, |
| 438 | row: rect.y, |
| 439 | modifiers: KeyModifiers::NONE, |
| 440 | }); |
| 441 | assert!(matches!( |
| 442 | action, |
| 443 | ViewAction::Emit(ViewEvent::StatusItemsUpdated { |
| 444 | final_save: false, |
| 445 | .. |
| 446 | }) |
| 447 | )); |
| 448 | assert_eq!(view.cursor, index); |
| 449 | assert_eq!(view.selected[index], !was_selected); |
| 450 | } |
| 451 | |
| 452 | #[test] |
| 453 | fn space_toggles_current_row_and_emits_live_preview() { |
| 454 | let active = StatusItem::default_footer(); |
| 455 | let mut view = StatusPickerView::new(&active, ProviderKind::Deepseek, Locale::En); |
| 456 | let action = view.handle_key(KeyEvent::new(KeyCode::Char(' '), KeyModifiers::NONE)); |
| 457 | match action { |
| 458 | ViewAction::Emit(ViewEvent::StatusItemsUpdated { items, final_save }) => { |
| 459 | assert!(!final_save); |
| 460 | assert!(!items.contains(&StatusItem::Mode)); |
| 461 | } |
| 462 | other => panic!("expected live preview emit, got {other:?}"), |
| 463 | } |
| 464 | } |
| 465 | |
| 466 | #[test] |
| 467 | fn enter_emits_final_save() { |
| 468 | let active = StatusItem::default_footer(); |
| 469 | let mut view = StatusPickerView::new(&active, ProviderKind::Deepseek, Locale::En); |
| 470 | let action = view.handle_key(KeyEvent::new(KeyCode::Enter, KeyModifiers::NONE)); |
| 471 | match action { |
| 472 | ViewAction::EmitAndClose(ViewEvent::StatusItemsUpdated { final_save, .. }) => { |
| 473 | assert!(final_save); |
| 474 | } |
| 475 | other => panic!("expected final save EmitAndClose, got {other:?}"), |
| 476 | } |
| 477 | } |
| 478 | |
| 479 | #[test] |
| 480 | fn esc_reverts_to_snapshot() { |
| 481 | let active = StatusItem::default_footer(); |
| 482 | let mut view = StatusPickerView::new(&active, ProviderKind::Deepseek, Locale::En); |
| 483 | view.handle_key(KeyEvent::new(KeyCode::Char(' '), KeyModifiers::NONE)); |
| 484 | // Move through the shared vocabulary, the same path a key takes. |
| 485 | view.handle_key(KeyEvent::new(KeyCode::Down, KeyModifiers::NONE)); |
| 486 | view.handle_key(KeyEvent::new(KeyCode::Char(' '), KeyModifiers::NONE)); |
| 487 | let action = view.handle_key(KeyEvent::new(KeyCode::Esc, KeyModifiers::NONE)); |
| 488 | match action { |
| 489 | ViewAction::EmitAndClose(ViewEvent::StatusItemsUpdated { items, final_save }) => { |
| 490 | assert!(!final_save); |
| 491 | assert_eq!(items, active); |
| 492 | } |
| 493 | other => panic!("expected revert EmitAndClose, got {other:?}"), |
| 494 | } |
| 495 | } |
| 496 | |
| 497 | #[test] |
| 498 | fn select_all_and_select_none_keys_work() { |
| 499 | let active: Vec<StatusItem> = Vec::new(); |
| 500 | let mut view = StatusPickerView::new(&active, ProviderKind::Deepseek, Locale::En); |
| 501 | let action = view.handle_key(KeyEvent::new(KeyCode::Char('a'), KeyModifiers::NONE)); |
| 502 | match action { |
| 503 | ViewAction::Emit(ViewEvent::StatusItemsUpdated { items, .. }) => { |
| 504 | assert_eq!(items.len(), StatusItem::all().len()); |
| 505 | } |
| 506 | other => panic!("expected select-all emit, got {other:?}"), |
| 507 | } |
| 508 | let action = view.handle_key(KeyEvent::new(KeyCode::Char('n'), KeyModifiers::NONE)); |
| 509 | match action { |
| 510 | ViewAction::Emit(ViewEvent::StatusItemsUpdated { items, .. }) => { |
| 511 | assert!(items.is_empty()); |
| 512 | } |
| 513 | other => panic!("expected select-none emit, got {other:?}"), |
| 514 | } |
| 515 | } |
| 516 | |
| 517 | #[test] |
| 518 | fn arrow_keys_wrap_cursor_at_edges() { |
| 519 | let active = StatusItem::default_footer(); |
| 520 | let mut view = StatusPickerView::new(&active, ProviderKind::Deepseek, Locale::En); |
| 521 | assert_eq!(view.cursor, 0); |
| 522 | view.handle_key(KeyEvent::new(KeyCode::Up, KeyModifiers::NONE)); |
| 523 | assert_eq!(view.cursor, StatusItem::all().len() - 1); |
| 524 | view.handle_key(KeyEvent::new(KeyCode::Down, KeyModifiers::NONE)); |
| 525 | assert_eq!(view.cursor, 0); |
| 526 | view.handle_key(KeyEvent::new(KeyCode::Down, KeyModifiers::NONE)); |
| 527 | assert_eq!(view.cursor, 1); |
| 528 | view.handle_key(KeyEvent::new(KeyCode::Up, KeyModifiers::NONE)); |
| 529 | assert_eq!(view.cursor, 0); |
| 530 | } |
| 531 | |
| 532 | #[test] |
| 533 | fn visible_row_start_keeps_cursor_in_view() { |
| 534 | assert_eq!(visible_row_start(14, 0, 8), 0); |
| 535 | assert_eq!(visible_row_start(14, 7, 8), 0); |
| 536 | assert_eq!(visible_row_start(14, 8, 8), 1); |
| 537 | assert_eq!(visible_row_start(14, 13, 8), 6); |
| 538 | } |
| 539 | |
| 540 | #[test] |
| 541 | fn selected_row_text_fills_available_width() { |
| 542 | let text = status_row_text("▸", "[ ]", &StatusItem::Cache, Locale::En, 40); |
| 543 | assert_eq!(text.width(), 40); |
| 544 | assert!(text.starts_with(" ▸ [ ] Prompt cache hit rate")); |
| 545 | } |
| 546 | |
| 547 | #[test] |
| 548 | fn selected_row_text_semantically_truncates_hint_at_narrow_width() { |
| 549 | let text = status_row_text("▸", "[ ]", &StatusItem::Cache, Locale::En, 44); |
| 550 | assert_eq!(text.width(), 44); |
| 551 | // Cut at a word boundary, never mid-word. |
| 552 | assert!(text.contains("% of…"), "{text:?}"); |
| 553 | assert!(!text.contains("% of p"), "{text:?}"); |
| 554 | } |
| 555 | |
| 556 | #[test] |
| 557 | fn balance_offered_for_prepaid_providers_and_hidden_for_local() { |
| 558 | let active = StatusItem::default_footer(); |
| 559 | let openrouter = StatusPickerView::new(&active, ProviderKind::Openrouter, Locale::En); |
| 560 | assert!(openrouter.rows.contains(&StatusItem::Balance)); |
| 561 | let ollama = StatusPickerView::new(&active, ProviderKind::Ollama, Locale::En); |
| 562 | assert!(!ollama.rows.contains(&StatusItem::Balance)); |
| 563 | assert!(ollama.rows.contains(&StatusItem::Mode)); |
| 564 | } |
| 565 | |
| 566 | #[test] |
| 567 | fn saving_status_items_keeps_the_balance_preference_on_a_local_provider() { |
| 568 | let mut ollama = StatusPickerView::new( |
| 569 | &[StatusItem::Balance, StatusItem::Mode], |
| 570 | ProviderKind::Ollama, |
| 571 | Locale::En, |
| 572 | ); |
| 573 | ollama.handle_key(KeyEvent::new(KeyCode::Char('n'), KeyModifiers::NONE)); |
| 574 | let action = ollama.handle_key(KeyEvent::new(KeyCode::Enter, KeyModifiers::NONE)); |
| 575 | let ViewAction::EmitAndClose(ViewEvent::StatusItemsUpdated { |
| 576 | items, |
| 577 | final_save: true, |
| 578 | }) = action |
| 579 | else { |
| 580 | panic!("expected saved status items"); |
| 581 | }; |
| 582 | assert_eq!(items, [StatusItem::Balance]); |
| 583 | } |
| 584 | |
| 585 | #[test] |
| 586 | fn status_picker_displays_localized_title_for_zh_hans() { |
| 587 | assert_eq!(tr(Locale::ZhHans, MessageId::StatusPickerTitle), " 状态行 "); |
| 588 | } |
| 589 | |
| 590 | /// The four terminal sizes the v0.8.66 modal blocker (#3732) requires |
| 591 | /// every overlay to remain readable and fully operable at. |
| 592 | const BLOCKER_SIZES: [(u16, u16); 4] = [(80, 24), (100, 30), (120, 32), (160, 40)]; |
| 593 | |
| 594 | #[test] |
| 595 | fn status_picker_is_usable_and_opaque_at_blocker_sizes() { |
| 596 | use crate::tui::views::ViewStack; |
| 597 | let active = StatusItem::default_footer(); |
| 598 | for (w, h) in BLOCKER_SIZES { |
| 599 | let area = Rect::new(0, 0, w, h); |
| 600 | let mut buf = Buffer::empty(area); |
| 601 | for y in 0..h { |
| 602 | for x in 0..w { |
| 603 | buf[(x, y)].set_symbol("X"); |
| 604 | } |
| 605 | } |
| 606 | let mut stack = ViewStack::new(); |
| 607 | stack.push(StatusPickerView::new( |
| 608 | &active, |
| 609 | ProviderKind::Deepseek, |
| 610 | Locale::En, |
| 611 | )); |
| 612 | stack.render(area, &mut buf); |
| 613 | |
| 614 | let rows: Vec<String> = (0..h) |
| 615 | .map(|y| { |
| 616 | (0..w) |
| 617 | .map(|x| buf[(x, y)].symbol().to_string()) |
| 618 | .collect::<String>() |
| 619 | }) |
| 620 | .collect(); |
| 621 | let text = rows.join("\n"); |
| 622 | |
| 623 | for label in ["toggle", "all", "none", "save", "cancel"] { |
| 624 | assert!(text.contains(label), "{w}x{h}: missing footer '{label}'"); |
| 625 | } |
| 626 | assert!( |
| 627 | !text.contains('X'), |
| 628 | "{w}x{h}: background bleed-through into modal surface" |
| 629 | ); |
| 630 | assert_eq!( |
| 631 | buf[(w / 2, h / 2)].bg, |
| 632 | palette::WHALE_BG, |
| 633 | "{w}x{h}: modal interior must be opaque" |
| 634 | ); |
| 635 | for (y, row) in rows.iter().enumerate() { |
| 636 | assert!( |
| 637 | UnicodeWidthStr::width(row.trim_end()) <= w as usize, |
| 638 | "{w}x{h}: row {y} overflows width: {row:?}" |
| 639 | ); |
| 640 | } |
| 641 | } |
| 642 | } |
| 643 | |
| 644 | #[test] |
| 645 | fn status_picker_no_english_leak_in_non_en_locales() { |
| 646 | for locale in [ |
| 647 | Locale::Ja, |
| 648 | Locale::ZhHans, |
| 649 | Locale::ZhHant, |
| 650 | Locale::PtBr, |
| 651 | Locale::Es419, |
| 652 | Locale::Vi, |
| 653 | Locale::Ca, |
| 654 | Locale::De, |
| 655 | Locale::Fr, |
| 656 | Locale::Id, |
| 657 | Locale::Hi, |
| 658 | Locale::Ru, |
| 659 | Locale::Uk, |
| 660 | ] { |
| 661 | let title = tr(locale, MessageId::StatusPickerTitle); |
| 662 | if locale == Locale::De { |
| 663 | // German "Statuszeile" is the correct native term — "Status" |
| 664 | // is a German word, not an English leak. |
| 665 | assert_eq!(title, " Statuszeile "); |
| 666 | } else { |
| 667 | assert!( |
| 668 | !title.contains("Status"), |
| 669 | "{} leaks English in title: {title}", |
| 670 | locale.tag() |
| 671 | ); |
| 672 | } |
| 673 | let instruction = tr(locale, MessageId::StatusPickerInstruction); |
| 674 | assert!( |
| 675 | !instruction.contains("footer"), |
| 676 | "{} leaks English in instruction: {instruction}", |
| 677 | locale.tag() |
| 678 | ); |
| 679 | } |
| 680 | } |
| 681 | } |
| 682 |