Skip to content

Commit

Permalink
feat(cli): Allow quoted arguments
Browse files Browse the repository at this point in the history
  • Loading branch information
danyspin97 committed Jun 19, 2024
1 parent f17e7dc commit d4a9cd8
Showing 1 changed file with 30 additions and 7 deletions.
37 changes: 30 additions & 7 deletions cli/src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -8,28 +8,51 @@ use std::{

use clap::Parser;
use serde::Serialize;
use serde_json::to_string;

Check warning on line 11 in cli/src/main.rs

View workflow job for this annotation

GitHub Actions / Check

unused import: `serde_json::to_string`

Check warning on line 11 in cli/src/main.rs

View workflow job for this annotation

GitHub Actions / Test Suite

unused import: `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();

let mut json_resp = false;

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();
Expand Down

0 comments on commit d4a9cd8

Please sign in to comment.