Added random selection

This commit is contained in:
benjamyn committed 2024-03-10 16:26:06 +11:00
1 parent 3fc1f271d0
commit e99cbeec6e
5 files changed
+123 -16

No files matched your search

+7 -1
View File
@@ -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<String> {
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)
+46 -13
View File
@@ -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<Wallpaper>
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, "")
}
+3 -2
View File
@@ -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!("{}", "");
}