diff options
| author | Saiyed Shizain <saiyedshizain@gmail.com> | 2026-01-18 21:02:45 +0530 |
|---|---|---|
| committer | Saiyed Shizain <saiyedshizain@gmail.com> | 2026-01-18 21:02:45 +0530 |
| commit | bb66e0f10c86f3b2e5a3a8c20d910c4df8d05ac7 (patch) | |
| tree | a5907978eec8d26184ed4817cb844da111d944e1 | |
| parent | 8f4d471f3b3a6d25b0ba3d3c2b9abbdce7d20312 (diff) | |
| download | pomoru-bb66e0f10c86f3b2e5a3a8c20d910c4df8d05ac7.tar.xz pomoru-bb66e0f10c86f3b2e5a3a8c20d910c4df8d05ac7.zip | |
idiomatic rust
| -rw-r--r-- | src/main.rs | 2 | ||||
| -rw-r--r-- | src/pomo.rs | 58 | ||||
| -rw-r--r-- | src/pomo/mod.rs | 41 | ||||
| -rw-r--r-- | src/pomo/state.rs | 49 |
4 files changed, 91 insertions, 59 deletions
diff --git a/src/main.rs b/src/main.rs index 349d2d2..1f11a9e 100644 --- a/src/main.rs +++ b/src/main.rs @@ -1,5 +1,5 @@ mod pomo; -use pomo::Pomo; +use pomo::state::Pomo; fn main() -> Result<(), Box<dyn std::error::Error>> { let mut app = Pomo::new(); diff --git a/src/pomo.rs b/src/pomo.rs deleted file mode 100644 index 256b2ce..0000000 --- a/src/pomo.rs +++ /dev/null @@ -1,58 +0,0 @@ -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) { - 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) -> Result<(), Box<dyn std::error::Error>> { - enable_raw_mode()?; - - self.time_remaining = Duration::from_secs(10); - - 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(()) - } -} diff --git a/src/pomo/mod.rs b/src/pomo/mod.rs new file mode 100644 index 0000000..7759877 --- /dev/null +++ b/src/pomo/mod.rs @@ -0,0 +1,41 @@ +pub mod state; +pub mod ui; + +use crate::pomo::state::Pomo; +use crossterm::{ + event::{self, Event, KeyCode}, + execute, + terminal::{disable_raw_mode, enable_raw_mode, EnterAlternateScreen, LeaveAlternateScreen}, +}; +use ratatui::prelude::*; +use std::io; +use std::time::Duration; + +impl Pomo { + pub 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)?; + + while !self.should_quit { + terminal.draw(|f| ui::render(f, self))?; + + if event::poll(Duration::from_millis(250))? { + if let Event::Key(key) = event::read()? { + match key.code { + KeyCode::Char('q') => self.should_quit = true, + KeyCode::Char(' ') => self.toggle_timer(), + _ => {} + } + } + } + self.tick(); + } + + disable_raw_mode()?; + execute!(terminal.backend_mut(), LeaveAlternateScreen)?; + Ok(()) + } +} diff --git a/src/pomo/state.rs b/src/pomo/state.rs new file mode 100644 index 0000000..111e935 --- /dev/null +++ b/src/pomo/state.rs @@ -0,0 +1,49 @@ +use ratatui::widgets::ListState; +use std::time::Duration; + +#[derive(PartialEq, Clone, Copy)] +pub enum SessionMode { + Work, + ShortBreak, + LongBreak, +} + +pub struct Task { + pub title: String, + pub is_done: bool, +} + +pub struct Pomo { + pub mode: SessionMode, + pub time_remaining: Duration, + pub is_running: bool, + pub tasks: Vec<Task>, + pub task_state: ListState, + pub should_quit: bool, +} + +impl Pomo { + pub fn new() -> Self { + Self { + mode: SessionMode::Work, + time_remaining: Duration::from_secs(25 * 60), + is_running: false, + tasks: Vec::new(), + task_state: ListState::default(), + should_quit: false, + } + } + + pub fn tick(&mut self) { + 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; + // logic for switching modes will go here + } + } + + pub fn toggle_timer(&mut self) { + self.is_running = !self.is_running; + } +} |