From f1fbba405b59f7266931f6b49481eacb4d3e6c02 Mon Sep 17 00:00:00 2001 From: Jean-Loup Beaussart Date: Tue, 10 Dec 2024 23:40:06 +0100 Subject: [PATCH] Provide settings to customize photo path and display duration --- Cargo.toml | 1 + settings.yml | 2 ++ src/image_provider.rs | 10 +++++----- src/main.rs | 6 ++++-- src/settings.rs | 22 ++++++++++++++++++++++ 5 files changed, 34 insertions(+), 7 deletions(-) create mode 100644 settings.yml create mode 100644 src/settings.rs diff --git a/Cargo.toml b/Cargo.toml index e40b574..bcd9002 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -43,6 +43,7 @@ lto = "thin" [dependencies] anyhow = "1.0.93" +config = "0.14.1" image = "0.25.4" macroquad = { version = "0.4.13" } walkdir = "2.5.0" diff --git a/settings.yml b/settings.yml new file mode 100644 index 0000000..7fc49a6 --- /dev/null +++ b/settings.yml @@ -0,0 +1,2 @@ +photo_path: "/mnt/c/Users/beaus/Downloads" +duration: 30 \ No newline at end of file diff --git a/src/image_provider.rs b/src/image_provider.rs index d42ea08..80fa016 100644 --- a/src/image_provider.rs +++ b/src/image_provider.rs @@ -1,18 +1,18 @@ use crate::dimensions::{calculate_resize_dimensions, Dimensions}; use image::ImageReader; use macroquad::texture::Texture2D; -use std::path::PathBuf; +use std::path::{Path, PathBuf}; use walkdir::WalkDir; -pub struct ImageProvider { - path: PathBuf, +pub struct ImageProvider<'a> { + path: &'a Path, directory_it: Box>>, texture: Option, screen_dimension: Dimensions, } -impl ImageProvider { - pub fn new(screen_dimension: Dimensions, path: PathBuf) -> Self { +impl<'a> ImageProvider<'a> { + pub fn new(screen_dimension: Dimensions, path: &'a Path) -> Self { Self { directory_it: Box::new(WalkDir::new(&path).max_open(8).into_iter()), texture: None, diff --git a/src/main.rs b/src/main.rs index 3863e13..af63332 100644 --- a/src/main.rs +++ b/src/main.rs @@ -8,6 +8,7 @@ use std::{ mod dimensions; mod image_provider; +mod settings; fn window_conf() -> Conf { Conf { @@ -32,9 +33,10 @@ fn display_image_centered(texture: &Texture2D) { #[macroquad::main(window_conf)] async fn main() -> anyhow::Result<()> { + let settings = settings::Settings::new("settings.yml".into())?; show_mouse(false); let screen_dim = Dimensions::new(screen_width() as u32, screen_height() as u32); - let mut provider = ImageProvider::new(screen_dim, "/mnt/c/Users/beaus/Downloads".into()); + let mut provider = ImageProvider::new(screen_dim, &settings.photo_path); provider.load_next_image()?; let mut time = Instant::now(); @@ -45,7 +47,7 @@ async fn main() -> anyhow::Result<()> { display_frame(&provider)?; - if time.elapsed().as_secs() > 5 { + if time.elapsed() > settings.duration { provider.load_next_image()?; time = Instant::now(); } diff --git a/src/settings.rs b/src/settings.rs new file mode 100644 index 0000000..00dd00c --- /dev/null +++ b/src/settings.rs @@ -0,0 +1,22 @@ +use config::{Config, File}; +use std::{path::PathBuf, time::Duration}; +pub struct Settings { + pub photo_path: PathBuf, + pub duration: Duration, +} + +impl Settings { + pub fn new(settings_file: PathBuf) -> anyhow::Result { + let settings = Config::builder() + .add_source(File::from(settings_file)) + .build()?; + + let photo_path = settings.get_string("photo_path")?.try_into()?; + let duration = settings.get_int("duration")?; + + Ok(Self { + photo_path, + duration: Duration::from_secs(duration as u64), + }) + } +}