feat(tui): allow faster scrolling with page keys

This commit is contained in:
Orhun Parmaksız 2022-01-20 19:17:18 +03:00
parent 990acbc1dd
commit 5b3b07fd7c
No known key found for this signature in database
GPG key ID: F83424824B3E4B90
2 changed files with 47 additions and 17 deletions

View file

@ -165,26 +165,51 @@ impl<'a> App<'a> {
self.input_time = Some(Instant::now());
}
}
Command::Scroll(Direction::Up) => {
Command::Scroll(Direction::Up, amount) => {
if let Some(options) = self.options.as_mut() {
options.previous();
} else if !self.parameter_list.items.is_empty() {
self.parameter_list.previous();
if amount == 1 {
self.parameter_list.previous();
} else {
self.parameter_list.state.select(
self.parameter_list
.state
.selected()
.and_then(|v| v.checked_sub(amount.into()))
.or(Some(0)),
)
}
}
}
Command::Scroll(Direction::Down) => {
Command::Scroll(Direction::Down, amount) => {
if let Some(options) = self.options.as_mut() {
options.next();
} else if !self.parameter_list.items.is_empty() {
self.parameter_list.next();
if amount == 1 {
self.parameter_list.next();
} else {
self.parameter_list.state.select(
self.parameter_list
.state
.selected()
.and_then(|v| v.checked_add(amount.into()))
.map(|mut index| {
if index > self.parameter_list.items.len() {
index = self.parameter_list.items.len() - 1;
}
index
}),
)
}
}
}
Command::Scroll(Direction::Top) => {
Command::Scroll(Direction::Top, _) => {
if !self.parameter_list.items.is_empty() {
self.parameter_list.state.select(Some(0));
}
}
Command::Scroll(Direction::Bottom) => {
Command::Scroll(Direction::Bottom, _) => {
if let Some(last_index) = self.parameter_list.items.len().checked_sub(1) {
self.parameter_list.state.select(Some(last_index))
}

View file

@ -10,7 +10,7 @@ pub enum Command {
/// Set the value of a parameter.
Set(String, String),
/// Scroll the widget.
Scroll(Direction),
Scroll(Direction, u8),
/// Move cursor to right/left.
MoveCursor(u8),
/// Enable the search mode.
@ -48,12 +48,15 @@ impl FromStr for Command {
values.next().ok_or(())?.to_string(),
))
} else if s.starts_with("scroll") {
Ok(Command::Scroll(Direction::try_from(
*(s.split_whitespace()
.collect::<Vec<&str>>()
.get(1)
.ok_or(())?),
)?))
Ok(Command::Scroll(
Direction::try_from(
*(s.split_whitespace()
.collect::<Vec<&str>>()
.get(1)
.ok_or(())?),
)?,
1,
))
} else {
Err(())
}
@ -78,10 +81,12 @@ impl Command {
}
} else {
match key {
Key::Up => Command::Scroll(Direction::Up),
Key::Down => Command::Scroll(Direction::Down),
Key::Char('t') => Command::Scroll(Direction::Top),
Key::Char('b') => Command::Scroll(Direction::Bottom),
Key::Up => Command::Scroll(Direction::Up, 1),
Key::Down => Command::Scroll(Direction::Down, 1),
Key::PageUp => Command::Scroll(Direction::Up, 4),
Key::PageDown => Command::Scroll(Direction::Down, 4),
Key::Char('t') => Command::Scroll(Direction::Top, 0),
Key::Char('b') => Command::Scroll(Direction::Bottom, 0),
Key::Char(':') => Command::UpdateInput(' '),
Key::Char('/') => Command::EnableSearch,
Key::Char('\n') => Command::Select,