diff --git a/src/command.rs b/src/command.rs index 041f841..e145e31 100644 --- a/src/command.rs +++ b/src/command.rs @@ -1,4 +1,4 @@ -use std::{fmt::Display, path::PathBuf, time::Duration}; +use std::{fmt::{format, Display}, path::PathBuf, time::Duration}; use clap::{Args, Subcommand}; @@ -18,6 +18,8 @@ pub enum Command { /// Display the fallback wallpaper /// If called again displays the previous image Fallback, + /// Change the directory from which images are sourced + WpDir(WallpaperDirectory), /// Set the interval for new images in seconds Interval(IntervalDuration), /// Query information about the current state @@ -32,6 +34,11 @@ pub struct IntervalDuration { pub duration: Duration, } +#[derive(Args, PartialEq, Eq)] +pub struct WallpaperDirectory { + pub path: PathBuf, +} + #[derive(Subcommand, PartialEq, Eq)] pub enum ModeArgs { Linear, @@ -50,6 +57,7 @@ pub enum GetArgs { Duration, Mode, Fallback, + WpDir, } fn parse_duration(arg: &str) -> Result { @@ -81,8 +89,12 @@ impl Display for Command { GetArgs::Duration => "get duration".to_string(), GetArgs::Mode => "get mode".to_string(), GetArgs::Fallback => "get fallback".to_string(), + GetArgs::WpDir => "get wp-dir".to_string(), }, Command::Daemon(_) => "daemon".to_string(), + Command::WpDir(wallpaper_directory) => { + format!("wp-dir {}", wallpaper_directory.path.to_str().unwrap().to_owned()) + } }; write!(f, "{args}") } diff --git a/src/daemon.rs b/src/daemon.rs index 02c3d73..67c1a64 100644 --- a/src/daemon.rs +++ b/src/daemon.rs @@ -178,8 +178,7 @@ pub fn start_daemon(args: DaemonArgs) { let config = if config_file.is_file() { let config = fs::read_to_string(config_file).expect("Couldn't read config file"); // Can't fail - let config = Config::from_str(&config).unwrap(); - config + Config::from_str(&config).unwrap() } else { Config::default() }; @@ -355,9 +354,13 @@ fn handle_connection(mut stream: UnixStream, state: Arc>) -> bool { } } GetArgs::Fallback => state.lock().unwrap().get_fallback().to_string(), + GetArgs::WpDir => state.lock().unwrap().get_image_dir().to_str().unwrap().to_owned(), } } Command::Daemon(_) => todo!(), + Command::WpDir(wallpaper_directory) => { + state.lock().unwrap().set_image_dir(wallpaper_directory.path); + } } stream.write_all(response.as_bytes()).unwrap(); diff --git a/src/state.rs b/src/state.rs index 808f7a4..9993d70 100644 --- a/src/state.rs +++ b/src/state.rs @@ -271,6 +271,15 @@ impl State { } } + pub fn set_image_dir(&mut self, dir: PathBuf) { + self.image_dir = dir; + self.update().unwrap(); + } + + pub fn get_image_dir(&self) -> &PathBuf { + &self.image_dir + } + pub fn get_current_image(&self) -> &PathBuf { self.history.previous.back().unwrap() }