From 00196cf86c8600846900bd46e38586b1109c2770 Mon Sep 17 00:00:00 2001 From: Marcelo Hernandez Lopez Date: Fri, 14 Jun 2024 00:49:03 -0400 Subject: [PATCH 1/5] Revert "src: use clap's default value mechanism" This reverts commit f1338c2820bcb46bee029bcaf3c28a1135367036. Oopsie, should've attempted to make a config first, as this commit makes it harder to do so, as DimOpts will always have _some_ value due to this. --- src/main.rs | 11 +++++++---- src/opts.rs | 16 ++++++++-------- 2 files changed, 15 insertions(+), 12 deletions(-) diff --git a/src/main.rs b/src/main.rs index 2a2db5e..473c0d6 100644 --- a/src/main.rs +++ b/src/main.rs @@ -2,7 +2,10 @@ use std::{process, thread, time::Duration}; use anyhow::{anyhow, Context}; use clap::Parser; -use dim_screen::{dim::DimData, opts::DimOpts}; +use dim_screen::{ + dim::DimData, + opts::{DimOpts, DEFAULT_ALPHA, DEFAULT_DURATION}, +}; use smithay_client_toolkit::{ compositor::CompositorState, reexports::client::{globals::registry_queue_init, Connection}, @@ -27,9 +30,10 @@ fn main() -> anyhow::Result<()> { let compositor = CompositorState::bind(&globals, &qh).context("Compositor not available")?; let layer_shell = LayerShell::bind(&globals, &qh).context("Layer shell failed?")?; - let alpha = args.alpha; - let duration = args.duration; + let alpha = args.alpha.unwrap_or(DEFAULT_ALPHA); + let mut data = DimData::new(compositor, &globals, &qh, layer_shell, alpha); + let duration = args.duration.unwrap_or(DEFAULT_DURATION); // A duration of 0 is considered as infinite, not starting the timer: if duration > 0 { thread::spawn(move || { @@ -38,7 +42,6 @@ fn main() -> anyhow::Result<()> { }); } - let mut data = DimData::new(compositor, &globals, &qh, layer_shell, alpha); while !data.should_exit() { event_queue .blocking_dispatch(&mut data) diff --git a/src/opts.rs b/src/opts.rs index efff1b8..a8ca8ca 100644 --- a/src/opts.rs +++ b/src/opts.rs @@ -3,8 +3,10 @@ use std::path::{Path, PathBuf}; use clap::{CommandFactory, Parser, ValueEnum}; use clap_complete::{generate_to, Shell}; -const DEFAULT_DURATION: u64 = 30; -const DEFAULT_ALPHA: f32 = 0.5; +/// Default duration in seconds +pub const DEFAULT_DURATION: u64 = 30; + +pub const DEFAULT_ALPHA: f32 = 0.5; #[derive(Debug, Parser)] #[command(author, version, about)] @@ -12,18 +14,16 @@ pub struct DimOpts { #[arg( short, long, - default_value_t = DEFAULT_DURATION, - help = "Duration in seconds, 0 is infinite" + help = format!("Duration in seconds, 0 is infinite, default is {DEFAULT_DURATION}") )] - pub duration: u64, + pub duration: Option, #[arg( short, long, - default_value_t = DEFAULT_ALPHA, - help = "0.0 is transparent, 1.0 is opaque" + help = format!("0.0 is transparent, 1.0 is opaque, default is {DEFAULT_ALPHA}") )] - pub alpha: f32, + pub alpha: Option, #[arg(long, value_name = "PATH", help = "Generate completions at given path")] pub gen_completions: Option, From a2d706f2a35b3fb0905391dce82ae797801b228f Mon Sep 17 00:00:00 2001 From: Marcelo Hernandez Lopez Date: Fri, 14 Jun 2024 01:06:22 -0400 Subject: [PATCH 2/5] src: move default value constants to lib.rs where the other constant was --- src/lib.rs | 3 +++ src/main.rs | 9 +++------ src/opts.rs | 9 +++------ 3 files changed, 9 insertions(+), 12 deletions(-) diff --git a/src/lib.rs b/src/lib.rs index 40c92c1..c269c29 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -1,5 +1,8 @@ pub mod dim; pub mod opts; +pub const DEFAULT_DURATION: u64 = 30; +pub const DEFAULT_ALPHA: f32 = 0.5; + /// Default window size in width and height, in logical pixels pub const INIT_SIZE: u32 = 100; diff --git a/src/main.rs b/src/main.rs index 473c0d6..4392f5a 100644 --- a/src/main.rs +++ b/src/main.rs @@ -2,10 +2,7 @@ use std::{process, thread, time::Duration}; use anyhow::{anyhow, Context}; use clap::Parser; -use dim_screen::{ - dim::DimData, - opts::{DimOpts, DEFAULT_ALPHA, DEFAULT_DURATION}, -}; +use dim_screen::{dim::DimData, opts::DimOpts, DEFAULT_ALPHA, DEFAULT_DURATION}; use smithay_client_toolkit::{ compositor::CompositorState, reexports::client::{globals::registry_queue_init, Connection}, @@ -31,9 +28,8 @@ fn main() -> anyhow::Result<()> { let layer_shell = LayerShell::bind(&globals, &qh).context("Layer shell failed?")?; let alpha = args.alpha.unwrap_or(DEFAULT_ALPHA); - let mut data = DimData::new(compositor, &globals, &qh, layer_shell, alpha); - let duration = args.duration.unwrap_or(DEFAULT_DURATION); + // A duration of 0 is considered as infinite, not starting the timer: if duration > 0 { thread::spawn(move || { @@ -42,6 +38,7 @@ fn main() -> anyhow::Result<()> { }); } + let mut data = DimData::new(compositor, &globals, &qh, layer_shell, alpha); while !data.should_exit() { event_queue .blocking_dispatch(&mut data) diff --git a/src/opts.rs b/src/opts.rs index a8ca8ca..32597f3 100644 --- a/src/opts.rs +++ b/src/opts.rs @@ -3,10 +3,7 @@ use std::path::{Path, PathBuf}; use clap::{CommandFactory, Parser, ValueEnum}; use clap_complete::{generate_to, Shell}; -/// Default duration in seconds -pub const DEFAULT_DURATION: u64 = 30; - -pub const DEFAULT_ALPHA: f32 = 0.5; +use crate::{DEFAULT_ALPHA, DEFAULT_DURATION}; #[derive(Debug, Parser)] #[command(author, version, about)] @@ -14,14 +11,14 @@ pub struct DimOpts { #[arg( short, long, - help = format!("Duration in seconds, 0 is infinite, default is {DEFAULT_DURATION}") + help = format!("Duration in seconds, 0 is infinite, [default: {DEFAULT_DURATION}]") )] pub duration: Option, #[arg( short, long, - help = format!("0.0 is transparent, 1.0 is opaque, default is {DEFAULT_ALPHA}") + help = format!("0.0 is transparent, 1.0 is opaque, [default: {DEFAULT_ALPHA}]") )] pub alpha: Option, From db8d8551f4d9414babc92ab767e79d78e29b0bb6 Mon Sep 17 00:00:00 2001 From: Marcelo Date: Fri, 14 Jun 2024 22:32:01 -0400 Subject: [PATCH 3/5] new: config woop woop --- Cargo.lock | 256 +++++++++++++++++++++++++++++++++++++++++++++++++--- Cargo.toml | 3 + src/lib.rs | 2 + src/main.rs | 31 ++++++- src/opts.rs | 16 +++- 5 files changed, 288 insertions(+), 20 deletions(-) diff --git a/Cargo.lock b/Cargo.lock index a6a108b..3a7f667 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -47,7 +47,7 @@ version = "1.1.0" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "ad186efb764318d35165f1758e7dcef3b10628e26d41a44bc5550652e6804391" dependencies = [ - "windows-sys", + "windows-sys 0.52.0", ] [[package]] @@ -57,7 +57,7 @@ source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "61a38449feb7068f52bb06c12759005cf459ee52bb4adc1d5a7c4322d716fb19" dependencies = [ "anstyle", - "windows-sys", + "windows-sys 0.52.0", ] [[package]] @@ -219,9 +219,33 @@ dependencies = [ "anyhow", "clap", "clap_complete", + "directories", "env_logger", "log", + "serde", "smithay-client-toolkit", + "toml", +] + +[[package]] +name = "directories" +version = "5.0.1" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "9a49173b84e034382284f27f1af4dcbbd231ffa358c0fe316541a7337f376a35" +dependencies = [ + "dirs-sys", +] + +[[package]] +name = "dirs-sys" +version = "0.4.1" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "520f05a5cbd335fae5a99ff7a6ab8627577660ee5cfd6a94a6a929b52ff0321c" +dependencies = [ + "libc", + "option-ext", + "redox_users", + "windows-sys 0.48.0", ] [[package]] @@ -262,6 +286,12 @@ dependencies = [ "log", ] +[[package]] +name = "equivalent" +version = "1.0.1" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "5443807d6dff69373d433ab9ef5378ad8df50ca6298caf15de6e52e24aaf54d5" + [[package]] name = "errno" version = "0.3.9" @@ -269,9 +299,26 @@ source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "534c5cf6194dfab3db3242765c03bbe257cf92f22b38f6bc0c58d59108a820ba" dependencies = [ "libc", - "windows-sys", + "windows-sys 0.52.0", ] +[[package]] +name = "getrandom" +version = "0.2.15" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "c4567c8db10ae91089c99af84c68c38da3ec2f087c3f82960bcdbf3656b6f4d7" +dependencies = [ + "cfg-if", + "libc", + "wasi", +] + +[[package]] +name = "hashbrown" +version = "0.14.5" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "e5274423e17b7c9fc20b6e7e208532f9b19825d82dfd615708b70edd83df41f1" + [[package]] name = "heck" version = "0.5.0" @@ -290,6 +337,16 @@ version = "2.1.0" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "9a3a5bfb195931eeb336b2a7b4d761daec841b97f947d34394601737a7bba5e4" +[[package]] +name = "indexmap" +version = "2.2.6" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "168fb715dda47215e360912c096649d23d58bf392ac62f73919e831745e40f26" +dependencies = [ + "equivalent", + "hashbrown", +] + [[package]] name = "is_terminal_polyfill" version = "1.70.0" @@ -309,7 +366,17 @@ source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "0c2a198fb6b0eada2a8df47933734e6d35d350665a33a3593d7164fa52c75c19" dependencies = [ "cfg-if", - "windows-targets", + "windows-targets 0.52.5", +] + +[[package]] +name = "libredox" +version = "0.1.3" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "c0ff37bd590ca25063e35af745c343cb7a0271906fb7b37e4813e8f79f00268d" +dependencies = [ + "bitflags", + "libc", ] [[package]] @@ -348,6 +415,12 @@ dependencies = [ "libc", ] +[[package]] +name = "option-ext" +version = "0.2.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "04744f49eae99ab78e0d5c0b603ab218f515ea8cfe5a456d7629ad883a3b6e7d" + [[package]] name = "pin-project-lite" version = "0.2.14" @@ -372,7 +445,7 @@ dependencies = [ "pin-project-lite", "rustix", "tracing", - "windows-sys", + "windows-sys 0.52.0", ] [[package]] @@ -402,6 +475,17 @@ dependencies = [ "proc-macro2", ] +[[package]] +name = "redox_users" +version = "0.4.5" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "bd283d9651eeda4b2a83a43c1c91b266c40fd76ecd39a50a8c630ae69dc72891" +dependencies = [ + "getrandom", + "libredox", + "thiserror", +] + [[package]] name = "regex" version = "1.10.5" @@ -441,7 +525,7 @@ dependencies = [ "errno", "libc", "linux-raw-sys", - "windows-sys", + "windows-sys 0.52.0", ] [[package]] @@ -450,6 +534,35 @@ version = "1.0.1" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "e1cf6437eb19a8f4a6cc0f7dca544973b0b78843adbfeb3683d1a94a0024a294" +[[package]] +name = "serde" +version = "1.0.203" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "7253ab4de971e72fb7be983802300c30b5a7f0c2e56fab8abfc6a214307c0094" +dependencies = [ + "serde_derive", +] + +[[package]] +name = "serde_derive" +version = "1.0.203" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "500cbc0ebeb6f46627f50f3f5811ccf6bf00643be300b4c3eabc0ef55dc5b5ba" +dependencies = [ + "proc-macro2", + "quote", + "syn", +] + +[[package]] +name = "serde_spanned" +version = "0.6.6" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "79e674e01f999af37c49f70a6ede167a8a60b2503e56c5599532a65baa5969a0" +dependencies = [ + "serde", +] + [[package]] name = "slab" version = "0.4.9" @@ -530,6 +643,40 @@ dependencies = [ "syn", ] +[[package]] +name = "toml" +version = "0.8.14" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "6f49eb2ab21d2f26bd6db7bf383edc527a7ebaee412d17af4d40fdccd442f335" +dependencies = [ + "serde", + "serde_spanned", + "toml_datetime", + "toml_edit", +] + +[[package]] +name = "toml_datetime" +version = "0.6.6" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "4badfd56924ae69bcc9039335b2e017639ce3f9b001c393c1b2d1ef846ce2cbf" +dependencies = [ + "serde", +] + +[[package]] +name = "toml_edit" +version = "0.22.14" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "f21c7aaf97f1bd9ca9d4f9e73b0a6c74bd5afef56f2bc931943a6e1c37e04e38" +dependencies = [ + "indexmap", + "serde", + "serde_spanned", + "toml_datetime", + "winnow", +] + [[package]] name = "tracing" version = "0.1.40" @@ -558,6 +705,12 @@ version = "0.2.2" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "06abde3611657adf66d383f00b093d7faecc7fa57071cce2578660c9f1010821" +[[package]] +name = "wasi" +version = "0.11.0+wasi-snapshot-preview1" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "9c8d87e72b64a3b4db28d11ce29237c246188f4f51057d65a7eab63b7987e423" + [[package]] name = "wayland-backend" version = "0.3.4" @@ -653,13 +806,37 @@ dependencies = [ "pkg-config", ] +[[package]] +name = "windows-sys" +version = "0.48.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "677d2418bec65e3338edb076e806bc1ec15693c5d0104683f2efe857f61056a9" +dependencies = [ + "windows-targets 0.48.5", +] + [[package]] name = "windows-sys" version = "0.52.0" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "282be5f36a8ce781fad8c8ae18fa3f9beff57ec1b52cb3de0789201425d9a33d" dependencies = [ - "windows-targets", + "windows-targets 0.52.5", +] + +[[package]] +name = "windows-targets" +version = "0.48.5" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "9a2fa6e2155d7247be68c096456083145c183cbbbc2764150dda45a87197940c" +dependencies = [ + "windows_aarch64_gnullvm 0.48.5", + "windows_aarch64_msvc 0.48.5", + "windows_i686_gnu 0.48.5", + "windows_i686_msvc 0.48.5", + "windows_x86_64_gnu 0.48.5", + "windows_x86_64_gnullvm 0.48.5", + "windows_x86_64_msvc 0.48.5", ] [[package]] @@ -668,28 +845,46 @@ version = "0.52.5" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "6f0713a46559409d202e70e28227288446bf7841d3211583a4b53e3f6d96e7eb" dependencies = [ - "windows_aarch64_gnullvm", - "windows_aarch64_msvc", - "windows_i686_gnu", + "windows_aarch64_gnullvm 0.52.5", + "windows_aarch64_msvc 0.52.5", + "windows_i686_gnu 0.52.5", "windows_i686_gnullvm", - "windows_i686_msvc", - "windows_x86_64_gnu", - "windows_x86_64_gnullvm", - "windows_x86_64_msvc", + "windows_i686_msvc 0.52.5", + "windows_x86_64_gnu 0.52.5", + "windows_x86_64_gnullvm 0.52.5", + "windows_x86_64_msvc 0.52.5", ] +[[package]] +name = "windows_aarch64_gnullvm" +version = "0.48.5" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "2b38e32f0abccf9987a4e3079dfb67dcd799fb61361e53e2882c3cbaf0d905d8" + [[package]] name = "windows_aarch64_gnullvm" version = "0.52.5" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "7088eed71e8b8dda258ecc8bac5fb1153c5cffaf2578fc8ff5d61e23578d3263" +[[package]] +name = "windows_aarch64_msvc" +version = "0.48.5" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "dc35310971f3b2dbbf3f0690a219f40e2d9afcf64f9ab7cc1be722937c26b4bc" + [[package]] name = "windows_aarch64_msvc" version = "0.52.5" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "9985fd1504e250c615ca5f281c3f7a6da76213ebd5ccc9561496568a2752afb6" +[[package]] +name = "windows_i686_gnu" +version = "0.48.5" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "a75915e7def60c94dcef72200b9a8e58e5091744960da64ec734a6c6e9b3743e" + [[package]] name = "windows_i686_gnu" version = "0.52.5" @@ -702,30 +897,63 @@ version = "0.52.5" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "87f4261229030a858f36b459e748ae97545d6f1ec60e5e0d6a3d32e0dc232ee9" +[[package]] +name = "windows_i686_msvc" +version = "0.48.5" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "8f55c233f70c4b27f66c523580f78f1004e8b5a8b659e05a4eb49d4166cca406" + [[package]] name = "windows_i686_msvc" version = "0.52.5" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "db3c2bf3d13d5b658be73463284eaf12830ac9a26a90c717b7f771dfe97487bf" +[[package]] +name = "windows_x86_64_gnu" +version = "0.48.5" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "53d40abd2583d23e4718fddf1ebec84dbff8381c07cae67ff7768bbf19c6718e" + [[package]] name = "windows_x86_64_gnu" version = "0.52.5" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "4e4246f76bdeff09eb48875a0fd3e2af6aada79d409d33011886d3e1581517d9" +[[package]] +name = "windows_x86_64_gnullvm" +version = "0.48.5" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "0b7b52767868a23d5bab768e390dc5f5c55825b6d30b86c844ff2dc7414044cc" + [[package]] name = "windows_x86_64_gnullvm" version = "0.52.5" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "852298e482cd67c356ddd9570386e2862b5673c85bd5f88df9ab6802b334c596" +[[package]] +name = "windows_x86_64_msvc" +version = "0.48.5" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "ed94fce61571a4006852b7389a063ab983c02eb1bb37b47f8272ce92d06d9538" + [[package]] name = "windows_x86_64_msvc" version = "0.52.5" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "bec47e5bfd1bff0eeaf6d8b485cc1074891a197ab4225d504cb7a1ab88b02bf0" +[[package]] +name = "winnow" +version = "0.6.13" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "59b5e5f6c299a3c7890b876a2a587f3115162487e704907d9b6cd29473052ba1" +dependencies = [ + "memchr", +] + [[package]] name = "xcursor" version = "0.3.5" diff --git a/Cargo.toml b/Cargo.toml index 4430188..73b591b 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -17,9 +17,12 @@ path = "src/main.rs" anyhow = "1.0.79" clap = { version = "4.4.18", features = ["derive"] } clap_complete = "4.4" +directories = "5.0" env_logger = "0.11.2" log = "0.4.20" +serde = { version = "1.0", features = ["derive"] } smithay-client-toolkit = "0.18.0" +toml = "0.8" [profile.release] lto = true diff --git a/src/lib.rs b/src/lib.rs index c269c29..d8014e1 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -4,5 +4,7 @@ pub mod opts; pub const DEFAULT_DURATION: u64 = 30; pub const DEFAULT_ALPHA: f32 = 0.5; +pub const CONFIG_FILENAME: &str = "config.toml"; + /// Default window size in width and height, in logical pixels pub const INIT_SIZE: u32 = 100; diff --git a/src/main.rs b/src/main.rs index 4392f5a..d91fa13 100644 --- a/src/main.rs +++ b/src/main.rs @@ -1,8 +1,10 @@ -use std::{process, thread, time::Duration}; +use std::{fs::File, io::read_to_string, process, thread, time::Duration}; -use anyhow::{anyhow, Context}; +use anyhow::{anyhow, bail, Context}; use clap::Parser; -use dim_screen::{dim::DimData, opts::DimOpts, DEFAULT_ALPHA, DEFAULT_DURATION}; +use dim_screen::{dim::DimData, opts::DimOpts, CONFIG_FILENAME, DEFAULT_ALPHA, DEFAULT_DURATION}; +use directories::ProjectDirs; +use log::{debug, info}; use smithay_client_toolkit::{ compositor::CompositorState, reexports::client::{globals::registry_queue_init, Connection}, @@ -18,6 +20,24 @@ fn main() -> anyhow::Result<()> { return Ok(()); } + let Some(dirs) = ProjectDirs::from("com", "marcelohdez", "dim") else { + bail!("Could not generate project directories on this OS?"); + }; + let config_dir = dirs.config_dir().join(CONFIG_FILENAME); + + let opts = if config_dir.exists() { + debug!("Config file found at {config_dir:?}"); + + let file = File::open(config_dir)?; + let config: DimOpts = toml::from_str(&read_to_string(file)?)?; + + debug!("Config: {config:?}"); + config.merge_onto_self(args) + } else { + info!("Config file not found!"); + args + }; + let conn = Connection::connect_to_env().context("Failed to connect to environment")?; let (globals, mut event_queue) = @@ -27,8 +47,9 @@ fn main() -> anyhow::Result<()> { let compositor = CompositorState::bind(&globals, &qh).context("Compositor not available")?; let layer_shell = LayerShell::bind(&globals, &qh).context("Layer shell failed?")?; - let alpha = args.alpha.unwrap_or(DEFAULT_ALPHA); - let duration = args.duration.unwrap_or(DEFAULT_DURATION); + debug!("Using options: {opts:?}"); + let alpha = opts.alpha.unwrap_or(DEFAULT_ALPHA); + let duration = opts.duration.unwrap_or(DEFAULT_DURATION); // A duration of 0 is considered as infinite, not starting the timer: if duration > 0 { diff --git a/src/opts.rs b/src/opts.rs index 32597f3..34ee1b1 100644 --- a/src/opts.rs +++ b/src/opts.rs @@ -2,10 +2,11 @@ use std::path::{Path, PathBuf}; use clap::{CommandFactory, Parser, ValueEnum}; use clap_complete::{generate_to, Shell}; +use serde::Deserialize; use crate::{DEFAULT_ALPHA, DEFAULT_DURATION}; -#[derive(Debug, Parser)] +#[derive(Debug, Deserialize, Parser)] #[command(author, version, about)] pub struct DimOpts { #[arg( @@ -22,6 +23,7 @@ pub struct DimOpts { )] pub alpha: Option, + #[serde(skip)] #[arg(long, value_name = "PATH", help = "Generate completions at given path")] pub gen_completions: Option, } @@ -38,3 +40,15 @@ impl DimOpts { Ok(()) } } + +impl DimOpts { + /// Merge other onto self, with other's values taking precedent + pub fn merge_onto_self(self, other: DimOpts) -> Self { + Self { + duration: other.duration.or(self.duration), + alpha: other.alpha.or(self.alpha), + + ..self + } + } +} From bd5a0fa75274461d45b19ea2d6771d41b8fed7af Mon Sep 17 00:00:00 2001 From: Marcelo Date: Sat, 15 Jun 2024 00:38:18 -0400 Subject: [PATCH 4/5] src: cleanup main --- src/main.rs | 68 ++++++++++++++++++++++++++++++++--------------------- 1 file changed, 41 insertions(+), 27 deletions(-) diff --git a/src/main.rs b/src/main.rs index d91fa13..dbf086f 100644 --- a/src/main.rs +++ b/src/main.rs @@ -7,7 +7,7 @@ use directories::ProjectDirs; use log::{debug, info}; use smithay_client_toolkit::{ compositor::CompositorState, - reexports::client::{globals::registry_queue_init, Connection}, + reexports::client::{globals::registry_queue_init, Connection, EventQueue}, shell::wlr_layer::LayerShell, }; @@ -20,32 +20,10 @@ fn main() -> anyhow::Result<()> { return Ok(()); } - let Some(dirs) = ProjectDirs::from("com", "marcelohdez", "dim") else { - bail!("Could not generate project directories on this OS?"); + let opts = match get_config()? { + Some(config) => config.merge_onto_self(args), + None => args, }; - let config_dir = dirs.config_dir().join(CONFIG_FILENAME); - - let opts = if config_dir.exists() { - debug!("Config file found at {config_dir:?}"); - - let file = File::open(config_dir)?; - let config: DimOpts = toml::from_str(&read_to_string(file)?)?; - - debug!("Config: {config:?}"); - config.merge_onto_self(args) - } else { - info!("Config file not found!"); - args - }; - - let conn = Connection::connect_to_env().context("Failed to connect to environment")?; - - let (globals, mut event_queue) = - registry_queue_init(&conn).context("Failed to initialize registry")?; - let qh = event_queue.handle(); - - let compositor = CompositorState::bind(&globals, &qh).context("Compositor not available")?; - let layer_shell = LayerShell::bind(&globals, &qh).context("Layer shell failed?")?; debug!("Using options: {opts:?}"); let alpha = opts.alpha.unwrap_or(DEFAULT_ALPHA); @@ -59,7 +37,7 @@ fn main() -> anyhow::Result<()> { }); } - let mut data = DimData::new(compositor, &globals, &qh, layer_shell, alpha); + let (mut data, mut event_queue) = create_wl_app(alpha)?; while !data.should_exit() { event_queue .blocking_dispatch(&mut data) @@ -68,3 +46,39 @@ fn main() -> anyhow::Result<()> { Err(anyhow!("Some user input was detected!")) } + +fn get_config() -> anyhow::Result> { + let Some(dirs) = ProjectDirs::from("com", "marcelohdez", "dim") else { + bail!("Could not generate project directories on this OS?"); + }; + let config_dir = dirs.config_dir().join(CONFIG_FILENAME); + + if !config_dir.exists() { + info!("No config found!"); + return Ok(None); + } + + debug!("Config file found at {config_dir:?}"); + + let file = File::open(config_dir)?; + let config: DimOpts = toml::from_str(&read_to_string(file)?)?; + + debug!("Config: {config:?}"); + Ok(Some(config)) +} + +fn create_wl_app(alpha: f32) -> anyhow::Result<(DimData, EventQueue)> { + let conn = Connection::connect_to_env().context("Failed to connect to environment")?; + + let (globals, event_queue) = + registry_queue_init(&conn).context("Failed to initialize registry")?; + let qh = event_queue.handle(); + + let compositor = CompositorState::bind(&globals, &qh).context("Compositor not available")?; + let layer_shell = LayerShell::bind(&globals, &qh).context("Layer shell failed?")?; + + Ok(( + DimData::new(compositor, &globals, &qh, layer_shell, alpha), + event_queue, + )) +} From 9a55ed83eee2896a96a0ba1d1ee51906de481059 Mon Sep 17 00:00:00 2001 From: Marcelo Date: Sun, 16 Jun 2024 03:31:15 -0400 Subject: [PATCH 5/5] readme: add mention of config file --- README.md | 9 +++++---- 1 file changed, 5 insertions(+), 4 deletions(-) diff --git a/README.md b/README.md index 915e796..4411791 100644 --- a/README.md +++ b/README.md @@ -20,11 +20,12 @@ for the default of `30` seconds, then if no input is detected the next command will proceed, in this case [swaylock] will lock your screen. `dim` should only finish **successfully** when no input is detected for the -duration given with the `--duration` command, which is 30 by default. If -`dim` finishes successfully before this duration, please [submit an issue]. +duration. If `dim` finishes successfully before this duration, please [submit +an issue]. -The alpha of `dim` may be configured with the `--alpha` option. For more info, -please see: +The alpha and duration of `dim` may be configured with either a config file +located at `~/.config/dim/config.toml`, or through arguments at call-time, for +all options and their defaults please see: ```bash dim --help