aboutsummaryrefslogtreecommitdiff
path: root/src
diff options
context:
space:
mode:
authorSaiyed Shizain <saiyedshizain@gmail.com>2026-01-18 20:21:28 +0530
committerSaiyed Shizain <saiyedshizain@gmail.com>2026-01-18 20:21:28 +0530
commit8f4d471f3b3a6d25b0ba3d3c2b9abbdce7d20312 (patch)
tree57142b2ed3065d5e73c5e71d01d165514cb4b5bf /src
parentcde182d44fe4dcaa3d206ceb5cf093f74de6c85e (diff)
downloadpomoru-8f4d471f3b3a6d25b0ba3d3c2b9abbdce7d20312.tar.xz
pomoru-8f4d471f3b3a6d25b0ba3d3c2b9abbdce7d20312.zip
add timer toggle and panic safe tick
Diffstat (limited to 'src')
-rw-r--r--src/main.rs5
-rw-r--r--src/pomo.rs45
2 files changed, 41 insertions, 9 deletions
diff --git a/src/main.rs b/src/main.rs
index 324d34e..349d2d2 100644
--- a/src/main.rs
+++ b/src/main.rs
@@ -1,7 +1,8 @@
mod pomo;
use pomo::Pomo;
-fn main() {
+fn main() -> Result<(), Box<dyn std::error::Error>> {
let mut app = Pomo::new();
- app.run();
+ app.run()?;
+ Ok(())
}
diff --git a/src/pomo.rs b/src/pomo.rs
index a5042ef..256b2ce 100644
--- a/src/pomo.rs
+++ b/src/pomo.rs
@@ -1,27 +1,58 @@
-use std::{ thread, time::Duration };
+use std::time::{ Duration, Instant };
+use crossterm::event::{ self, Event, KeyCode };
+use crossterm::terminal::{ enable_raw_mode, disable_raw_mode };
+use std::io::{ stdout, Write };
pub struct Pomo {
pub time_remaining: Duration,
+ pub is_running: bool,
}
impl Pomo {
pub fn new() -> Self {
Self {
time_remaining: Duration::from_secs(25 * 60),
+ is_running: false,
}
}
pub fn tick(&mut self) {
- self.time_remaining -= Duration::from_secs(1);
+ if self.is_running && self.time_remaining.as_secs() > 0 {
+ self.time_remaining -= Duration::from_secs(1);
+ } else if self.time_remaining.as_secs() == 0 {
+ self.is_running = false;
+ }
+ }
+
+ pub fn toggle_timer(&mut self) {
+ self.is_running = !self.is_running;
}
- pub fn run(&mut self) {
+ pub fn run(&mut self) -> Result<(), Box<dyn std::error::Error>> {
+ enable_raw_mode()?;
+
self.time_remaining = Duration::from_secs(10);
- while self.time_remaining > Duration::from_secs(0) {
- println!("{}", self.time_remaining.as_secs());
- self.tick();
- thread::sleep(Duration::from_secs(1));
+
+ let mut last_tick = Instant::now();
+
+ while self.time_remaining > Duration::ZERO {
+ if event::poll(Duration::from_millis(50))? {
+ if let Event::Key(key) = event::read()? {
+ if key.code == KeyCode::Char(' ') {
+ self.toggle_timer();
+ }
+ }
+ }
+
+ if self.is_running && last_tick.elapsed() >= Duration::from_secs(1) {
+ println!("{}", self.time_remaining.as_secs());
+ self.tick();
+ last_tick = Instant::now();
+ }
}
+
println!("Done");
+ disable_raw_mode()?;
+ Ok(())
}
}