From 49477137425a8ac6cf032739058f29aa2d1f9787 Mon Sep 17 00:00:00 2001 From: Julian T Date: Sat, 18 Feb 2023 00:00:10 +0100 Subject: Add choice command to setwall --- scripts/resc/setwall/src/main.rs | 47 +++++++++++++++++++++++++++++++++++++++- 1 file changed, 46 insertions(+), 1 deletion(-) (limited to 'scripts/resc') diff --git a/scripts/resc/setwall/src/main.rs b/scripts/resc/setwall/src/main.rs index 5ee5e64..6fd9dd7 100644 --- a/scripts/resc/setwall/src/main.rs +++ b/scripts/resc/setwall/src/main.rs @@ -9,6 +9,8 @@ use std::error; use std::path::{PathBuf, Path}; use std::process::Command; use std::fmt; +use std::io; +use std::io::Write; #[derive(Parser, Debug)] #[command(version, about, long_about=None)] @@ -18,6 +20,9 @@ struct Args { #[arg(short, long, help="Reapply last applied wallpaper")] reapply: bool, + + #[arg(short, long, help="Choose a specific wallpaper")] + specific: bool, } #[derive(Debug, Deserialize, Clone, Copy)] @@ -71,6 +76,17 @@ fn main() -> Result<(), Box> { let img = state::load_state()?.ok_or(Box::new(Error::NoState))?; config.reapply_image_file(&img)?; + return Ok(()); + } else if args.specific { + let files = config.get_images()?; + let choices: Vec<&str> = files.iter().map(|i| + i.0.file_name().unwrap().to_str().unwrap() + ).collect(); + if let Some((index, _)) = choice(&choices)? { + config.reapply_image_file(&files[index].0)?; + } else { + println!("No images chosen, doing nothing"); + } return Ok(()); } @@ -82,6 +98,30 @@ fn main() -> Result<(), Box> { } +fn choice<'a, T: fmt::Display>(choices: &'a [T]) -> Result, Box>{ + + for (i, choice) in choices.iter().enumerate() { + println!("{}: {}", i, choice); + } + + print!("Which one?: "); + io::stdout().flush()?; + let mut choice = String::new(); + io::stdin().read_line(&mut choice)?; + choice.pop(); + + if choice == "" || choice == "q" { + return Ok(None) + } + + let choice = choice.parse::()?; + if choice < choices.len() { + Ok(Some((choice, &choices[choice]))) + } else { + Ok(None) + } +} + impl Config { fn parse(file: &Path) -> Result> { let f = File::open(file)?; @@ -114,7 +154,7 @@ impl Config { } } - fn select_image_file(&self) -> Result, Box> { + fn get_images(&self) -> Result, Box> { let folder = Path::new(&self.folder); let rs = self.build_regexset()?; let mut imgs: Vec<(PathBuf, Rule)> = Vec::new(); @@ -128,6 +168,11 @@ impl Config { // deja vu } + Ok(imgs) + } + + fn select_image_file(&self) -> Result, Box> { + let imgs = self.get_images()?; Ok(imgs.choose(&mut thread_rng()).cloned()) } -- cgit v1.2.3