diff --git a/Cargo.lock b/Cargo.lock index 6490866..a49352e 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -11,18 +11,41 @@ dependencies = [ "memchr", ] +[[package]] +name = "cfg-if" +version = "1.0.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "baf1de4339761588bc0619e3cbc0120ee582ebb74b53b4efbf79117bd2da40fd" + [[package]] name = "configparser" version = "3.0.4" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "4ec6d3da8e550377a85339063af6e3735f4b1d9392108da4e083a1b3b9820288" +[[package]] +name = "getrandom" +version = "0.2.12" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "190092ea657667030ac6a35e305e62fc4dd69fd98ac98631e5d3a2b1575a12b5" +dependencies = [ + "cfg-if", + "libc", + "wasi", +] + [[package]] name = "glob" version = "0.3.1" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "d2fabcfbdc87f4758337ca535fb41a6d701b65693ce38287d856d1674551ec9b" +[[package]] +name = "libc" +version = "0.2.153" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "9c198f91728a82281a64e1f4f9eeb25d82cb32a5de251c6bd1b5154d63a8e7bd" + [[package]] name = "memchr" version = "2.7.1" @@ -35,9 +58,46 @@ version = "0.1.0" dependencies = [ "configparser", "glob", + "rand", "untildify", ] +[[package]] +name = "ppv-lite86" +version = "0.2.17" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "5b40af805b3121feab8a3c29f04d8ad262fa8e0561883e7653e024ae4479e6de" + +[[package]] +name = "rand" +version = "0.8.5" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "34af8d1a0e25924bc5b7c43c079c942339d8f0a8b57c39049bef581b46327404" +dependencies = [ + "libc", + "rand_chacha", + "rand_core", +] + +[[package]] +name = "rand_chacha" +version = "0.3.1" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "e6c10a63a0fa32252be49d21e7709d4d4baf8d231c2dbce1eaa8141b9b127d88" +dependencies = [ + "ppv-lite86", + "rand_core", +] + +[[package]] +name = "rand_core" +version = "0.6.4" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "ec0be4795e2f6a28069bec0b5ff3e2ac9bafc99e6a9a7dc3547996c5c816922c" +dependencies = [ + "getrandom", +] + [[package]] name = "regex" version = "1.10.3" @@ -73,3 +133,9 @@ version = "0.1.1" dependencies = [ "regex", ] + +[[package]] +name = "wasi" +version = "0.11.0+wasi-snapshot-preview1" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "9c8d87e72b64a3b4db28d11ce29237c246188f4f51057d65a7eab63b7987e423" diff --git a/Cargo.toml b/Cargo.toml index 1d4ac5e..bad2c32 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -6,4 +6,5 @@ edition = "2021" [dependencies] configparser = "3.0.4" glob = "0.3.1" +rand = "0.8.5" untildify = { path = "../untildify" } diff --git a/src/enums.rs b/src/enums.rs index e64d2b3..df78926 100644 --- a/src/enums.rs +++ b/src/enums.rs @@ -51,12 +51,18 @@ impl fmt::Display for MonitorType { } #[derive(Debug)] -enum ImageTypes { +pub enum ImageTypes { PNG, JPG, JPEG, } +impl ImageTypes { + pub fn get_types() -> Vec { + vec!["png".to_string(), "jpg".to_string(), "jpeg".to_string()] + } +} + impl fmt::Display for ImageTypes { fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result { write!(f, "{:?}", self) diff --git a/src/files.rs b/src/files.rs index 28752b3..a54bf24 100644 --- a/src/files.rs +++ b/src/files.rs @@ -1,5 +1,8 @@ use core::fmt; use glob::glob; +use rand::seq::SliceRandom; +use std::fmt::Write as _; +use std::io::Write as _; use std::process::exit; use crate::config::*; @@ -42,25 +45,43 @@ impl Wallpapers { } } // Todo implement a random selector for a given key, like `wallpapers.random_selection(MonitorType::Ultrawide)` - pub fn random_selection(monitor_type: MonitorType) {} + pub fn random_selection(&self, monitor_type: MonitorType) { + match monitor_type { + MonitorType::Ultrawide => { + println!("{:?}", self.ultrawide.choose(&mut rand::thread_rng())); + } + MonitorType::Horizontal => { + println!("{:?}", self.horizontal.choose(&mut rand::thread_rng())); + } + MonitorType::Vertical => { + println!("{:?}", self.vertical.choose(&mut rand::thread_rng())); + } + _ => {} + } + } // Todo implement a function to load all files of particular filetypes into the Vec pub fn load_images(&mut self, monitor_type: MonitorType) { - let mut path = self + let path = self .config .wallpaper_dir .get(monitor_type.clone()) .to_owned(); - path.push_str("/**/*.png"); + for filetype in ImageTypes::get_types().iter() { + let mut tmp_path = path.clone(); + let mut tmp_glob = String::new(); + write!(&mut tmp_glob, "/**/*.{}", filetype).unwrap(); + tmp_path.push_str(&tmp_glob); - for entry in glob(&path).unwrap() { - match entry { - Ok(path) => { - self.add(monitor_type.clone(), path.display().to_string()); - } - Err(e) => { - println!("{}", e); - } - }; + for entry in glob(&tmp_path).unwrap() { + match entry { + Ok(path) => { + self.add(monitor_type.clone(), path.display().to_string()); + } + Err(e) => { + println!("{}", e); + } + }; + } } } @@ -72,13 +93,25 @@ impl Wallpapers { _ => {} } } + + pub fn load_all(&mut self) { + self.load_images(MonitorType::Ultrawide); + self.load_images(MonitorType::Horizontal); + self.load_images(MonitorType::Vertical); + } } impl fmt::Display for Wallpapers { fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result { writeln!(f, "Wallpapers discovered")?; for v in &self.ultrawide { - writeln!(f, "{:?}", v)?; + writeln!(f, "Ultrawide: {:?}", v)?; + } + for v in &self.horizontal { + writeln!(f, "Horizontal: {:?}", v)?; + } + for v in &self.vertical { + writeln!(f, "Vertical: {:?}", v)?; } writeln!(f, "") } diff --git a/src/main.rs b/src/main.rs index 32da1e9..02ff5d0 100644 --- a/src/main.rs +++ b/src/main.rs @@ -8,6 +8,7 @@ use crate::files::*; fn main() { let mut wallpapers = Wallpapers::new(); - wallpapers.load_images(MonitorType::Ultrawide); - println!("{}", wallpapers); + wallpapers.load_all(); + wallpapers.random_selection(MonitorType::Ultrawide); + println!("{}", ""); }