From 2a13c25230033305d98ca71f867c59a8c3b93d05 Mon Sep 17 00:00:00 2001 From: arabian Date: Fri, 2 Jan 2026 02:57:34 +0300 Subject: [PATCH] feat: implemented loop support in pwsp-cli --- src/bin/cli.rs | 6 ++++++ src/types/commands.rs | 29 +++++++++++++++++++++++++++++ src/types/socket.rs | 8 ++++++++ src/utils/commands.rs | 10 ++++++++++ 4 files changed, 53 insertions(+) diff --git a/src/bin/cli.rs b/src/bin/cli.rs index b01849b..3023d39 100644 --- a/src/bin/cli.rs +++ b/src/bin/cli.rs @@ -65,6 +65,8 @@ enum GetCommands { Input, /// All audio inputs Inputs, + /// Is loop enabled (true or false) + Loop, } #[derive(Subcommand, Debug)] @@ -75,6 +77,8 @@ enum SetCommands { Position { position: f32 }, /// Audio input id (see pwsp-cli get inputs) Input { name: String }, + /// Enable or disable loop (true or false) + Loop { enabled: String }, } #[tokio::main] @@ -101,11 +105,13 @@ async fn main() -> Result<(), Box> { GetCommands::CurrentFilePath => Request::get_current_file_path(), GetCommands::Input => Request::get_input(), GetCommands::Inputs => Request::get_inputs(), + GetCommands::Loop => Request::get_loop(), }, Commands::Set { parameter } => match parameter { SetCommands::Volume { volume } => Request::set_volume(volume), SetCommands::Position { position } => Request::seek(position), SetCommands::Input { name } => Request::set_input(&name), + SetCommands::Loop { enabled } => Request::set_loop(&enabled), }, }; diff --git a/src/types/commands.rs b/src/types/commands.rs index d8f3d1a..527b84a 100644 --- a/src/types/commands.rs +++ b/src/types/commands.rs @@ -52,6 +52,12 @@ pub struct SetCurrentInputCommand { pub name: Option, } +pub struct GetLoopCommand {} + +pub struct SetLoopCommand { + pub enabled: Option, +} + #[async_trait] impl Executable for PingCommand { async fn execute(&self) -> Response { @@ -256,3 +262,26 @@ impl Executable for SetCurrentInputCommand { } } } + +#[async_trait] +impl Executable for GetLoopCommand { + async fn execute(&self) -> Response { + let audio_player = get_audio_player().await.lock().await; + Response::new(true, audio_player.looped.to_string()) + } +} + +#[async_trait] +impl Executable for SetLoopCommand { + async fn execute(&self) -> Response { + let mut audio_player = get_audio_player().await.lock().await; + + match self.enabled { + Some(enabled) => { + audio_player.looped = enabled; + Response::new(true, format!("Loop was set to {}", enabled)) + } + None => Response::new(false, "Invalid enabled value"), + } + } +} diff --git a/src/types/socket.rs b/src/types/socket.rs index 62ae61f..3d184d5 100644 --- a/src/types/socket.rs +++ b/src/types/socket.rs @@ -87,6 +87,14 @@ impl Request { pub fn set_input(name: &str) -> Self { Request::new("set_input", vec![("input_name", name)]) } + + pub fn get_loop() -> Self { + Request::new("get_loop", vec![]) + } + + pub fn set_loop(enabled: &str) -> Self { + Request::new("set_loop", vec![("enabled", enabled)]) + } } #[derive(Default, Debug, Clone, Serialize, Deserialize)] diff --git a/src/utils/commands.rs b/src/utils/commands.rs index 8bcbe88..5998c62 100644 --- a/src/utils/commands.rs +++ b/src/utils/commands.rs @@ -48,6 +48,16 @@ pub fn parse_command(request: &Request) -> Option> { let name = Some(request.args.get("input_name").unwrap_or(&String::new())).cloned(); Some(Box::new(SetCurrentInputCommand { name })) } + "get_loop" => Some(Box::new(GetLoopCommand {})), + "set_loop" => { + let enabled = request + .args + .get("enabled") + .unwrap_or(&String::new()) + .parse::() + .ok(); + Some(Box::new(SetLoopCommand { enabled })) + } _ => None, } }