From 3fc1f271d0195d54a12050545a3df9bd7e8ef4a1 Mon Sep 17 00:00:00 2001 From: Benjamyn Love Date: Sun, 10 Mar 2024 16:01:32 +1100 Subject: [PATCH] Started globbing stuff --- Cargo.lock | 9 ++++-- Cargo.toml | 3 +- src/config.rs | 90 ++++++++++++++------------------------------------- src/enums.rs | 64 ++++++++++++++++++++++++++++++++++++ src/files.rs | 85 ++++++++++++++++++++++++++++++++++++++++++++++++ src/main.rs | 14 +++++--- 6 files changed, 192 insertions(+), 73 deletions(-) create mode 100644 src/enums.rs create mode 100644 src/files.rs diff --git a/Cargo.lock b/Cargo.lock index 13dba39..6490866 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -17,6 +17,12 @@ version = "3.0.4" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "4ec6d3da8e550377a85339063af6e3735f4b1d9392108da4e083a1b3b9820288" +[[package]] +name = "glob" +version = "0.3.1" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "d2fabcfbdc87f4758337ca535fb41a6d701b65693ce38287d856d1674551ec9b" + [[package]] name = "memchr" version = "2.7.1" @@ -28,6 +34,7 @@ name = "mycelium" version = "0.1.0" dependencies = [ "configparser", + "glob", "untildify", ] @@ -63,8 +70,6 @@ checksum = "c08c74e62047bb2de4ff487b251e4a92e24f48745648451635cec7d591162d9f" [[package]] name = "untildify" version = "0.1.1" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "992d9e1100cf171942054a07eef9a04986cc8acdb1f771be47accaa301a46bb7" dependencies = [ "regex", ] diff --git a/Cargo.toml b/Cargo.toml index ecaca2c..1d4ac5e 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -5,4 +5,5 @@ edition = "2021" [dependencies] configparser = "3.0.4" -untildify = "0.1.1" +glob = "0.3.1" +untildify = { path = "../untildify" } diff --git a/src/config.rs b/src/config.rs index f1d2f6a..e4e0f72 100644 --- a/src/config.rs +++ b/src/config.rs @@ -1,59 +1,9 @@ use configparser::ini::Ini; use core::fmt; -use std::env::VarError; +use crate::enums::*; use untildify; -#[derive(Debug)] -pub enum WallpaperHandler { - Feh, - Plasma, - Gnome, - Error, -} - -impl WallpaperHandler { - pub fn new(handler: String) -> WallpaperHandler { - match handler.as_str() { - "feh" => WallpaperHandler::Feh, - "plasma" => WallpaperHandler::Plasma, - "gnome" => WallpaperHandler::Gnome, - _ => WallpaperHandler::Error, - } - } -} - -impl fmt::Display for WallpaperHandler { - fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result { - write!(f, "{:?}", self) - } -} - -#[derive(Debug)] -pub enum MonitorType { - Ultrawide, - Horizontal, - Vertical, - Error, -} - -impl MonitorType { - fn new(mon_type: String) -> MonitorType { - match mon_type.as_str() { - "ultrawide" => MonitorType::Ultrawide, - "vertical" => MonitorType::Vertical, - "horizontal" => MonitorType::Horizontal, - _ => MonitorType::Error, - } - } -} - -impl fmt::Display for MonitorType { - fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result { - write!(f, "{:?}", self) - } -} - #[derive(Debug)] pub struct Monitors { monitors: Vec, @@ -95,6 +45,15 @@ impl WallpaperDir { vertical: expanded_tilde.clone(), } } + + pub fn get(&self, monitor_type: MonitorType) -> String { + match monitor_type { + MonitorType::Ultrawide => self.ultrawide.to_string(), + MonitorType::Horizontal => self.horizontal.to_string(), + MonitorType::Vertical => self.vertical.to_string(), + MonitorType::Error => "error".to_string(), + } + } } impl fmt::Display for WallpaperDir { @@ -126,15 +85,7 @@ impl Config { monitors: Monitors::new(), } } -} -impl fmt::Display for Config { - fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result { - write!(f, "Mycelium Config: X Server: {xserv}, Wallpaper Engine: {wallpaper_engine}, Wallpaper Directories: {wallpaper_dirs}, Monitors: {monitors}", xserv = self.x_server, wallpaper_engine = self.wallpaper_engine, wallpaper_dirs = self.wallpaper_dir, monitors = self.monitors) - } -} - -impl Config { pub fn from_config(file_path: String) -> Config { let mut config = Ini::new(); let map = config.load(file_path).unwrap(); @@ -146,9 +97,12 @@ impl Config { // Get the wallpaper dirs and parse them - let ultrawide_dir = config.get("general", "ultra_wall_dir").unwrap(); - let horizontal_dir = config.get("general", "hor_wall_dir").unwrap(); - let vert_wall_dir = config.get("general", "vert_wall_dir").unwrap(); + let ultrawide_dir = + untildify::untildify(config.get("general", "ultra_wall_dir").unwrap().as_ref()); + let horizontal_dir = + untildify::untildify(config.get("general", "hor_wall_dir").unwrap().as_ref()); + let vert_wall_dir = + untildify::untildify(config.get("general", "vert_wall_dir").unwrap().as_ref()); let wallpaper_dir = WallpaperDir { ultrawide: ultrawide_dir, horizontal: horizontal_dir, @@ -172,9 +126,15 @@ impl Config { Config { x_server: config.get("general", "x_server").unwrap(), - wallpaper_engine: wallpaper_engine, - wallpaper_dir: wallpaper_dir, - monitors: monitors, + wallpaper_engine, + wallpaper_dir, + monitors, } } } + +impl fmt::Display for Config { + fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result { + write!(f, "Mycelium Config: X Server: {xserv}, Wallpaper Engine: {wallpaper_engine}, Wallpaper Directories: {wallpaper_dirs}, Monitors: {monitors}", xserv = self.x_server, wallpaper_engine = self.wallpaper_engine, wallpaper_dirs = self.wallpaper_dir, monitors = self.monitors) + } +} diff --git a/src/enums.rs b/src/enums.rs new file mode 100644 index 0000000..e64d2b3 --- /dev/null +++ b/src/enums.rs @@ -0,0 +1,64 @@ +use core::fmt; + +#[derive(Debug)] +pub enum WallpaperHandler { + Feh, + Plasma, + Gnome, + Error, +} + +impl WallpaperHandler { + pub fn new(handler: String) -> WallpaperHandler { + match handler.as_str() { + "feh" => WallpaperHandler::Feh, + "plasma" => WallpaperHandler::Plasma, + "gnome" => WallpaperHandler::Gnome, + _ => WallpaperHandler::Error, + } + } +} + +impl fmt::Display for WallpaperHandler { + fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result { + write!(f, "{:?}", self) + } +} + +#[derive(Debug, Clone)] +pub enum MonitorType { + Ultrawide, + Horizontal, + Vertical, + Error, +} + +impl MonitorType { + pub fn new(mon_type: String) -> MonitorType { + match mon_type.as_str() { + "ultrawide" => MonitorType::Ultrawide, + "vertical" => MonitorType::Vertical, + "horizontal" => MonitorType::Horizontal, + _ => MonitorType::Error, + } + } +} + +impl fmt::Display for MonitorType { + fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result { + write!(f, "{:?}", self) + } +} + +#[derive(Debug)] +enum ImageTypes { + PNG, + JPG, + JPEG, +} + +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 new file mode 100644 index 0000000..28752b3 --- /dev/null +++ b/src/files.rs @@ -0,0 +1,85 @@ +use core::fmt; +use glob::glob; +use std::process::exit; + +use crate::config::*; +use crate::enums::*; + +#[derive(Debug)] +pub struct Wallpaper { + path: String, +} + +impl Wallpaper { + pub fn new(path: String) -> Wallpaper { + Wallpaper { path } + } +} + +impl fmt::Display for Wallpaper { + fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result { + write!(f, "{}", self.path) + } +} + +#[derive(Debug)] +pub struct Wallpapers { + ultrawide: Vec, + horizontal: Vec, + vertical: Vec, + pub config: Config, +} + +impl Wallpapers { + pub fn new() -> Wallpapers { + Wallpapers { + ultrawide: vec![], + horizontal: vec![], + vertical: vec![], + config: Config::from_config( + "/home/ben/.config/wallpaperctl/wallpaperctl.ini".to_owned(), + ), + } + } + // Todo implement a random selector for a given key, like `wallpapers.random_selection(MonitorType::Ultrawide)` + pub fn random_selection(monitor_type: MonitorType) {} + // 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 + .config + .wallpaper_dir + .get(monitor_type.clone()) + .to_owned(); + path.push_str("/**/*.png"); + + for entry in glob(&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 }), + _ => {} + } + } +} + +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, "") + } +} diff --git a/src/main.rs b/src/main.rs index 5fd6161..32da1e9 100644 --- a/src/main.rs +++ b/src/main.rs @@ -1,9 +1,13 @@ mod config; +use crate::config::*; +mod enums; +use crate::enums::*; +mod files; +use crate::files::*; fn main() { - // let mut config = config::Config::new(); - let config = crate::config::Config::from_config( - "/home/ben/.config/wallpaperctl/wallpaperctl.ini".to_owned(), - ); - println!("{}", config); + let mut wallpapers = Wallpapers::new(); + + wallpapers.load_images(MonitorType::Ultrawide); + println!("{}", wallpapers); }