Skip to content

Commit

Permalink
Support scrolling forward and backward half a page
Browse files Browse the repository at this point in the history
  • Loading branch information
YS-L committed Nov 26, 2023
1 parent fbed13d commit 0f23b38
Show file tree
Hide file tree
Showing 3 changed files with 72 additions and 0 deletions.
58 changes: 58 additions & 0 deletions src/app.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1065,4 +1065,62 @@ mod tests {
let lines = to_lines(&actual_buffer);
assert_eq!(lines, expected);
}

#[test]
fn test_scroll_half_page() {
let mut app = App::new(
"tests/data/cities.csv",
Delimiter::Default,
None,
false,
None,
false,
)
.unwrap();
thread::sleep(time::Duration::from_millis(100));

let backend = TestBackend::new(40, 10);
let mut terminal = Terminal::new(backend).unwrap();

// TODO: since some states are updated late only during rendering, sometimes some extra
// no-ops are required to warm up the states. I don't like it, but this is how it has to be
// in tests for now.
step_and_draw(&mut app, &mut terminal, Control::Nothing);
step_and_draw(&mut app, &mut terminal, Control::Nothing);
step_and_draw(&mut app, &mut terminal, Control::ScrollHalfPageDown);
step_and_draw(&mut app, &mut terminal, Control::ScrollHalfPageDown);
let expected = vec![
"────────────────────────────────────────",
" LatD LatM LatS NS … ",
"───┬────────────────────────────────────",
"5 │ 43 37 48 N … ",
"6 │ 36 5 59 N … ",
"7 │ 49 52 48 N … ",
"8 │ 39 11 23 N … ",
"9 │ 34 14 24 N … ",
"───┴────────────────────────────────────",
"stdin [Row 5/128, Col 1/10] ",
];
let actual_buffer = terminal.backend().buffer().clone();
let lines = to_lines(&actual_buffer);
assert_eq!(lines, expected);

step_and_draw(&mut app, &mut terminal, Control::Nothing);
step_and_draw(&mut app, &mut terminal, Control::ScrollHalfPageUp);
let expected = vec![
"────────────────────────────────────────",
" LatD LatM LatS NS … ",
"───┬────────────────────────────────────",
"3 │ 46 35 59 N … ",
"4 │ 42 16 12 N … ",
"5 │ 43 37 48 N … ",
"6 │ 36 5 59 N … ",
"7 │ 49 52 48 N … ",
"───┴────────────────────────────────────",
"stdin [Row 3/128, Col 1/10] ",
];
let actual_buffer = terminal.backend().buffer().clone();
let lines = to_lines(&actual_buffer);
assert_eq!(lines, expected);
}
}
6 changes: 6 additions & 0 deletions src/input.rs
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,8 @@ pub enum Control {
ScrollBottom,
ScrollPageUp,
ScrollPageDown,
ScrollHalfPageUp,
ScrollHalfPageDown,
ScrollPageLeft,
ScrollPageRight,
ScrollLeftMost,
Expand Down Expand Up @@ -118,6 +120,8 @@ impl InputHandler {
KeyCode::Char('H') => Control::Help,
KeyCode::PageDown => Control::ScrollPageDown,
KeyCode::PageUp => Control::ScrollPageUp,
KeyCode::Char('d') => Control::ScrollHalfPageDown,
KeyCode::Char('u') => Control::ScrollHalfPageUp,
KeyCode::Char(x) if "0123456789".contains(x.to_string().as_str()) => {
self.buffer_state = BufferState::Active(x.to_string());
self.mode = InputMode::GotoLine;
Expand Down Expand Up @@ -146,6 +150,8 @@ impl InputHandler {
KeyModifiers::CONTROL => match key_event.code {
KeyCode::Char('f') => Control::ScrollPageDown,
KeyCode::Char('b') => Control::ScrollPageUp,
KeyCode::Char('d') => Control::ScrollHalfPageDown,
KeyCode::Char('u') => Control::ScrollHalfPageUp,
KeyCode::Char('h') => Control::ScrollPageLeft,
KeyCode::Char('l') => Control::ScrollPageRight,
KeyCode::Left => Control::ScrollLeftMost,
Expand Down
8 changes: 8 additions & 0 deletions src/view.rs
Original file line number Diff line number Diff line change
Expand Up @@ -444,6 +444,10 @@ impl RowsView {
self.increase_rows_from(1)?;
}
}
Control::ScrollHalfPageDown => {
self.increase_rows_from(self.num_rows_rendered / 2)?;
self.selection.row.select_first()
}
Control::ScrollPageDown => {
self.increase_rows_from(self.num_rows_rendered)?;
self.selection.row.select_first()
Expand All @@ -459,6 +463,10 @@ impl RowsView {
self.decrease_rows_from(1)?;
}
}
Control::ScrollHalfPageUp => {
self.decrease_rows_from(self.num_rows_rendered / 2)?;
self.selection.row.select_first()
}
Control::ScrollPageUp => {
self.decrease_rows_from(self.num_rows_rendered)?;
self.selection.row.select_first()
Expand Down

0 comments on commit 0f23b38

Please sign in to comment.