From bb66e0f10c86f3b2e5a3a8c20d910c4df8d05ac7 Mon Sep 17 00:00:00 2001 From: Saiyed Shizain Date: Sun, 18 Jan 2026 21:02:45 +0530 Subject: idiomatic rust --- src/pomo/mod.rs | 41 +++++++++++++++++++++++++++++++++++++++++ src/pomo/state.rs | 49 +++++++++++++++++++++++++++++++++++++++++++++++++ 2 files changed, 90 insertions(+) create mode 100644 src/pomo/mod.rs create mode 100644 src/pomo/state.rs (limited to 'src/pomo') 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, + 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; + } +} -- cgit v1.2.3