Added random selection

This commit is contained in:
Benjamyn Love 2024-03-10 16:26:06 +11:00
parent 3fc1f271d0
commit e99cbeec6e
5 changed files with 123 additions and 16 deletions

66
Cargo.lock generated
View File

@ -11,18 +11,41 @@ dependencies = [
"memchr", "memchr",
] ]
[[package]]
name = "cfg-if"
version = "1.0.0"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "baf1de4339761588bc0619e3cbc0120ee582ebb74b53b4efbf79117bd2da40fd"
[[package]] [[package]]
name = "configparser" name = "configparser"
version = "3.0.4" version = "3.0.4"
source = "registry+https://github.com/rust-lang/crates.io-index" source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "4ec6d3da8e550377a85339063af6e3735f4b1d9392108da4e083a1b3b9820288" 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]] [[package]]
name = "glob" name = "glob"
version = "0.3.1" version = "0.3.1"
source = "registry+https://github.com/rust-lang/crates.io-index" source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "d2fabcfbdc87f4758337ca535fb41a6d701b65693ce38287d856d1674551ec9b" checksum = "d2fabcfbdc87f4758337ca535fb41a6d701b65693ce38287d856d1674551ec9b"
[[package]]
name = "libc"
version = "0.2.153"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "9c198f91728a82281a64e1f4f9eeb25d82cb32a5de251c6bd1b5154d63a8e7bd"
[[package]] [[package]]
name = "memchr" name = "memchr"
version = "2.7.1" version = "2.7.1"
@ -35,9 +58,46 @@ version = "0.1.0"
dependencies = [ dependencies = [
"configparser", "configparser",
"glob", "glob",
"rand",
"untildify", "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]] [[package]]
name = "regex" name = "regex"
version = "1.10.3" version = "1.10.3"
@ -73,3 +133,9 @@ version = "0.1.1"
dependencies = [ dependencies = [
"regex", "regex",
] ]
[[package]]
name = "wasi"
version = "0.11.0+wasi-snapshot-preview1"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "9c8d87e72b64a3b4db28d11ce29237c246188f4f51057d65a7eab63b7987e423"

View File

@ -6,4 +6,5 @@ edition = "2021"
[dependencies] [dependencies]
configparser = "3.0.4" configparser = "3.0.4"
glob = "0.3.1" glob = "0.3.1"
rand = "0.8.5"
untildify = { path = "../untildify" } untildify = { path = "../untildify" }

View File

@ -51,12 +51,18 @@ impl fmt::Display for MonitorType {
} }
#[derive(Debug)] #[derive(Debug)]
enum ImageTypes { pub enum ImageTypes {
PNG, PNG,
JPG, JPG,
JPEG, JPEG,
} }
impl ImageTypes {
pub fn get_types() -> Vec<String> {
vec!["png".to_string(), "jpg".to_string(), "jpeg".to_string()]
}
}
impl fmt::Display for ImageTypes { impl fmt::Display for ImageTypes {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result { fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
write!(f, "{:?}", self) write!(f, "{:?}", self)

View File

@ -1,5 +1,8 @@
use core::fmt; use core::fmt;
use glob::glob; use glob::glob;
use rand::seq::SliceRandom;
use std::fmt::Write as _;
use std::io::Write as _;
use std::process::exit; use std::process::exit;
use crate::config::*; use crate::config::*;
@ -42,17 +45,34 @@ impl Wallpapers {
} }
} }
// Todo implement a random selector for a given key, like `wallpapers.random_selection(MonitorType::Ultrawide)` // 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<Wallpaper> // Todo implement a function to load all files of particular filetypes into the Vec<Wallpaper>
pub fn load_images(&mut self, monitor_type: MonitorType) { pub fn load_images(&mut self, monitor_type: MonitorType) {
let mut path = self let path = self
.config .config
.wallpaper_dir .wallpaper_dir
.get(monitor_type.clone()) .get(monitor_type.clone())
.to_owned(); .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() { for entry in glob(&tmp_path).unwrap() {
match entry { match entry {
Ok(path) => { Ok(path) => {
self.add(monitor_type.clone(), path.display().to_string()); self.add(monitor_type.clone(), path.display().to_string());
@ -63,6 +83,7 @@ impl Wallpapers {
}; };
} }
} }
}
fn add(&mut self, monitor_type: MonitorType, filepath: String) { fn add(&mut self, monitor_type: MonitorType, filepath: String) {
match monitor_type { match monitor_type {
@ -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 { impl fmt::Display for Wallpapers {
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result { fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
writeln!(f, "Wallpapers discovered")?; writeln!(f, "Wallpapers discovered")?;
for v in &self.ultrawide { 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, "") writeln!(f, "")
} }

View File

@ -8,6 +8,7 @@ use crate::files::*;
fn main() { fn main() {
let mut wallpapers = Wallpapers::new(); let mut wallpapers = Wallpapers::new();
wallpapers.load_images(MonitorType::Ultrawide); wallpapers.load_all();
println!("{}", wallpapers); wallpapers.random_selection(MonitorType::Ultrawide);
println!("{}", "");
} }