104 lines
3.1 KiB
Rust
104 lines
3.1 KiB
Rust
use core::fmt;
|
|
use glob::glob;
|
|
use rand::seq::SliceRandom;
|
|
use std::fmt::Write as _;
|
|
|
|
use crate::config::*;
|
|
use crate::enums::*;
|
|
|
|
#[derive(Debug, Clone)]
|
|
pub struct Wallpaper {
|
|
path: String,
|
|
}
|
|
|
|
impl fmt::Display for Wallpaper {
|
|
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
|
|
write!(f, "{}", self.path)
|
|
}
|
|
}
|
|
|
|
#[derive(Debug, Clone)]
|
|
pub struct Wallpapers {
|
|
ultrawide: Vec<Wallpaper>,
|
|
horizontal: Vec<Wallpaper>,
|
|
vertical: Vec<Wallpaper>,
|
|
pub config: Config,
|
|
}
|
|
|
|
impl Wallpapers {
|
|
pub fn new(config: Config) -> Wallpapers {
|
|
Wallpapers {
|
|
ultrawide: vec![],
|
|
horizontal: vec![],
|
|
vertical: vec![],
|
|
config,
|
|
}
|
|
}
|
|
|
|
pub fn random_selection(&self, monitor_type: &MonitorType) -> &Wallpaper {
|
|
match monitor_type {
|
|
MonitorType::Ultrawide => self.ultrawide.choose(&mut rand::thread_rng()).unwrap(),
|
|
MonitorType::Horizontal => self.horizontal.choose(&mut rand::thread_rng()).unwrap(),
|
|
MonitorType::Vertical => self.vertical.choose(&mut rand::thread_rng()).unwrap(),
|
|
_ => self.horizontal.choose(&mut rand::thread_rng()).unwrap(),
|
|
}
|
|
}
|
|
|
|
pub fn load_images(&mut self, monitor_type: MonitorType) {
|
|
let path = self
|
|
.config
|
|
.wallpaper_dir
|
|
.get(monitor_type.clone())
|
|
.to_owned();
|
|
let types = vec!["png".to_string(), "jpg".to_string(), "jpeg".to_string()];
|
|
for filetype in 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(&tmp_path).unwrap() {
|
|
match entry {
|
|
Ok(path) => {
|
|
self.add(monitor_type.clone(), path.display().to_string());
|
|
}
|
|
Err(e) => {
|
|
println!("{}", e);
|
|
}
|
|
};
|
|
}
|
|
}
|
|
}
|
|
|
|
fn add(&mut self, monitor_type: MonitorType, filepath: String) {
|
|
match monitor_type {
|
|
MonitorType::Ultrawide => self.ultrawide.push(Wallpaper { path: filepath }),
|
|
MonitorType::Horizontal => self.horizontal.push(Wallpaper { path: filepath }),
|
|
MonitorType::Vertical => self.vertical.push(Wallpaper { path: filepath }),
|
|
MonitorType::Error => self.horizontal.push(Wallpaper { path: filepath }),
|
|
}
|
|
}
|
|
|
|
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, "Ultrawide: {:?}", v)?;
|
|
}
|
|
for v in &self.horizontal {
|
|
writeln!(f, "Horizontal: {:?}", v)?;
|
|
}
|
|
for v in &self.vertical {
|
|
writeln!(f, "Vertical: {:?}", v)?;
|
|
}
|
|
writeln!(f, "")
|
|
}
|
|
}
|