add toggle-pause command to pwsp-cli

This commit is contained in:
2025-12-16 20:30:34 +03:00
parent e8d1947f18
commit 546b6fd13f
4 changed files with 37 additions and 8 deletions
+10 -7
View File
@@ -14,17 +14,17 @@ struct Cli {
#[derive(Subcommand, Debug)] #[derive(Subcommand, Debug)]
enum Commands { enum Commands {
/// Perform an action (ping, pause, resume, stop, play) /// Perform an action (ping, pause, resume, toggle-pause, stop, play)
Action { Action {
#[clap(subcommand)] #[clap(subcommand)]
action: Actions, action: Actions,
}, },
/// Get information from the player (is paused, volume, position, state) /// Get information from the player (is paused, volume, position, duration, state, current-file-path, input, inputs)
Get { Get {
#[clap(subcommand)] #[clap(subcommand)]
parameter: GetCommands, parameter: GetCommands,
}, },
/// Set information in the player (volume, position) /// Set information in the player (volume, position, input)
Set { Set {
#[clap(subcommand)] #[clap(subcommand)]
parameter: SetCommands, parameter: SetCommands,
@@ -39,6 +39,8 @@ enum Actions {
Pause, Pause,
/// Resume audio playback /// Resume audio playback
Resume, Resume,
/// Toggle pause
TogglePause,
/// Stop audio playback and clear the queue /// Stop audio playback and clear the queue
Stop, Stop,
/// Play a file /// Play a file
@@ -51,11 +53,11 @@ enum GetCommands {
IsPaused, IsPaused,
/// Playback volume /// Playback volume
Volume, Volume,
/// Playback position /// Playback position (in seconds)
Position, Position,
/// Duration of the current file /// Duration of the current file
Duration, Duration,
/// Player state /// Player state (Playing, Paused or Stopped)
State, State,
/// Current playing file path /// Current playing file path
CurrentFilePath, CurrentFilePath,
@@ -69,9 +71,9 @@ enum GetCommands {
enum SetCommands { enum SetCommands {
/// Playback volume /// Playback volume
Volume { volume: f32 }, Volume { volume: f32 },
/// Playback position /// Playback position (in seconds)
Position { position: f32 }, Position { position: f32 },
/// Input /// Audio input id (see pwsp-cli get inputs)
Input { name: String }, Input { name: String },
} }
@@ -86,6 +88,7 @@ async fn main() -> Result<(), Box<dyn Error>> {
Actions::Ping => Request::ping(), Actions::Ping => Request::ping(),
Actions::Pause => Request::pause(), Actions::Pause => Request::pause(),
Actions::Resume => Request::resume(), Actions::Resume => Request::resume(),
Actions::TogglePause => Request::toggle_pause(),
Actions::Stop => Request::stop(), Actions::Stop => Request::stop(),
Actions::Play { file_path } => Request::play(file_path.to_str().unwrap()), Actions::Play { file_path } => Request::play(file_path.to_str().unwrap()),
}, },
+22 -1
View File
@@ -1,5 +1,5 @@
use crate::{ use crate::{
types::socket::Response, types::{audio_player::PlayerState, socket::Response},
utils::{daemon::get_audio_player, pipewire::get_all_devices}, utils::{daemon::get_audio_player, pipewire::get_all_devices},
}; };
use async_trait::async_trait; use async_trait::async_trait;
@@ -16,6 +16,8 @@ pub struct PauseCommand {}
pub struct ResumeCommand {} pub struct ResumeCommand {}
pub struct TogglePauseCommand {}
pub struct StopCommand {} pub struct StopCommand {}
pub struct IsPausedCommand {} pub struct IsPausedCommand {}
@@ -75,6 +77,25 @@ impl Executable for ResumeCommand {
} }
} }
#[async_trait]
impl Executable for TogglePauseCommand {
async fn execute(&self) -> Response {
let mut audio_player = get_audio_player().await.lock().await;
if audio_player.get_state() == PlayerState::Stopped {
return Response::new(false, "Audio is not playing");
}
if audio_player.is_paused() {
audio_player.resume();
Response::new(true, "Audio was resumed")
} else {
audio_player.pause();
Response::new(true, "Audio was paused")
}
}
}
#[async_trait] #[async_trait]
impl Executable for StopCommand { impl Executable for StopCommand {
async fn execute(&self) -> Response { async fn execute(&self) -> Response {
+4
View File
@@ -32,6 +32,10 @@ impl Request {
Request::new("resume", vec![]) Request::new("resume", vec![])
} }
pub fn toggle_pause() -> Self {
Request::new("toggle_pause", vec![])
}
pub fn stop() -> Self { pub fn stop() -> Self {
Request::new("stop", vec![]) Request::new("stop", vec![])
} }
+1
View File
@@ -7,6 +7,7 @@ pub fn parse_command(request: &Request) -> Option<Box<dyn Executable + Send>> {
"ping" => Some(Box::new(PingCommand {})), "ping" => Some(Box::new(PingCommand {})),
"pause" => Some(Box::new(PauseCommand {})), "pause" => Some(Box::new(PauseCommand {})),
"resume" => Some(Box::new(ResumeCommand {})), "resume" => Some(Box::new(ResumeCommand {})),
"toggle_pause" => Some(Box::new(TogglePauseCommand {})),
"stop" => Some(Box::new(StopCommand {})), "stop" => Some(Box::new(StopCommand {})),
"is_paused" => Some(Box::new(IsPausedCommand {})), "is_paused" => Some(Box::new(IsPausedCommand {})),
"get_state" => Some(Box::new(GetStateCommand {})), "get_state" => Some(Box::new(GetStateCommand {})),