8000 Add vim-like page scrolling by BlakeJC94 · Pull Request #1072 · hrkfdn/ncspot · GitHub
[go: up one dir, main page]
More Web Proxy on the site http://driver.im/
Skip to content

Add vim-like page scrolling #1072

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 20 commits into from
Mar 12, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -330,6 +330,7 @@ Note: \<FOO\> - mandatory arg; [BAR] - optional arg
| `playpause`<br/>Aliases: `pause`, `toggleplay`, `toggleplayback` | Toggle playback. |
| `stop` | Stop playback. |
| `seek` [`+`\|`-`]\<TIME\> | Seek to the specified position, or seek relative to current position by prepending `+`/`-`.<br/>\* TIME is anything accepted by [parse_duration](https://docs.rs/parse_duration/latest/parse_duration/)<br/>\* Default unit is `ms` for backward compatibility. |
| `move` \<DIRECTION\> \<STEP_SIZE\> | Scroll the current view `up`/`down`/`left`/`right` with integer step sizes, or `pageup`/`pagedown`/`pageleft`/`pageright` with float step sizes.
| `repeat` [REPEAT_MODE]<br/>Alias: `loop` | Set repeat mode. Omit argument to step through the available modes.<br/>\* Valid values for REPEAT_MODE: `list` (aliases: `playlist`, `queue`), `track` (aliases: `once`, `single`), `none` (alias: `off`) |
| `shuffle` [`on`\|`off`] | Enable or disable shuffle. Omit argument to toggle. |
| `previous` | Play the previous track. |
Expand Down
27 changes: 23 additions & 4 deletions src/command.rs
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,7 @@ pub enum MoveMode {
#[strum(serialize_all = "lowercase")]
pub enum MoveAmount {
Integer(i32),
Float(f32),
Extreme,
}

Expand Down Expand Up @@ -186,6 +187,7 @@ impl fmt::Display for Command {
(MoveMode::Down, MoveAmount::Extreme) => vec!["bottom".to_string()],
(MoveMode::Left, MoveAmount::Extreme) => vec!["leftmost".to_string()],
(MoveMode::Right, MoveAmount::Extreme) => vec!["rightmost".to_string()],
(mode, MoveAmount::Float(amount)) => vec![mode.to_string(), amount.to_string()],
(mode, MoveAmount::Integer(amount)) => vec![mode.to_string(), amount.to_string()],
},
Command::Shift(mode, amount) => vec![mode.to_string(), amount.unwrap_or(1).to_string()],
Expand Down Expand Up @@ -562,10 +564,10 @@ pub fn parse(input: &str) -> Result<Vec<Command>, CommandParseError> {
use MoveMode::*;
match move_mode_raw {
"playing" => Ok(Playing),
"top" | "up" => Ok(Up),
"bottom" | "down" => Ok(Down),
"leftmost" | "left" => Ok(Left),
"rightmost" | "right" => Ok(Right),
"top" | "pageup" | "up" => Ok(Up),
"bottom" | "pagedown" | "down" => Ok(Down),
"leftmost" | "pageleft" | "left" => Ok(Left),
"rightmost" | "pageright" | "right" => Ok(Right),
_ => Err(BadEnumArg {
arg: move_mode_raw.into(),
accept: vec![
Expand All @@ -574,6 +576,10 @@ pub fn parse(input: &str) -> Result<Vec<Command>, CommandParseError> {
"bottom".into(),
"leftmost".into(),
"rightmost".into(),
"pageup".into(),
"pagedown".into(),
"pageleft".into(),
"pageright".into(),
"up".into(),
"down".into(),
"left".into(),
Expand All @@ -585,6 +591,19 @@ pub fn parse(input: &str) -> Result<Vec<Command>, CommandParseError> {
let move_amount = match move_mode_raw {
"playing" => Ok(MoveAmount::default()),
"top" | "bottom" | "leftmost" | "rightmost" => Ok(MoveAmount::Extreme),
"pageup" | "pagedown" | "pageleft" | "pageright" => {
let amount = match args.get(1) {
Some(&amount_raw) => amount_raw
.parse::<f32>()
.map(MoveAmount::Float)
.map_err(|err| ArgParseError {
arg: amount_raw.into(),
err: err.to_string(),
})?,
None => MoveAmount::default(),
};
Ok(amount)
}
"up" | "down" | "left" | "right" => {
let amount = match args.get(1) {
Some(&amount_raw) => amount_raw
Expand Down
9 changes: 9 additions & 0 deletions src/ext_traits.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
use cursive::views::ViewRef;
use cursive::{View, XY};

use crate::command::{Command, MoveAmount, MoveMode};
use crate::commands::CommandResult;
Expand Down Expand Up @@ -37,13 +38,21 @@ impl<T: 'static> SelectViewExt for cursive::views::SelectView<T> {
MoveMode::Up => {
match amount {
MoveAmount::Extreme => self.set_selection(0),
MoveAmount::Float(scale) => {
let amount = (*self).required_size(XY::default()).y as f32 * scale;
self.select_up(amount as usize)
}
MoveAmount::Integer(amount) => self.select_up(*amount as usize),
};
Ok(CommandResult::Consumed(None))
}
MoveMode::Down => {
match amount {
MoveAmount::Extreme => self.set_selection(items),
MoveAmount::Float(scale) => {
let amount = (*self).required_size(XY::default()).y as f32 * scale;
self.select_down(amount as usize)
}
MoveAmount::Integer(amount) => self.select_down(*amount as usize),
};
Ok(CommandResult::Consumed(None))
Expand Down
10 changes: 10 additions & 0 deletions src/ui/help.rs
Original file line number Diff line number Diff line change
Expand Up @@ -70,6 +70,11 @@ impl ViewExt for HelpView {
MoveAmount::Extreme => {
self.view.scroll_to_top();
}
MoveAmount::Float(scale) => {
let amount = (viewport.height() as f32) * scale;
scroller
.scroll_to_y(viewport.top().saturating_sub(amount as usize));
}
MoveAmount::Integer(amount) => scroller
.scroll_to_y(viewport.top().saturating_sub(*amount as usize)),
};
Expand All @@ -80,6 +85,11 @@ impl ViewExt for HelpView {
MoveAmount::Extreme => {
self.view.scroll_to_bottom();
}
MoveAmount::Float(scale) => {
let amount = (viewport.height() as f32) * scale;
scroller
.scroll_to_y(viewport.bottom().saturating_add(amount as usize));
}
MoveAmount::Integer(amount) => scroller
.scroll_to_y(viewport.bottom().saturating_add(*amount as usize)),
};
Expand Down
8 changes: 8 additions & 0 deletions src/ui/listview.rs
Original file line number Diff line number Diff line change
Expand Up @@ -542,6 +542,10 @@ impl<I: ListItem + Clone> ViewExt for ListView<I> {
if self.selected > 0 {
match amount {
MoveAmount::Extreme => self.move_focus_to(0),
MoveAmount::Float(scale) => {
let amount = (self.last_size.y as f32) * scale;
self.move_focus(-(amount as i32))
}
MoveAmount::Integer(amount) => self.move_focus(-(*amount)),
}
}
Expand All @@ -551,6 +555,10 @@ impl<I: ListItem + Clone> ViewExt for ListView<I> {
if self.selected < last_idx {
match amount {
MoveAmount::Extreme => self.move_focus_to(last_idx),
MoveAmount::Float(scale) => {
let amount = (self.last_size.y as f32) * scale;
self.move_focus(amount as i32)
}
MoveAmount::Integer(amount) => self.move_focus(*amount),
}
}
Expand Down
2 changes: 2 additions & 0 deletions src/ui/tabview.rs
Original file line number Diff line number Diff line change
Expand Up @@ -144,13 +144,15 @@ impl ViewExt for TabView {
match amount {
MoveAmount::Extreme => self.move_focus_to(0),
MoveAmount::Integer(amount) => self.move_focus(-(*amount)),
_ => (),
}
return Ok(CommandResult::Consumed(None));
}
MoveMode::Right if self.selected < last_idx => {
match amount {
MoveAmount::Extreme => self.move_focus_to(last_idx),
MoveAmount::Integer(amount) => self.move_focus(*amount),
_ => (),
}
return Ok(CommandResult::Consumed(None));
}
Expand Down
0