diff --git a/src/main.rs b/src/main.rs index 25d37233..8cc061c8 100644 --- a/src/main.rs +++ b/src/main.rs @@ -2,10 +2,9 @@ use clap::{Parser, Subcommand}; use serde_json::json; use std::io::{self}; use std::path::PathBuf; +use tokio::io::{AsyncReadExt, AsyncWriteExt}; use tokio::net::UnixStream; -use tokio::io::{AsyncWriteExt, AsyncReadExt}; -use tracing::{debug, info, error}; -use tracing_subscriber; +use tracing::{debug, error, info}; #[derive(Parser)] #[command(author, version, about)] @@ -35,8 +34,14 @@ enum CommandOptions { } const SOCKET_PATH: &str = "/tmp/mpvsocket"; -async fn send_ipc_command(command: &str, args: &[serde_json::Value]) -> io::Result> { - debug!("Sending IPC command: {} with arguments: {:?}", command, args); +async fn send_ipc_command( + command: &str, + args: &[serde_json::Value], +) -> io::Result> { + debug!( + "Sending IPC command: {} with arguments: {:?}", + command, args + ); match UnixStream::connect(SOCKET_PATH).await { Ok(mut socket) => { @@ -99,30 +104,37 @@ async fn main() -> io::Result<()> { info!("Unpausing playback"); send_ipc_command("set_property", &[json!("pause"), json!(false)]).await?; } + CommandOptions::Pause => { info!("Pausing playback"); send_ipc_command("set_property", &[json!("pause"), json!(true)]).await?; } + CommandOptions::Stop => { info!("Stopping playback and quitting MPV"); send_ipc_command("quit", &[]).await?; } + CommandOptions::Next => { info!("Skipping to next item in the playlist"); send_ipc_command("playlist-next", &[]).await?; } + CommandOptions::Prev => { info!("Skipping to previous item in the playlist"); send_ipc_command("playlist-prev", &[]).await?; } + CommandOptions::Seek { seconds } => { info!("Seeking to {} seconds", seconds); send_ipc_command("seek", &[json!(seconds)]).await?; } + CommandOptions::Move { index1, index2 } => { info!("Moving item from index {} to {}", index1, index2); send_ipc_command("playlist-move", &[json!(index1), json!(index2)]).await?; } + CommandOptions::Remove { index } => { if let Some(idx) = index { info!("Removing item at index {}", idx); @@ -132,22 +144,32 @@ async fn main() -> io::Result<()> { send_ipc_command("playlist-remove", &[json!("current")]).await?; } } + CommandOptions::Clear => { info!("Clearing the playlist"); send_ipc_command("playlist-clear", &[]).await?; } + CommandOptions::List => { info!("Listing playlist items"); if let Some(data) = send_ipc_command("get_property", &[json!("playlist")]).await? { println!("{}", serde_json::to_string_pretty(&data)?); } } + CommandOptions::Add { filenames } => { + if filenames.is_empty() { + let e = "No files provided to add to the playlist"; + error!("{}", e); + return Err(io::Error::new(io::ErrorKind::InvalidInput, e)); + } + info!("Adding {} files to the playlist", filenames.len()); for filename in filenames { send_ipc_command("loadfile", &[json!(filename), json!("append-play")]).await?; } } + CommandOptions::Replace { filenames } => { info!("Replacing current playlist with {} files", filenames.len()); if let Some(first_file) = filenames.first() { @@ -157,6 +179,7 @@ async fn main() -> io::Result<()> { } } } + CommandOptions::Prop { properties } => { info!("Fetching properties: {:?}", properties); for property in properties {