Compare commits

...

3 Commits

Author SHA1 Message Date
pap
860f8b1e05 fix clippy 2025-08-09 12:32:23 +01:00
pap
6018eb1266 add ctrl+l unit test 2025-08-09 12:21:18 +01:00
pap
bf3f92dbb0 ctrl+l support 2025-08-09 12:16:43 +01:00

View File

@@ -182,6 +182,23 @@ impl App<'_> {
self.app_event_tx.clone()
}
/// Clear the entire screen and re-anchor the viewport at the very top-left.
/// Generic over backend so we can unit-test with `TestBackend`.
fn clear_to_top_generic<B: ratatui::backend::Backend>(
terminal: &mut crate::custom_terminal::Terminal<B>,
) {
let mut area = terminal.viewport_area;
area.y = 0;
terminal.set_viewport_area(area);
let _ = terminal.set_cursor_position((0, 0));
let _ = terminal
.backend_mut()
.clear_region(ratatui::backend::ClearType::All);
let _ = terminal.clear();
}
/// Schedule a redraw if one is not already pending.
#[allow(clippy::unwrap_used)]
fn schedule_redraw(&self) {
@@ -236,6 +253,16 @@ impl App<'_> {
self.app_event_tx.send(AppEvent::ExitRequest);
}
},
KeyEvent {
code: KeyCode::Char('l'),
modifiers: crossterm::event::KeyModifiers::CONTROL,
kind: KeyEventKind::Press,
..
} => {
// Clear-screen full and re-anchor to top, then redraw.
Self::clear_to_top_generic(terminal);
self.app_event_tx.send(AppEvent::RequestRedraw);
}
KeyEvent {
code: KeyCode::Esc,
kind: KeyEventKind::Press,
@@ -550,3 +577,31 @@ impl App<'_> {
}
}
}
#[cfg(test)]
mod tests {
use super::App;
use ratatui::backend::TestBackend;
use ratatui::layout::Rect;
#[test]
fn ctrl_l_clear_reanchors_viewport_and_moves_cursor() -> std::io::Result<()> {
// Build a test terminal with a small screen and a viewport offset.
let backend = TestBackend::new(20, 6);
let mut term = crate::custom_terminal::Terminal::with_options(backend)?;
// Simulate a viewport not at the top (e.g., after history insertions).
term.set_viewport_area(Rect::new(0, 3, 20, 2));
assert_eq!(term.viewport_area.y, 3);
// Invoke the same logic used by the Ctrl+L handler.
App::clear_to_top_generic(&mut term);
// Viewport should be re-anchored at the top and cursor at (0,0).
assert_eq!(term.viewport_area.y, 0);
let pos = term.get_cursor_position()?;
assert_eq!(pos.x, 0);
assert_eq!(pos.y, 0);
Ok(())
}
}