Skip to content

Commit

Permalink
feat: add next, previous, and seek functionality (#4)
Browse files Browse the repository at this point in the history
* Implement next, prev, and seek functionality

This adds messages to skip to the next and previous sonds in the queue as well
as seeking forward or backwards by 5s. No default keybindings are provided.

* Implement seek_seconds option

Allows modifying the time in seconds seek skips in the config file. Defaults to
5 seconds if not set.

* Add into about new functions to CONFIGUTATION.md

---------

Co-authored-by: Ranmaru <alex@tsubame>
  • Loading branch information
ranmaru22 and Ranmaru authored Sep 25, 2024
1 parent 6080d2f commit 69c182a
Show file tree
Hide file tree
Showing 4 changed files with 67 additions and 2 deletions.
8 changes: 6 additions & 2 deletions CONFIGURATION.md
Original file line number Diff line number Diff line change
Expand Up @@ -77,6 +77,10 @@ multiple keybinds.
| `left` | move left | `<left>` | d | h |
| `right` | move right | `<right>` | n | l |
| `toggle_playpause` | toggles between play and pause | p | | |
| `next song` | jumps to the next song in the queue | | | |
| `previous song` | jumps to the previous song in the queue | | | |
| `seek` | seeks forward by 5 seconds | | | |
| `seek_backwards` | seeks backwards by 5 seconds | | | |
| `select` | act on the selected entry | `<enter>` | | |
| `quit` | close the program | q | | |
| `switch_to_library` | switch to library screen | 1 | | |
Expand All @@ -93,8 +97,8 @@ multiple keybinds.
| `toggle_single` | toggle single | s | | |
| `toggle_consume` | toggle consume | c | | |
| `toggle_random` | toggle random | z | | |
| `top` | jump to top | `<home>` | < | g g |
| `bottom` | jump to bottom | `<end>` | > | G |
| `top` | jump to top | `<home>` | < | g g |
| `bottom` | jump to bottom | `<end>` | > | G |

Note that the dvorak/qwerty sets *do not* delete the default
keybindings.
Expand Down
5 changes: 5 additions & 0 deletions src/config.rs
Original file line number Diff line number Diff line change
Expand Up @@ -12,13 +12,15 @@ use keybind::{get_message, KeybindMap};
pub struct Config {
pub keybindings: KeybindMap,
pub theme: Theme,
pub seek_seconds: i64,
}

impl Config {
pub fn default() -> Self {
Config {
keybindings: KeybindMap::default(),
theme: Theme::new(),
seek_seconds: 5,
}
}
pub fn try_read_config(mut self) -> Self {
Expand All @@ -31,6 +33,9 @@ impl Config {
for (key, value) in toml {
match (key.as_str(), value) {
("keybindings", Value::Table(t)) => self.read_keybinds(t),
("seek_seconds", Value::Integer(k)) if k > 0 => {
self.seek_seconds = k
}
("theme", Value::Table(t)) => self.read_theme(t),
("dvorak_keybindings", Value::Boolean(true)) => {
self.keybindings = self.keybindings.with_dvorak_style();
Expand Down
4 changes: 4 additions & 0 deletions src/config/keybind.rs
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,10 @@ pub fn get_message(s: &str) -> Option<Message> {
"toggle_single" => Some(Message::Set(Toggle::Single)),
"toggle_consume" => Some(Message::Set(Toggle::Consume)),
"toggle_random" => Some(Message::Set(Toggle::Random)),
"next_song" => Some(Message::NextSong),
"previous_song" => Some(Message::PreviousSong),
"seek" => Some(Message::Seek(SeekDirection::Forward)),
"seek_backwards" => Some(Message::Seek(SeekDirection::Backward)),
_ => None,
}
}
Expand Down
52 changes: 52 additions & 0 deletions src/update.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,10 @@ use crate::model::proto::Searchable;
use crate::model::{Model, Screen, State};
use crate::util::{safe_decrement, safe_increment};
use bitflags::bitflags;
use mpd::status::State as PlayState;
use ratatui::crossterm::event::{self, KeyCode, KeyEvent};
use std::option::Option;
use std::time::Duration;

pub mod build_library;
mod handlers;
Expand Down Expand Up @@ -55,10 +57,19 @@ pub enum Toggle {
Consume,
}

#[derive(PartialEq, Clone, Debug)]
pub enum SeekDirection {
Forward,
Backward,
}

#[derive(Clone, Debug)]
pub enum Message {
Direction(Dirs),
PlayPause,
NextSong,
PreviousSong,
Seek(SeekDirection),
Select,
SwitchState(State),
SwitchScreen(Screen),
Expand Down Expand Up @@ -169,6 +180,47 @@ pub fn handle_msg(model: &mut Model, m: Message) -> Result<Update> {
model.conn.toggle_pause()?;
Ok(Update::STATUS)
}
Message::NextSong => match model.status.state {
PlayState::Stop => Ok(Update::empty()),
_ => {
model.conn.next()?;
Ok(Update::CURRENT_SONG | Update::STATUS)
}
},
Message::PreviousSong => match model.status.state {
PlayState::Stop => Ok(Update::empty()),
_ => {
model.conn.prev()?;
Ok(Update::CURRENT_SONG | Update::STATUS)
}
},
Message::Seek(direction) => {
let mut update_flags = Update::empty();

if let (Some((current_pos, total)), Some(queue_pos)) =
(model.status.time, model.status.song)
{
let delta = Duration::from_secs(
model.config.seek_seconds.unsigned_abs(),
);

let new_pos = if direction == SeekDirection::Backward {
(current_pos.checked_sub(delta))
.unwrap_or(Duration::default())
} else {
(current_pos.checked_add(delta)).unwrap_or(total).min(total)
};

if new_pos >= total {
model.conn.next()?;
update_flags |= Update::CURRENT_SONG | Update::STATUS;
} else {
model.conn.seek(queue_pos.pos, new_pos)?;
update_flags |= Update::STATUS;
}
}
Ok(update_flags)
}
Message::Set(t) => {
match t {
Toggle::Repeat => model.conn.repeat(!model.status.repeat),
Expand Down

0 comments on commit 69c182a

Please sign in to comment.