Skip to content

Commit

Permalink
Add additional controls to text window
Browse files Browse the repository at this point in the history
Adds PgUp, PgDown, Home and End as controls for the text window
  • Loading branch information
LucasPickering committed Dec 15, 2023
1 parent bb539eb commit cc55c57
Show file tree
Hide file tree
Showing 3 changed files with 41 additions and 8 deletions.
2 changes: 2 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@
- Add collection ID/path to help modal ([#59](https://github.com/LucasPickering/slumber/issues/59))
- Also add collection ID to terminal title
- Persist UI state between sessions ([#39](https://github.com/LucasPickering/slumber/issues/39))
- Text window can be controlled with PgUp/PgDown/Home/End ([#77](https://github.com/LucasPickering/slumber/issues/77))

### Changed

Expand All @@ -24,6 +25,7 @@
### Fixed

- Don't require collection file to be present for `show` subcommand ([#62](https://github.com/LucasPickering/slumber/issues/62))
- Fix state file being created in root Slumber directory if collection file is invalid ([#71](https://github.com/LucasPickering/slumber/issues/71))

## [0.9.0] - 2023-11-28

Expand Down
8 changes: 8 additions & 0 deletions src/tui/input.rs
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,10 @@ impl InputEngine {
InputBinding::new(KeyCode::Down, Action::Down).hide(),
InputBinding::new(KeyCode::Left, Action::Left).hide(),
InputBinding::new(KeyCode::Right, Action::Right).hide(),
InputBinding::new(KeyCode::PageUp, Action::PageUp).hide(),
InputBinding::new(KeyCode::PageDown, Action::PageDown).hide(),
InputBinding::new(KeyCode::Home, Action::Home).hide(),
InputBinding::new(KeyCode::End, Action::End).hide(),
InputBinding::new(KeyCode::Enter, Action::Submit),
InputBinding::new(KeyCode::Esc, Action::Cancel),
]
Expand Down Expand Up @@ -154,6 +158,10 @@ pub enum Action {
Down,
Left,
Right,
PageUp,
PageDown,
Home,
End,

/// Do a thing. E.g. select an item in a list
Submit,
Expand Down
39 changes: 31 additions & 8 deletions src/tui/view/common/text_window.rs
Original file line number Diff line number Diff line change
Expand Up @@ -38,18 +38,26 @@ impl<T> TextWindow<T> {
}
}

/// Get the final line that we can't scroll past. This will be the first
/// line of the last page of text
fn max_scroll_line(&self) -> u16 {
self.text_height
.get()
.saturating_sub(self.window_height.get())
}

fn scroll_up(&mut self, lines: u16) {
self.offset_y = self.offset_y.saturating_sub(lines);
}

fn scroll_down(&mut self, lines: u16) {
self.offset_y = cmp::min(
self.offset_y + lines,
// Don't scroll past the bottom of the text
self.text_height
.get()
.saturating_sub(self.window_height.get()),
);
self.offset_y = cmp::min(self.offset_y + lines, self.max_scroll_line());
}

/// Scroll to a specific line number. The target line will end up as close
/// to the top of the page as possible
fn scroll_to(&mut self, line: u16) {
self.offset_y = cmp::min(line, self.max_scroll_line());
}
}

Expand All @@ -68,6 +76,22 @@ impl<T: Debug> EventHandler for TextWindow<T> {
self.scroll_down(1);
Update::Consumed
}
Action::PageUp => {
self.scroll_up(self.window_height.get());
Update::Consumed
}
Action::PageDown => {
self.scroll_down(self.window_height.get());
Update::Consumed
}
Action::Home => {
self.scroll_to(0);
Update::Consumed
}
Action::End => {
self.scroll_to(u16::MAX);
Update::Consumed
}
_ => Update::Propagate(event),
},
_ => Update::Propagate(event),
Expand All @@ -81,7 +105,6 @@ where
{
fn draw(&self, context: &mut DrawContext, _: (), area: Rect) {
let text = self.text.generate();
// TODO how do we handle text longer than 65k lines?
let text_height = text.lines.len() as u16;
self.text_height.set(text_height);
self.window_height.set(area.height);
Expand Down

0 comments on commit cc55c57

Please sign in to comment.