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)]
enum Commands {
/// Perform an action (ping, pause, resume, stop, play)
/// Perform an action (ping, pause, resume, toggle-pause, stop, play)
Action {
#[clap(subcommand)]
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 {
#[clap(subcommand)]
parameter: GetCommands,
},
/// Set information in the player (volume, position)
/// Set information in the player (volume, position, input)
Set {
#[clap(subcommand)]
parameter: SetCommands,
@@ -39,6 +39,8 @@ enum Actions {
Pause,
/// Resume audio playback
Resume,
/// Toggle pause
TogglePause,
/// Stop audio playback and clear the queue
Stop,
/// Play a file
@@ -51,11 +53,11 @@ enum GetCommands {
IsPaused,
/// Playback volume
Volume,
/// Playback position
/// Playback position (in seconds)
Position,
/// Duration of the current file
Duration,
/// Player state
/// Player state (Playing, Paused or Stopped)
State,
/// Current playing file path
CurrentFilePath,
@@ -69,9 +71,9 @@ enum GetCommands {
enum SetCommands {
/// Playback volume
Volume { volume: f32 },
/// Playback position
/// Playback position (in seconds)
Position { position: f32 },
/// Input
/// Audio input id (see pwsp-cli get inputs)
Input { name: String },
}
@@ -86,6 +88,7 @@ async fn main() -> Result<(), Box<dyn Error>> {
Actions::Ping => Request::ping(),
Actions::Pause => Request::pause(),
Actions::Resume => Request::resume(),
Actions::TogglePause => Request::toggle_pause(),
Actions::Stop => Request::stop(),
Actions::Play { file_path } => Request::play(file_path.to_str().unwrap()),
},
+22 -1
View File
@@ -1,5 +1,5 @@
use crate::{
types::socket::Response,
types::{audio_player::PlayerState, socket::Response},
utils::{daemon::get_audio_player, pipewire::get_all_devices},
};
use async_trait::async_trait;
@@ -16,6 +16,8 @@ pub struct PauseCommand {}
pub struct ResumeCommand {}
pub struct TogglePauseCommand {}
pub struct StopCommand {}
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]
impl Executable for StopCommand {
async fn execute(&self) -> Response {
+4
View File
@@ -32,6 +32,10 @@ impl Request {
Request::new("resume", vec![])
}
pub fn toggle_pause() -> Self {
Request::new("toggle_pause", vec![])
}
pub fn stop() -> Self {
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 {})),
"pause" => Some(Box::new(PauseCommand {})),
"resume" => Some(Box::new(ResumeCommand {})),
"toggle_pause" => Some(Box::new(TogglePauseCommand {})),
"stop" => Some(Box::new(StopCommand {})),
"is_paused" => Some(Box::new(IsPausedCommand {})),
"get_state" => Some(Box::new(GetStateCommand {})),