diff options
| author | Saiyed Shizain <saiyedshizain@gmail.com> | 2026-01-18 22:12:25 +0530 |
|---|---|---|
| committer | Saiyed Shizain <saiyedshizain@gmail.com> | 2026-01-18 22:12:25 +0530 |
| commit | 921e2220bdea2eb6c0e03c0bac8abe431f9698a1 (patch) | |
| tree | 4143ead206ed7c4fd0f1859ca7ba7155a842f070 | |
| parent | 2a40c1bc7a1869f921c3428546f81f3f1cb26e07 (diff) | |
| download | pomoru-921e2220bdea2eb6c0e03c0bac8abe431f9698a1.tar.xz pomoru-921e2220bdea2eb6c0e03c0bac8abe431f9698a1.zip | |
add panic handler & implement tokio tick
| -rw-r--r-- | Cargo.lock | 115 | ||||
| -rw-r--r-- | Cargo.toml | 2 | ||||
| -rw-r--r-- | src/main.rs | 22 | ||||
| -rw-r--r-- | src/pomo/mod.rs | 69 |
4 files changed, 169 insertions, 39 deletions
@@ -93,6 +93,12 @@ source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "1fbdf580320f38b612e485521afda1ee26d10cc9884efaaa750d383e13e3c5f4" [[package]] +name = "bytes" +version = "1.11.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "b35204fbdc0b3f4446b89fc1ac2cf84a8a68971995d0bf2e925ec7cd960f9cb3" + +[[package]] name = "castaway" version = "0.2.4" source = "registry+https://github.com/rust-lang/crates.io-index" @@ -301,7 +307,7 @@ source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "39cab71617ae0d63f51a36d69f866391735b51691dbda63cf6f96d042b63efeb" dependencies = [ "libc", - "windows-sys", + "windows-sys 0.61.2", ] [[package]] @@ -576,7 +582,7 @@ dependencies = [ "libc", "log", "wasi", - "windows-sys", + "windows-sys 0.61.2", ] [[package]] @@ -979,7 +985,7 @@ dependencies = [ "errno", "libc", "linux-raw-sys", - "windows-sys", + "windows-sys 0.61.2", ] [[package]] @@ -1091,6 +1097,16 @@ source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "67b1b7a3b5fe4f1376887184045fcf45c69e92af734b7aaddc05fb777b6fbd03" [[package]] +name = "socket2" +version = "0.6.1" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "17129e116933cf371d018bb80ae557e889637989d8638274fb25622827b03881" +dependencies = [ + "libc", + "windows-sys 0.60.2", +] + +[[package]] name = "static_assertions" version = "1.1.0" source = "registry+https://github.com/rust-lang/crates.io-index" @@ -1275,7 +1291,26 @@ version = "1.49.0" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "72a2903cd7736441aac9df9d7688bd0ce48edccaadf181c3b90be801e81d3d86" dependencies = [ + "bytes", + "libc", + "mio", + "parking_lot", "pin-project-lite", + "signal-hook-registry", + "socket2", + "tokio-macros", + "windows-sys 0.61.2", +] + +[[package]] +name = "tokio-macros" +version = "2.6.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "af407857209536a95c8e56f8231ef2c2e2aff839b22e07a1ffcbc617e9db9fa5" +dependencies = [ + "proc-macro2", + "quote", + "syn 2.0.114", ] [[package]] @@ -1514,6 +1549,15 @@ checksum = "f0805222e57f7521d6a62e36fa9163bc891acd422f971defe97d64e70d0a4fe5" [[package]] name = "windows-sys" +version = "0.60.2" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "f2f500e4d28234f72040990ec9d39e3a6b950f9f22d3dba18416c35882612bcb" +dependencies = [ + "windows-targets", +] + +[[package]] +name = "windows-sys" version = "0.61.2" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "ae137229bcbd6cdf0f7b80a31df61766145077ddf49416a728b02cb3921ff3fc" @@ -1522,6 +1566,71 @@ dependencies = [ ] [[package]] +name = "windows-targets" +version = "0.53.5" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "4945f9f551b88e0d65f3db0bc25c33b8acea4d9e41163edf90dcd0b19f9069f3" +dependencies = [ + "windows-link", + "windows_aarch64_gnullvm", + "windows_aarch64_msvc", + "windows_i686_gnu", + "windows_i686_gnullvm", + "windows_i686_msvc", + "windows_x86_64_gnu", + "windows_x86_64_gnullvm", + "windows_x86_64_msvc", +] + +[[package]] +name = "windows_aarch64_gnullvm" +version = "0.53.1" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "a9d8416fa8b42f5c947f8482c43e7d89e73a173cead56d044f6a56104a6d1b53" + +[[package]] +name = "windows_aarch64_msvc" +version = "0.53.1" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "b9d782e804c2f632e395708e99a94275910eb9100b2114651e04744e9b125006" + +[[package]] +name = "windows_i686_gnu" +version = "0.53.1" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "960e6da069d81e09becb0ca57a65220ddff016ff2d6af6a223cf372a506593a3" + +[[package]] +name = "windows_i686_gnullvm" +version = "0.53.1" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "fa7359d10048f68ab8b09fa71c3daccfb0e9b559aed648a8f95469c27057180c" + +[[package]] +name = "windows_i686_msvc" +version = "0.53.1" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "1e7ac75179f18232fe9c285163565a57ef8d3c89254a30685b57d83a38d326c2" + +[[package]] +name = "windows_x86_64_gnu" +version = "0.53.1" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "9c3842cdd74a865a8066ab39c8a7a473c0778a3f29370b5fd6b4b9aa7df4a499" + +[[package]] +name = "windows_x86_64_gnullvm" +version = "0.53.1" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "0ffa179e2d07eee8ad8f57493436566c7cc30ac536a3379fdf008f47f6bb7ae1" + +[[package]] +name = "windows_x86_64_msvc" +version = "0.53.1" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "d6bbff5f0aada427a1e5a6da5f1f98158182f26556f345ac9e04d36d0ebed650" + +[[package]] name = "wit-bindgen" version = "0.51.0" source = "registry+https://github.com/rust-lang/crates.io-index" @@ -7,4 +7,4 @@ edition = "2024" crossterm = "0.29.0" ratatui = "0.30.0" serde = "1.0.228" -tokio = "1.49.0" +tokio = { version = "1.49.0", features = ["full"] } diff --git a/src/main.rs b/src/main.rs index 1f11a9e..bd5deb5 100644 --- a/src/main.rs +++ b/src/main.rs @@ -1,8 +1,26 @@ mod pomo; use pomo::state::Pomo; +use std::panic; +use crossterm::terminal::{disable_raw_mode, LeaveAlternateScreen}; +use crossterm::execute; -fn main() -> Result<(), Box<dyn std::error::Error>> { +fn setup_panic_hook() { + let original_hook = panic::take_hook(); + panic::set_hook(Box::new(move |panic_info| { + // Restore terminal state before exiting + let _ = disable_raw_mode(); + let mut stdout = std::io::stdout(); + let _ = execute!(stdout, LeaveAlternateScreen); + + // Call the original hook to print the error + original_hook(panic_info); + })); +} + +#[tokio::main] +async fn main() -> Result<(), Box<dyn std::error::Error>> { + setup_panic_hook(); let mut app = Pomo::new(); - app.run()?; + app.run().await?; Ok(()) } diff --git a/src/pomo/mod.rs b/src/pomo/mod.rs index 9c02653..ac58537 100644 --- a/src/pomo/mod.rs +++ b/src/pomo/mod.rs @@ -3,57 +3,42 @@ pub mod ui; use crate::pomo::state::Pomo; use crossterm::{ - event::{self, Event, KeyCode}, + event::{self, Event, KeyCode, KeyModifiers }, execute, terminal::{disable_raw_mode, enable_raw_mode, EnterAlternateScreen, LeaveAlternateScreen}, }; use ratatui::prelude::*; -use std::io; -use std::time::{Duration, Instant}; -use crossterm::event::KeyModifiers; +use std::{ io, time::Duration }; +use tokio::time::{self, MissedTickBehavior}; impl Pomo { - pub fn run(&mut self) -> io::Result<()> { + pub async fn run(&mut self) -> io::Result<()> { enable_raw_mode()?; let mut stdout = io::stdout(); execute!(stdout, EnterAlternateScreen)?; let backend = CrosstermBackend::new(stdout); let mut terminal = Terminal::new(backend)?; - let mut last_tick = Instant::now(); - let tick_rate = Duration::from_millis(250); + let mut interval = time::interval(Duration::from_millis(250)); + interval.set_missed_tick_behavior(MissedTickBehavior::Skip); + + let mut second_tick = time::interval(Duration::from_secs(1)); while !self.should_quit { terminal.draw(|f| ui::render(f, self))?; - let timeout = tick_rate - .checked_sub(last_tick.elapsed()) - .unwrap_or_else(|| Duration::from_secs(0)); - - if event::poll(timeout)? { - if let Event::Key(key) = event::read()? { - match key.code { - KeyCode::Char('q') => self.should_quit = true, - KeyCode::Char('?') => self.show_help = !self.show_help, - KeyCode::Char('h') if key.modifiers.contains(KeyModifiers::CONTROL) => { - self.show_help = !self.show_help - }, - // Only allow other inputs if help isn't showing (to keep it clean) - _ if !self.show_help => match key.code { - KeyCode::Char(' ') => self.toggle_timer(), - KeyCode::Char('j') | KeyCode::Down => self.next_task(), - KeyCode::Char('k') | KeyCode::Up => self.previous_task(), - KeyCode::Enter => self.toggle_task(), - _ => {} - }, - _ => {} + tokio::select! { + // Handle key events + _ = interval.tick() => { + if event::poll(Duration::from_millis(0))? { + if let Event::Key(key) = event::read()? { + self.handle_key(key); + } } } - } - - if last_tick.elapsed() >= Duration::from_secs(1) { - self.tick(); - last_tick = Instant::now(); + _ = second_tick.tick() => { + self.tick(); + } } } @@ -61,4 +46,22 @@ impl Pomo { execute!(terminal.backend_mut(), LeaveAlternateScreen)?; Ok(()) } + + fn handle_key(&mut self, key: event::KeyEvent) { + match key.code { + KeyCode::Char('q') => self.should_quit = true, + KeyCode::Char('?') => self.show_help = !self.show_help, + KeyCode::Char('h') if key.modifiers.contains(KeyModifiers::CONTROL) => { + self.show_help = !self.show_help + } + _ if !self.show_help => match key.code { + KeyCode::Char(' ') => self.toggle_timer(), + KeyCode::Char('j') | KeyCode::Down => self.next_task(), + KeyCode::Char('k') | KeyCode::Up => self.previous_task(), + KeyCode::Enter => self.toggle_task(), + _ => {} + }, + _ => {} + } + } } |