aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorLibravatar Silas Bartha <[email protected]>2023-11-11 12:44:41 -0500
committerLibravatar Silas Bartha <[email protected]>2023-11-11 12:44:41 -0500
commit4ca2b6e5e73f4d381a4662d10e54316d3ae69aeb (patch)
tree176cc6b58efc4341ac0e25c95b4111368b015866
parent7fbf56d406733b45a986a4f1d930feb08b1b8172 (diff)
improved time accuracy with pausable_clock
-rw-r--r--Cargo.lock197
-rw-r--r--Cargo.toml3
-rw-r--r--src/main.rs58
3 files changed, 201 insertions, 57 deletions
diff --git a/Cargo.lock b/Cargo.lock
index fb642f7..64d30fe 100644
--- a/Cargo.lock
+++ b/Cargo.lock
@@ -144,17 +144,6 @@ dependencies = [
]
[[package]]
-name = "async-net"
-version = "1.8.0"
-source = "registry+https://github.com/rust-lang/crates.io-index"
-checksum = "0434b1ed18ce1cf5769b8ac540e33f01fa9471058b5e89da9e06f3c882a8c12f"
-dependencies = [
- "async-io 1.13.0",
- "blocking",
- "futures-lite 1.13.0",
-]
-
-[[package]]
name = "async-process"
version = "1.8.1"
source = "registry+https://github.com/rust-lang/crates.io-index"
@@ -568,6 +557,19 @@ dependencies = [
]
[[package]]
+name = "generator"
+version = "0.7.5"
+source = "registry+https://github.com/rust-lang/crates.io-index"
+checksum = "5cc16584ff22b460a382b7feec54b23d2908d858152e5739a120b949293bd74e"
+dependencies = [
+ "cc",
+ "libc",
+ "log",
+ "rustversion",
+ "windows 0.48.0",
+]
+
+[[package]]
name = "generic-array"
version = "0.14.7"
source = "registry+https://github.com/rust-lang/crates.io-index"
@@ -667,6 +669,12 @@ dependencies = [
]
[[package]]
+name = "lazy_static"
+version = "1.4.0"
+source = "registry+https://github.com/rust-lang/crates.io-index"
+checksum = "e2abad23fbc42b3700f2f279844dc832adb2b2eb069b2df918f455c4e18cc646"
+
+[[package]]
name = "libc"
version = "0.2.150"
source = "registry+https://github.com/rust-lang/crates.io-index"
@@ -705,6 +713,19 @@ dependencies = [
]
[[package]]
+name = "loom"
+version = "0.5.6"
+source = "registry+https://github.com/rust-lang/crates.io-index"
+checksum = "ff50ecb28bb86013e935fb6683ab1f6d3a20016f123c76fd4c27470076ac30f5"
+dependencies = [
+ "cfg-if",
+ "generator",
+ "scoped-tls",
+ "tracing",
+ "tracing-subscriber",
+]
+
+[[package]]
name = "mac-notification-sys"
version = "0.6.1"
source = "registry+https://github.com/rust-lang/crates.io-index"
@@ -727,6 +748,15 @@ dependencies = [
]
[[package]]
+name = "matchers"
+version = "0.1.0"
+source = "registry+https://github.com/rust-lang/crates.io-index"
+checksum = "8263075bb86c5a1b1427b5ae862e8889656f126e9f77c484496e8b47cf5c5558"
+dependencies = [
+ "regex-automata 0.1.10",
+]
+
+[[package]]
name = "memchr"
version = "2.6.4"
source = "registry+https://github.com/rust-lang/crates.io-index"
@@ -767,6 +797,16 @@ dependencies = [
]
[[package]]
+name = "nu-ansi-term"
+version = "0.46.0"
+source = "registry+https://github.com/rust-lang/crates.io-index"
+checksum = "77a8165726e8236064dbb45459242600304b42a5ea24ee2948e18e023bf7ba84"
+dependencies = [
+ "overload",
+ "winapi",
+]
+
+[[package]]
name = "objc"
version = "0.2.7"
source = "registry+https://github.com/rust-lang/crates.io-index"
@@ -812,12 +852,27 @@ dependencies = [
]
[[package]]
+name = "overload"
+version = "0.1.1"
+source = "registry+https://github.com/rust-lang/crates.io-index"
+checksum = "b15813163c1d831bf4a13c3610c05c0d03b39feb07f7e09fa234dac9b15aaf39"
+
+[[package]]
name = "parking"
version = "2.2.0"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "bb813b8af86854136c6922af0598d719255ecb2179515e6e7730d468f05c9cae"
[[package]]
+name = "pausable_clock"
+version = "1.0.1"
+source = "registry+https://github.com/rust-lang/crates.io-index"
+checksum = "53bf074c4edd0314749976a1b244996fde867a568a3a40cd7e22355ac00da745"
+dependencies = [
+ "loom",
+]
+
+[[package]]
name = "pin-project-lite"
version = "0.2.13"
source = "registry+https://github.com/rust-lang/crates.io-index"
@@ -872,11 +927,11 @@ dependencies = [
[[package]]
name = "pomd"
-version = "0.1.0"
+version = "1.1.0"
dependencies = [
"async-std",
"notify-rust",
- "smol",
+ "pausable_clock",
"zbus",
]
@@ -987,8 +1042,17 @@ checksum = "380b951a9c5e80ddfd6136919eef32310721aa4aacd4889a8d39124b026ab343"
dependencies = [
"aho-corasick",
"memchr",
- "regex-automata",
- "regex-syntax",
+ "regex-automata 0.4.3",
+ "regex-syntax 0.8.2",
+]
+
+[[package]]
+name = "regex-automata"
+version = "0.1.10"
+source = "registry+https://github.com/rust-lang/crates.io-index"
+checksum = "6c230d73fb8d8c1b9c0b3135c5142a8acee3a0558fb8db5cf1cb65f8d7862132"
+dependencies = [
+ "regex-syntax 0.6.29",
]
[[package]]
@@ -999,11 +1063,17 @@ checksum = "5f804c7828047e88b2d32e2d7fe5a105da8ee3264f01902f796c8e067dc2483f"
dependencies = [
"aho-corasick",
"memchr",
- "regex-syntax",
+ "regex-syntax 0.8.2",
]
[[package]]
name = "regex-syntax"
+version = "0.6.29"
+source = "registry+https://github.com/rust-lang/crates.io-index"
+checksum = "f162c6dd7b008981e4d40210aca20b4bd0f9b60ca9271061b07f78537722f2e1"
+
+[[package]]
+name = "regex-syntax"
version = "0.8.2"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "c08c74e62047bb2de4ff487b251e4a92e24f48745648451635cec7d591162d9f"
@@ -1036,6 +1106,18 @@ dependencies = [
]
[[package]]
+name = "rustversion"
+version = "1.0.14"
+source = "registry+https://github.com/rust-lang/crates.io-index"
+checksum = "7ffc183a10b4478d04cbbbfc96d0873219d962dd5accaff2ffbd4ceb7df837f4"
+
+[[package]]
+name = "scoped-tls"
+version = "1.0.1"
+source = "registry+https://github.com/rust-lang/crates.io-index"
+checksum = "e1cf6437eb19a8f4a6cc0f7dca544973b0b78843adbfeb3683d1a94a0024a294"
+
+[[package]]
name = "serde"
version = "1.0.192"
source = "registry+https://github.com/rust-lang/crates.io-index"
@@ -1078,6 +1160,15 @@ dependencies = [
]
[[package]]
+name = "sharded-slab"
+version = "0.1.7"
+source = "registry+https://github.com/rust-lang/crates.io-index"
+checksum = "f40ca3c46823713e0d4209592e8d6e826aa57e928f09752619fc696c499637f6"
+dependencies = [
+ "lazy_static",
+]
+
+[[package]]
name = "signal-hook-registry"
version = "1.4.1"
source = "registry+https://github.com/rust-lang/crates.io-index"
@@ -1096,21 +1187,10 @@ dependencies = [
]
[[package]]
-name = "smol"
-version = "1.3.0"
+name = "smallvec"
+version = "1.11.2"
source = "registry+https://github.com/rust-lang/crates.io-index"
-checksum = "13f2b548cd8447f8de0fdf1c592929f70f4fc7039a05e47404b0d096ec6987a1"
-dependencies = [
- "async-channel",
- "async-executor",
- "async-fs",
- "async-io 1.13.0",
- "async-lock 2.8.0",
- "async-net",
- "async-process",
- "blocking",
- "futures-lite 1.13.0",
-]
+checksum = "4dccd0940a2dcdf68d092b8cbab7dc0ad8fa938bf95787e1b916b0e3d0e8e970"
[[package]]
name = "socket2"
@@ -1157,7 +1237,7 @@ source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "006851c9ccefa3c38a7646b8cec804bb429def3da10497bfa977179869c3e8e2"
dependencies = [
"quick-xml",
- "windows",
+ "windows 0.51.1",
]
[[package]]
@@ -1194,6 +1274,16 @@ dependencies = [
]
[[package]]
+name = "thread_local"
+version = "1.1.7"
+source = "registry+https://github.com/rust-lang/crates.io-index"
+checksum = "3fdd6f064ccff2d6567adcb3873ca630700f00b5ad3f060c25b5dcfd9a4ce152"
+dependencies = [
+ "cfg-if",
+ "once_cell",
+]
+
+[[package]]
name = "time"
version = "0.3.30"
source = "registry+https://github.com/rust-lang/crates.io-index"
@@ -1257,6 +1347,36 @@ source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "c06d3da6113f116aaee68e4d601191614c9053067f9ab7f6edbcb161237daa54"
dependencies = [
"once_cell",
+ "valuable",
+]
+
+[[package]]
+name = "tracing-log"
+version = "0.1.4"
+source = "registry+https://github.com/rust-lang/crates.io-index"
+checksum = "f751112709b4e791d8ce53e32c4ed2d353565a795ce84da2285393f41557bdf2"
+dependencies = [
+ "log",
+ "once_cell",
+ "tracing-core",
+]
+
+[[package]]
+name = "tracing-subscriber"
+version = "0.3.17"
+source = "registry+https://github.com/rust-lang/crates.io-index"
+checksum = "30a651bc37f915e81f087d86e62a18eec5f79550c7faff886f7090b4ea757c77"
+dependencies = [
+ "matchers",
+ "nu-ansi-term",
+ "once_cell",
+ "regex",
+ "sharded-slab",
+ "smallvec",
+ "thread_local",
+ "tracing",
+ "tracing-core",
+ "tracing-log",
]
[[package]]
@@ -1282,6 +1402,12 @@ source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "3354b9ac3fae1ff6755cb6db53683adb661634f67557942dea4facebec0fee4b"
[[package]]
+name = "valuable"
+version = "0.1.0"
+source = "registry+https://github.com/rust-lang/crates.io-index"
+checksum = "830b7e5d4d90034032940e4ace0d9a9a057e7a45cd94e6c007832e39edb82f6d"
+
+[[package]]
name = "value-bag"
version = "1.4.2"
source = "registry+https://github.com/rust-lang/crates.io-index"
@@ -1405,6 +1531,15 @@ checksum = "712e227841d057c1ee1cd2fb22fa7e5a5461ae8e48fa2ca79ec42cfc1931183f"
[[package]]
name = "windows"
+version = "0.48.0"
+source = "registry+https://github.com/rust-lang/crates.io-index"
+checksum = "e686886bc078bc1b0b600cac0147aadb815089b6e4da64016cbd754b6342700f"
+dependencies = [
+ "windows-targets",
+]
+
+[[package]]
+name = "windows"
version = "0.51.1"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "ca229916c5ee38c2f2bc1e9d8f04df975b4bd93f9955dc69fabb5d91270045c9"
diff --git a/Cargo.toml b/Cargo.toml
index 69a063a..7170ba4 100644
--- a/Cargo.toml
+++ b/Cargo.toml
@@ -1,9 +1,10 @@
[package]
name = "pomd"
-version = "0.1.0"
+version = "1.1.0"
edition = "2021"
[dependencies]
notify-rust = "4"
zbus = "3.14.1"
async-std = { version = "1.12.0", features = ["attributes"] }
+pausable_clock = "1.0.1"
diff --git a/src/main.rs b/src/main.rs
index 6be031c..7f4f6b7 100644
--- a/src/main.rs
+++ b/src/main.rs
@@ -1,8 +1,9 @@
use std::{
sync::{Arc, Mutex},
- time::{Duration, Instant},
+ time::Duration,
};
+use pausable_clock::{PausableClock, PausableInstant};
use zbus::{dbus_interface, ConnectionBuilder, Result};
use notify_rust::Notification;
@@ -13,11 +14,11 @@ const LONG_BREAK_DURATION_SECS: f32 = 15.0 * 60.0;
const NUM_ITERATIONS: u8 = 4;
struct Pomd {
- remaining: Duration,
+ duration: Duration,
iteration: u8,
- running: bool,
on_break: bool,
- last_instant: Instant,
+ clock: PausableClock,
+ start: PausableInstant
}
#[derive(Default)]
@@ -28,7 +29,8 @@ struct PomdInterface {
#[dbus_interface(name = "dev.exvacuum.pomd")]
impl PomdInterface {
async fn get_remaining(&self) -> Duration {
- self.data.lock().unwrap().remaining
+ let data = self.data.lock().unwrap();
+ data.duration.checked_sub(data.start.elapsed(&data.clock)).unwrap_or_default()
}
async fn get_iteration(&self) -> u8 {
@@ -36,7 +38,7 @@ impl PomdInterface {
}
async fn is_running(&self) -> bool {
- self.data.lock().unwrap().running
+ !self.data.lock().unwrap().clock.is_paused()
}
async fn is_on_break(&self) -> bool {
@@ -44,11 +46,11 @@ impl PomdInterface {
}
async fn start(&self) {
- self.data.lock().unwrap().running = true;
+ self.data.lock().unwrap().clock.resume();
}
async fn pause(&self) {
- self.data.lock().unwrap().running = false;
+ self.data.lock().unwrap().clock.pause();
}
async fn stop(&self) {
@@ -56,42 +58,42 @@ impl PomdInterface {
}
async fn skip(&self) {
- self.data.lock().unwrap().running = false;
self.data.lock().unwrap().setup_next_iteration();
}
}
impl Default for Pomd {
fn default() -> Self {
+ let clock = PausableClock::new(Duration::ZERO, true);
+ let start = clock.now();
Self {
- remaining: Duration::from_secs_f32(WORK_DURATION_SECS),
+ duration: Duration::from_secs_f32(WORK_DURATION_SECS),
iteration: 0,
- running: false,
on_break: false,
- last_instant: Instant::now(),
+ clock,
+ start,
}
}
}
impl Pomd {
fn update(&mut self) {
- let elapsed = self.last_instant.elapsed();
- self.last_instant = Instant::now();
- if self.running {
- if self.remaining > elapsed {
- self.remaining -= elapsed;
- } else {
- self.running = false;
+ if self.duration < self.start.elapsed(&self.clock) {
self.notify();
self.setup_next_iteration();
- }
}
}
fn setup_next_iteration(&mut self) {
+ self.clock.pause();
+ self.start = self.clock.now();
self.on_break ^= true;
- self.remaining = if self.on_break {
- if self.iteration == NUM_ITERATIONS-1 { Duration::from_secs_f32(LONG_BREAK_DURATION_SECS) } else { Duration::from_secs_f32(SHORT_BREAK_DURATION_SECS) }
+ self.duration = if self.on_break {
+ if self.iteration == NUM_ITERATIONS - 1 {
+ Duration::from_secs_f32(LONG_BREAK_DURATION_SECS)
+ } else {
+ Duration::from_secs_f32(SHORT_BREAK_DURATION_SECS)
+ }
} else {
self.iteration = (self.iteration + 1) % NUM_ITERATIONS;
Duration::from_secs_f32(WORK_DURATION_SECS)
@@ -103,12 +105,18 @@ impl Pomd {
Notification::new()
.summary("Break Complete")
.body("Click to dismiss")
- .show().unwrap();
+ .show()
+ .unwrap();
} else {
Notification::new()
- .summary(&format!("Pomodoro Complete ({}/{})", self.iteration + 1, NUM_ITERATIONS))
+ .summary(&format!(
+ "Pomodoro Complete ({}/{})",
+ self.iteration + 1,
+ NUM_ITERATIONS
+ ))
.body("Click to dismiss")
- .show().unwrap();
+ .show()
+ .unwrap();
}
}
}