Skip to content

Commit

Permalink
feat: add load command
Browse files Browse the repository at this point in the history
Relates to #16
  • Loading branch information
johnallen3d committed Sep 26, 2023
1 parent ef33d92 commit c16c40b
Show file tree
Hide file tree
Showing 3 changed files with 40 additions and 0 deletions.
3 changes: 3 additions & 0 deletions src/args.rs
Original file line number Diff line number Diff line change
Expand Up @@ -95,6 +95,9 @@ pub(crate) enum Commands {
/// List all of the playlists
#[command()]
Lsplaylists,
/// Load a playlist into the queue (optionally provide a range)
#[command()]
Load { name: String, range: Option<String> },
/// List songs in a playlist
#[command()]
Playlist { name: Option<String> },
Expand Down
36 changes: 36 additions & 0 deletions src/client.rs
Original file line number Diff line number Diff line change
Expand Up @@ -366,6 +366,42 @@ impl Client {
Ok(Some(response))
}

pub fn load(
&mut self,
name: &String,
range: Option<String>,
) -> eyre::Result<Option<String>> {
match range {
Some(range_str) => {
let parts: Vec<u32> = range_str
.split(':')
.filter_map(|s| s.parse().ok())
.collect();

match parts.as_slice() {
[start, end] if start < end && *start > 0 && *end > 0 => {
self.client.load(name, *start..*end)?;
}
[start, end] if start >= end => {
return Err(eyre::eyre!(
"end cannot be less than or equal to start"
));
}
_ => {
return Err(eyre::eyre!(
"invalid range, should be 'start:end' where start and end > 0."
));
}
}
}
None => {
self.client.load(name, ..)?;
}
}

Ok(Some(format!("loading: {name}")))
}

pub fn playlist(
&mut self,
name: Option<String>,
Expand Down
1 change: 1 addition & 0 deletions src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -54,6 +54,7 @@ fn main() {
Some(Commands::Queued) => mpd.queued(),
Some(Commands::Shuffle) => mpd.shuffle(),
Some(Commands::Lsplaylists) => mpd.lsplaylists(),
Some(Commands::Load { name, range }) => mpd.load(&name, range),
Some(Commands::Playlist { name }) => mpd.playlist(name),
Some(Commands::Listall { file }) => mpd.listall(file.as_deref()),
Some(Commands::Ls { directory }) => mpd.ls(directory.as_deref()),
Expand Down

0 comments on commit c16c40b

Please sign in to comment.