Started globbing stuff

This commit is contained in:
Benjamyn Love 2024-03-10 16:01:32 +11:00
parent ed148b05cb
commit 3fc1f271d0
6 changed files with 192 additions and 73 deletions

9
Cargo.lock generated
View File

@ -17,6 +17,12 @@ 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 = "glob"
version = "0.3.1"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "d2fabcfbdc87f4758337ca535fb41a6d701b65693ce38287d856d1674551ec9b"
[[package]] [[package]]
name = "memchr" name = "memchr"
version = "2.7.1" version = "2.7.1"
@ -28,6 +34,7 @@ name = "mycelium"
version = "0.1.0" version = "0.1.0"
dependencies = [ dependencies = [
"configparser", "configparser",
"glob",
"untildify", "untildify",
] ]
@ -63,8 +70,6 @@ checksum = "c08c74e62047bb2de4ff487b251e4a92e24f48745648451635cec7d591162d9f"
[[package]] [[package]]
name = "untildify" name = "untildify"
version = "0.1.1" version = "0.1.1"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "992d9e1100cf171942054a07eef9a04986cc8acdb1f771be47accaa301a46bb7"
dependencies = [ dependencies = [
"regex", "regex",
] ]

View File

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

View File

@ -1,59 +1,9 @@
use configparser::ini::Ini; use configparser::ini::Ini;
use core::fmt; use core::fmt;
use std::env::VarError;
use crate::enums::*;
use untildify; 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)] #[derive(Debug)]
pub struct Monitors { pub struct Monitors {
monitors: Vec<MonitorType>, monitors: Vec<MonitorType>,
@ -95,6 +45,15 @@ impl WallpaperDir {
vertical: expanded_tilde.clone(), 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 { impl fmt::Display for WallpaperDir {
@ -126,15 +85,7 @@ impl Config {
monitors: Monitors::new(), 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 { pub fn from_config(file_path: String) -> Config {
let mut config = Ini::new(); let mut config = Ini::new();
let map = config.load(file_path).unwrap(); let map = config.load(file_path).unwrap();
@ -146,9 +97,12 @@ impl Config {
// Get the wallpaper dirs and parse them // Get the wallpaper dirs and parse them
let ultrawide_dir = config.get("general", "ultra_wall_dir").unwrap(); let ultrawide_dir =
let horizontal_dir = config.get("general", "hor_wall_dir").unwrap(); untildify::untildify(config.get("general", "ultra_wall_dir").unwrap().as_ref());
let vert_wall_dir = config.get("general", "vert_wall_dir").unwrap(); 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 { let wallpaper_dir = WallpaperDir {
ultrawide: ultrawide_dir, ultrawide: ultrawide_dir,
horizontal: horizontal_dir, horizontal: horizontal_dir,
@ -172,9 +126,15 @@ impl Config {
Config { Config {
x_server: config.get("general", "x_server").unwrap(), x_server: config.get("general", "x_server").unwrap(),
wallpaper_engine: wallpaper_engine, wallpaper_engine,
wallpaper_dir: wallpaper_dir, wallpaper_dir,
monitors: monitors, 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)
}
}

64
src/enums.rs Normal file
View File

@ -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)
}
}

85
src/files.rs Normal file
View File

@ -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<Wallpaper>,
horizontal: Vec<Wallpaper>,
vertical: Vec<Wallpaper>,
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<Wallpaper>
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, "")
}
}

View File

@ -1,9 +1,13 @@
mod config; mod config;
use crate::config::*;
mod enums;
use crate::enums::*;
mod files;
use crate::files::*;
fn main() { fn main() {
// let mut config = config::Config::new(); let mut wallpapers = Wallpapers::new();
let config = crate::config::Config::from_config(
"/home/ben/.config/wallpaperctl/wallpaperctl.ini".to_owned(), wallpapers.load_images(MonitorType::Ultrawide);
); println!("{}", wallpapers);
println!("{}", config);
} }