diff options
author | Silas Bartha <[email protected]> | 2023-12-21 23:36:07 -0500 |
---|---|---|
committer | Silas Bartha <[email protected]> | 2023-12-21 23:36:07 -0500 |
commit | 901d27db601a27d580c1f2f8d4b3d3361f60cc2e (patch) | |
tree | 3ef9ffbc13a86742740eb390a16a58062bdf44d9 | |
parent | 7733a7d394499979f589506db87200a812249523 (diff) |
1.2.0 - configuration file support
-rw-r--r-- | Cargo.lock | 53 | ||||
-rw-r--r-- | Cargo.toml | 4 | ||||
-rw-r--r-- | README.md | 4 | ||||
-rw-r--r-- | src/main.rs | 66 |
4 files changed, 104 insertions, 23 deletions
@@ -325,6 +325,18 @@ dependencies = [ ] [[package]] +name = "confy" +version = "0.5.1" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "e37668cb35145dcfaa1931a5f37fde375eeae8068b4c0d2f289da28a270b2d2c" +dependencies = [ + "directories", + "serde", + "thiserror", + "toml", +] + +[[package]] name = "cpufeatures" version = "0.2.11" source = "registry+https://github.com/rust-lang/crates.io-index" @@ -383,6 +395,15 @@ dependencies = [ ] [[package]] +name = "directories" +version = "4.0.1" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "f51c5d4ddabd36886dd3e1438cb358cdcb0d7c499cb99cb4ac2e38e18b5cb210" +dependencies = [ + "dirs-sys", +] + +[[package]] name = "dirs-next" version = "2.0.0" source = "registry+https://github.com/rust-lang/crates.io-index" @@ -393,6 +414,17 @@ dependencies = [ ] [[package]] +name = "dirs-sys" +version = "0.3.7" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "1b1d1d91c932ef41c0f2663aa8b0ca0342d444d842c06914aa0a7e352d0bada6" +dependencies = [ + "libc", + "redox_users", + "winapi", +] + +[[package]] name = "dirs-sys-next" version = "0.1.2" source = "registry+https://github.com/rust-lang/crates.io-index" @@ -927,11 +959,13 @@ dependencies = [ [[package]] name = "pomd" -version = "1.1.0" +version = "1.2.0" dependencies = [ "async-std", + "confy", "notify-rust", "pausable_clock", + "serde", "zbus", ] @@ -1119,18 +1153,18 @@ checksum = "e1cf6437eb19a8f4a6cc0f7dca544973b0b78843adbfeb3683d1a94a0024a294" [[package]] name = "serde" -version = "1.0.192" +version = "1.0.193" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "bca2a08484b285dcb282d0f67b26cadc0df8b19f8c12502c13d966bf9482f001" +checksum = "25dd9975e68d0cb5aa1120c288333fc98731bd1dd12f561e468ea4728c042b89" dependencies = [ "serde_derive", ] [[package]] name = "serde_derive" -version = "1.0.192" +version = "1.0.193" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "d6c7207fbec9faa48073f3e3074cbe553af6ea512d7c21ba46e434e70ea9fbc1" +checksum = "43576ca501357b9b071ac53cdc7da8ef0cbd9493d8df094cd821777ea6e894d3" dependencies = [ "proc-macro2", "quote", @@ -1302,6 +1336,15 @@ source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "ef927ca75afb808a4d64dd374f00a2adf8d0fcff8e7b184af886c3c87ec4a3f3" [[package]] +name = "toml" +version = "0.5.11" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "f4f7f0dd8d50a853a531c426359045b1998f04219d88799810762cd4ad314234" +dependencies = [ + "serde", +] + +[[package]] name = "toml_datetime" version = "0.6.5" source = "registry+https://github.com/rust-lang/crates.io-index" @@ -1,6 +1,6 @@ [package] name = "pomd" -version = "1.1.1" +version = "1.2.0" edition = "2021" [dependencies] @@ -8,3 +8,5 @@ notify-rust = "4" zbus = "3.14.1" async-std = { version = "1.12.0", features = ["attributes"] } pausable_clock = "1.0.1" +confy = "0.5.1" +serde = "1.0.193" diff --git a/README.md b/README.md new file mode 100644 index 0000000..d2faf00 --- /dev/null +++ b/README.md @@ -0,0 +1,4 @@ +# pomd: pomodoro daemon + +This program provides a simple pomodoro daemon for linux. + diff --git a/src/main.rs b/src/main.rs index b34d17d..af1eea7 100644 --- a/src/main.rs +++ b/src/main.rs @@ -4,16 +4,35 @@ use std::{ }; use pausable_clock::{PausableClock, PausableInstant}; +use serde::{Serialize, Deserialize}; use zbus::{dbus_interface, ConnectionBuilder, Result}; use notify_rust::Notification; -const WORK_DURATION_SECS: f32 = 15.0 * 60.0; -const SHORT_BREAK_DURATION_SECS: f32 = 5.0 * 60.0; -const LONG_BREAK_DURATION_SECS: f32 = 25.0 * 60.0; -const NUM_ITERATIONS: u8 = 4; +#[derive(Serialize, Deserialize, Clone, Copy)] +struct PomdConfig { + work_duration: f32, + short_break_duration: f32, + long_break_duration: f32, + num_iterations: u8, + notify: bool, +} + +impl Default for PomdConfig { + fn default() -> Self { + Self { + work_duration: 15.0 * 60.0, + short_break_duration: 5.0 * 60.0, + long_break_duration: 25.0 * 60.0, + num_iterations: 4, + notify: true, + } + } +} + struct Pomd { + config: PomdConfig, duration: Duration, iteration: u8, on_break: bool, @@ -21,9 +40,18 @@ struct Pomd { start: PausableInstant } -#[derive(Default)] struct PomdInterface { data: Arc<Mutex<Pomd>>, + config: PomdConfig +} + +impl PomdInterface { + fn new(config: PomdConfig) -> Self { + Self { + data: Arc::new(Mutex::new(Pomd::new(config))), + config, + } + } } #[dbus_interface(name = "dev.exvacuum.pomd")] @@ -54,7 +82,7 @@ impl PomdInterface { } async fn stop(&self) { - *self.data.lock().unwrap() = Pomd::default(); + *self.data.lock().unwrap() = Pomd::new(self.config); } async fn skip(&self) { @@ -62,12 +90,13 @@ impl PomdInterface { } } -impl Default for Pomd { - fn default() -> Self { +impl Pomd { + fn new(config: PomdConfig) -> Self { let clock = PausableClock::new(Duration::ZERO, true); let start = clock.now(); Self { - duration: Duration::from_secs_f32(WORK_DURATION_SECS), + config, + duration: Duration::from_secs_f32(config.work_duration), iteration: 0, on_break: false, clock, @@ -79,7 +108,9 @@ impl Default for Pomd { impl Pomd { fn update(&mut self) { if self.duration < self.start.elapsed(&self.clock) { - self.notify(); + if self.config.notify { + self.notify(); + } self.setup_next_iteration(); } } @@ -89,14 +120,14 @@ impl Pomd { self.start = self.clock.now(); self.on_break ^= true; self.duration = if self.on_break { - if self.iteration == NUM_ITERATIONS - 1 { - Duration::from_secs_f32(LONG_BREAK_DURATION_SECS) + if self.iteration == self.config.num_iterations - 1 { + Duration::from_secs_f32(self.config.long_break_duration) } else { - Duration::from_secs_f32(SHORT_BREAK_DURATION_SECS) + Duration::from_secs_f32(self.config.short_break_duration) } } else { - self.iteration = (self.iteration + 1) % NUM_ITERATIONS; - Duration::from_secs_f32(WORK_DURATION_SECS) + self.iteration = (self.iteration + 1) % self.config.num_iterations; + Duration::from_secs_f32(self.config.work_duration) } } @@ -112,7 +143,7 @@ impl Pomd { .summary(&format!( "Pomodoro Complete ({}/{})", self.iteration + 1, - NUM_ITERATIONS + self.config.num_iterations )) .body("Click to dismiss") .show() @@ -123,7 +154,8 @@ impl Pomd { #[async_std::main] async fn main() -> Result<()> { - let pomd_interface = PomdInterface::default(); + let config: PomdConfig = confy::load("pomd", "config").expect("Failed to load config!"); + let pomd_interface = PomdInterface::new(config); let pomd = pomd_interface.data.clone(); let _connection = ConnectionBuilder::session()? .name("dev.exvacuum.pomd")? |