From c9d02507ac4f3235be06d0c2744dc8af1ebbd1d1 Mon Sep 17 00:00:00 2001 From: Saul Chavez Sanchez <34733080+ChaboCode@users.noreply.github.com> Date: Thu, 22 Oct 2020 13:21:49 -0500 Subject: [PATCH] Increase/Decrease volume by 5 (#298) * Increase/Decrease volume by 5 * Fixed static value * use fallback value in case parsing fails fixes #282 Co-authored-by: Henrik Friedrichsen --- .gitignore | 2 ++ README.md | 3 ++- src/command.rs | 16 ++++++++++------ src/commands.rs | 21 +++++++++++++++------ 4 files changed, 29 insertions(+), 13 deletions(-) diff --git a/.gitignore b/.gitignore index c1e281b..e41e974 100644 --- a/.gitignore +++ b/.gitignore @@ -9,3 +9,5 @@ *.log tags + +/.vscode/ \ No newline at end of file diff --git a/README.md b/README.md index 19c572b..adc310c 100644 --- a/README.md +++ b/README.md @@ -95,7 +95,8 @@ depending on your desktop environment settings. Have a look at the * `<` and `>` play the previous or next track * `f` and `b` to seek forward or backward * `Shift-f` and `Shift-b` to seek forward or backward in steps of 10s -* `-` and `+` decrease or increase the volume +* `-` and `+` decrease or increase the volume by 1 +* `[` and `]` decrease of increase the volume by 5 * `r` to toggle repeat mode * `z` to toggle shuffle playback * `q` quits ncspot diff --git a/src/command.rs b/src/command.rs index 2940c33..0390442 100644 --- a/src/command.rs +++ b/src/command.rs @@ -99,8 +99,8 @@ pub enum Command { Delete, Focus(String), Seek(SeekDirection), - VolumeUp, - VolumeDown, + VolumeUp(u16), + VolumeDown(u16), Repeat(Option), Shuffle(Option), Share(TargetMode), @@ -136,8 +136,8 @@ impl fmt::Display for Command { Command::Delete => "delete".to_string(), Command::Focus(tab) => format!("focus {}", tab), Command::Seek(direction) => format!("seek {}", direction), - Command::VolumeUp => "volup".to_string(), - Command::VolumeDown => "voldown".to_string(), + Command::VolumeUp(amount) => format!("volup {}", amount), + Command::VolumeDown(amount) => format!("voldown {}", amount), Command::Repeat(mode) => { let param = match mode { Some(mode) => format!("{}", mode), @@ -344,8 +344,12 @@ pub fn parse(input: &str) -> Option { _ => Command::Save, }) .or(Some(Command::Save)), - "volup" => Some(Command::VolumeUp), - "voldown" => Some(Command::VolumeDown), + "volup" => Some(Command::VolumeUp( + args.get(0).and_then(|v| v.parse::().ok()).unwrap_or(1), + )), + "voldown" => Some(Command::VolumeDown( + args.get(0).and_then(|v| v.parse::().ok()).unwrap_or(1), + )), "help" => Some(Command::Help), "reload" => Some(Command::ReloadConfig), "insert" => { diff --git a/src/commands.rs b/src/commands.rs index 877f181..edac43c 100644 --- a/src/commands.rs +++ b/src/commands.rs @@ -148,13 +148,19 @@ impl CommandManager { } Ok(None) } - Command::VolumeUp => { - let volume = self.spotify.volume().saturating_add(VOLUME_PERCENT); + Command::VolumeUp(amount) => { + let volume = self + .spotify + .volume() + .saturating_add(VOLUME_PERCENT * amount); self.spotify.set_volume(volume); Ok(None) } - Command::VolumeDown => { - let volume = self.spotify.volume().saturating_sub(VOLUME_PERCENT); + Command::VolumeDown(amount) => { + let volume = self + .spotify + .volume() + .saturating_sub(VOLUME_PERCENT * amount); debug!("vol {}", volume); self.spotify.set_volume(volume); Ok(None) @@ -294,8 +300,11 @@ impl CommandManager { "Shift+b".into(), Command::Seek(SeekDirection::Relative(-10000)), ); - kb.insert("+".into(), Command::VolumeUp); - kb.insert("-".into(), Command::VolumeDown); + kb.insert("+".into(), Command::VolumeUp(1)); + kb.insert("]".into(), Command::VolumeUp(5)); + kb.insert("-".into(), Command::VolumeDown(1)); + kb.insert("[".into(), Command::VolumeDown(5)); + kb.insert("r".into(), Command::Repeat(None)); kb.insert("z".into(), Command::Shuffle(None)); kb.insert("x".into(), Command::Share(TargetMode::Current));