From d4a9cd8b751bed47bf57a93a7ee63054ba43e63b Mon Sep 17 00:00:00 2001 From: Danilo Spinella Date: Wed, 19 Jun 2024 13:00:50 +0200 Subject: [PATCH] feat(cli): Allow quoted arguments --- cli/src/main.rs | 37 ++++++++++++++++++++++++++++++------- 1 file changed, 30 insertions(+), 7 deletions(-) diff --git a/cli/src/main.rs b/cli/src/main.rs index 9a1fa5d..ac45385 100644 --- a/cli/src/main.rs +++ b/cli/src/main.rs @@ -8,10 +8,19 @@ use std::{ use clap::Parser; use serde::Serialize; +use serde_json::to_string; use wpaperd_ipc::{socket_path, IpcError, IpcMessage, IpcResponse}; use crate::opts::{Opts, SubCmd}; +fn unquote(s: String) -> String { + if s.starts_with('"') && s.ends_with('"') { + s.trim_start_matches('"').trim_end_matches('"').to_string() + } else { + s + } +} + fn main() { let args = Opts::parse(); @@ -19,17 +28,31 @@ fn main() { let mut conn = UnixStream::connect(socket_path().unwrap()).unwrap(); let msg = match args.subcmd { - SubCmd::GetWallpaper { monitor } => IpcMessage::CurrentWallpaper { monitor }, + SubCmd::GetWallpaper { monitor } => IpcMessage::CurrentWallpaper { + monitor: unquote(monitor), + }, SubCmd::AllWallpapers { json } => { json_resp = json; IpcMessage::AllWallpapers } - SubCmd::NextWallpaper { monitors } => IpcMessage::NextWallpaper { monitors }, - SubCmd::PreviousWallpaper { monitors } => IpcMessage::PreviousWallpaper { monitors }, - SubCmd::ReloadWallpaper { monitors } => IpcMessage::ReloadWallpaper { monitors }, - SubCmd::PauseWallpaper { monitors } => IpcMessage::PauseWallpaper { monitors }, - SubCmd::ResumeWallpaper { monitors } => IpcMessage::ResumeWallpaper { monitors }, - SubCmd::TogglePauseWallpaper { monitors } => IpcMessage::TogglePauseWallpaper { monitors }, + SubCmd::NextWallpaper { monitors } => IpcMessage::NextWallpaper { + monitors: monitors.into_iter().map(unquote).collect(), + }, + SubCmd::PreviousWallpaper { monitors } => IpcMessage::PreviousWallpaper { + monitors: monitors.into_iter().map(unquote).collect(), + }, + SubCmd::ReloadWallpaper { monitors } => IpcMessage::ReloadWallpaper { + monitors: monitors.into_iter().map(unquote).collect(), + }, + SubCmd::PauseWallpaper { monitors } => IpcMessage::PauseWallpaper { + monitors: monitors.into_iter().map(unquote).collect(), + }, + SubCmd::ResumeWallpaper { monitors } => IpcMessage::ResumeWallpaper { + monitors: monitors.into_iter().map(unquote).collect(), + }, + SubCmd::TogglePauseWallpaper { monitors } => IpcMessage::TogglePauseWallpaper { + monitors: monitors.into_iter().map(unquote).collect(), + }, }; conn.write_all(&serde_json::to_vec(&msg).unwrap()).unwrap(); let mut buf = String::new();