Skip to content

Commit

Permalink
Increase/Decrease volume by 5 (#298)
Browse files Browse the repository at this point in the history
* Increase/Decrease volume by 5

* Fixed static value

* use fallback value in case parsing fails

fixes #282

Co-authored-by: Henrik Friedrichsen <henrik@affekt.org>
  • Loading branch information
ChaboCode and hrkfdn committed Oct 22, 2020
1 parent da915ab commit c9d0250
Show file tree
Hide file tree
Showing 4 changed files with 29 additions and 13 deletions.
2 changes: 2 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -9,3 +9,5 @@
*.log

tags

/.vscode/
3 changes: 2 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
16 changes: 10 additions & 6 deletions src/command.rs
Original file line number Diff line number Diff line change
Expand Up @@ -99,8 +99,8 @@ pub enum Command {
Delete,
Focus(String),
Seek(SeekDirection),
VolumeUp,
VolumeDown,
VolumeUp(u16),
VolumeDown(u16),
Repeat(Option<RepeatSetting>),
Shuffle(Option<bool>),
Share(TargetMode),
Expand Down Expand Up @@ -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),
Expand Down Expand Up @@ -344,8 +344,12 @@ pub fn parse(input: &str) -> Option<Command> {
_ => 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::<u16>().ok()).unwrap_or(1),
)),
"voldown" => Some(Command::VolumeDown(
args.get(0).and_then(|v| v.parse::<u16>().ok()).unwrap_or(1),
)),
"help" => Some(Command::Help),
"reload" => Some(Command::ReloadConfig),
"insert" => {
Expand Down
21 changes: 15 additions & 6 deletions src/commands.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down Expand Up @@ -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));
Expand Down

0 comments on commit c9d0250

Please sign in to comment.