diff options
Diffstat (limited to 'src/pomo')
| -rw-r--r-- | src/pomo/mod.rs | 58 | ||||
| -rw-r--r-- | src/pomo/state.rs | 30 | ||||
| -rw-r--r-- | src/pomo/ui.rs | 130 |
3 files changed, 149 insertions, 69 deletions
diff --git a/src/pomo/mod.rs b/src/pomo/mod.rs index e979550..be55a0d 100644 --- a/src/pomo/mod.rs +++ b/src/pomo/mod.rs @@ -1,15 +1,15 @@ pub mod state; pub mod ui; -use crate::pomo::state::{Pomo, AppScreen, InputMode, Task, SessionMode, Config}; +use crate::pomo::state::{AppScreen, Config, InputMode, Pomo, SessionMode, Task}; use crossterm::{ event::{self, Event, KeyCode}, execute, - terminal::{disable_raw_mode, enable_raw_mode, EnterAlternateScreen, LeaveAlternateScreen}, + terminal::{EnterAlternateScreen, LeaveAlternateScreen, disable_raw_mode, enable_raw_mode}, }; -use ratatui::prelude::*; -use std::{ io, time::Duration, fs }; use directories::ProjectDirs; +use ratatui::prelude::*; +use std::{fs, io, time::Duration}; impl Pomo { pub fn save(&self) -> Result<(), Box<dyn std::error::Error>> { @@ -64,7 +64,7 @@ impl Pomo { _ = second_tick.tick() => { self.tick(); } - + // Tighten poll to 16ms (~60fps feel) for input responsiveness event_res = tokio::task::spawn_blocking(|| event::poll(Duration::from_millis(16))) => { if let Ok(Ok(true)) = event_res { @@ -114,8 +114,13 @@ impl Pomo { (AppScreen::Timer, KeyCode::Char('t')) => self.screen = AppScreen::Tasks, (AppScreen::Timer, KeyCode::Char(' ')) => self.is_running = !self.is_running, (AppScreen::Timer, KeyCode::Char('r')) => self.time_remaining = self.work_time, - (AppScreen::Tasks, KeyCode::Char('t')) | (AppScreen::Tasks, KeyCode::Esc) => self.screen = AppScreen::Timer, - (AppScreen::Tasks, KeyCode::Char('i')) => { self.input_mode = InputMode::Insert; self.input_buffer.clear(); } + (AppScreen::Tasks, KeyCode::Char('t')) | (AppScreen::Tasks, KeyCode::Esc) => { + self.screen = AppScreen::Timer + } + (AppScreen::Tasks, KeyCode::Char('i')) => { + self.input_mode = InputMode::Insert; + self.input_buffer.clear(); + } (AppScreen::Tasks, KeyCode::Char('e')) => self.enter_edit_mode(), (AppScreen::Tasks, KeyCode::Char('d')) => self.delete_task(), (AppScreen::Tasks, KeyCode::Char('j')) | (AppScreen::Tasks, KeyCode::Down) => { @@ -151,10 +156,15 @@ impl Pomo { } } - InputMode::Insert => self.tasks.push(Task { title: self.input_buffer.clone(), is_done: false }), + InputMode::Insert => self.tasks.push(Task { + title: self.input_buffer.clone(), + is_done: false, + }), - InputMode::Edit => if let Some(i) = self.task_state.selected() { - self.tasks[i].title = self.input_buffer.clone(); + InputMode::Edit => { + if let Some(i) = self.task_state.selected() { + self.tasks[i].title = self.input_buffer.clone(); + } } _ => {} @@ -163,8 +173,12 @@ impl Pomo { self.input_mode = InputMode::Normal; } KeyCode::Esc => self.input_mode = InputMode::Normal, - KeyCode::Backspace => { self.input_buffer.pop(); } - KeyCode::Char(c) => { self.input_buffer.push(c); } + KeyCode::Backspace => { + self.input_buffer.pop(); + } + KeyCode::Char(c) => { + self.input_buffer.push(c); + } _ => {} } } @@ -179,7 +193,9 @@ impl Pomo { fn delete_task(&mut self) { if let Some(i) = self.task_state.selected() { self.tasks.remove(i); - if self.tasks.is_empty() { self.task_state.select(None); } + if self.tasks.is_empty() { + self.task_state.select(None); + } } } @@ -191,7 +207,13 @@ impl Pomo { fn next_task(&mut self) { let i = match self.task_state.selected() { - Some(i) => if i >= self.tasks.len() - 1 { 0 } else { i + 1 }, + Some(i) => { + if i >= self.tasks.len() - 1 { + 0 + } else { + i + 1 + } + } None => 0, }; self.task_state.select(Some(i)); @@ -199,7 +221,13 @@ impl Pomo { fn previous_task(&mut self) { let i = match self.task_state.selected() { - Some(i) => if i == 0 { self.tasks.len() - 1 } else { i - 1 }, + Some(i) => { + if i == 0 { + self.tasks.len() - 1 + } else { + i - 1 + } + } None => 0, }; self.task_state.select(Some(i)); diff --git a/src/pomo/state.rs b/src/pomo/state.rs index ec66255..8574d38 100644 --- a/src/pomo/state.rs +++ b/src/pomo/state.rs @@ -1,29 +1,41 @@ +use notify_rust::Notification; use ratatui::widgets::ListState; +use serde::{Deserialize, Serialize}; use std::time::Duration; -use serde::{ Serialize, Deserialize }; -use notify_rust::Notification; #[derive(PartialEq, Clone, Copy)] -pub enum SessionMode { Work, ShortBreak, LongBreak } +pub enum SessionMode { + Work, + ShortBreak, + LongBreak, +} #[derive(PartialEq, Clone, Copy)] -pub enum AppScreen { Timer, Tasks } +pub enum AppScreen { + Timer, + Tasks, +} #[derive(PartialEq, Clone, Copy)] -pub enum InputMode { Normal, Insert, Edit, TimerEdit } +pub enum InputMode { + Normal, + Insert, + Edit, + TimerEdit, +} #[derive(Serialize, Deserialize)] pub struct Config { pub work_time_mins: u64, pub short_break_mins: u64, pub long_break_mins: u64, - pub tasks: Vec<Task> + pub tasks: Vec<Task>, } #[derive(Serialize, Deserialize, Clone)] pub struct Task { pub title: String, - pub is_done: bool + pub is_done: bool, } pub struct Pomo { @@ -40,7 +52,7 @@ pub struct Pomo { pub tasks: Vec<Task>, pub task_state: ListState, pub input_buffer: String, - pub should_quit: bool + pub should_quit: bool, } impl Pomo { @@ -60,7 +72,7 @@ impl Pomo { tasks: Vec::new(), task_state: ListState::default(), input_buffer: String::new(), - should_quit: false + should_quit: false, } } diff --git a/src/pomo/ui.rs b/src/pomo/ui.rs index 116dbca..864d6d0 100644 --- a/src/pomo/ui.rs +++ b/src/pomo/ui.rs @@ -1,5 +1,5 @@ +use crate::pomo::state::{AppScreen, InputMode, Pomo, SessionMode}; use ratatui::{prelude::*, widgets::*}; -use crate::pomo::state::{Pomo, AppScreen, SessionMode, InputMode}; const MOCHA_LAVENDER: Color = Color::Rgb(180, 190, 254); const MOCHA_OVERLAY0: Color = Color::Rgb(108, 112, 134); @@ -15,7 +15,7 @@ pub fn render(f: &mut Frame, app: &mut Pomo) { .constraints([ Constraint::Min(0), Constraint::Length(1), - Constraint::Length(1) + Constraint::Length(1), ]) .split(f.area()); @@ -26,7 +26,7 @@ pub fn render(f: &mut Frame, app: &mut Pomo) { .constraints([ Constraint::Fill(1), Constraint::Length(15), - Constraint::Fill(1) + Constraint::Fill(1), ]) .split(root_layout[0]); @@ -37,11 +37,11 @@ pub fn render(f: &mut Frame, app: &mut Pomo) { Paragraph::new(footer) .alignment(Alignment::Center) .style(Style::default().fg(MOCHA_OVERLAY0)), - root_layout[1] + root_layout[1], ); } AppScreen::Tasks => { - render_task_screen(f, app, root_layout[1]); + render_task_screen(f, app, root_layout[1]); } } @@ -63,22 +63,27 @@ fn render_timer_screen(f: &mut Frame, app: &Pomo, area: Rect) { ]) .split(area); - let priority_text = app.tasks.iter().find(|t| !t.is_done) + let priority_text = app + .tasks + .iter() + .find(|t| !t.is_done) .map(|t| format!("Current Focus: {}", t.title)) .unwrap_or_else(|| "No Active Tasks".to_string()); - + f.render_widget( Paragraph::new(priority_text) .alignment(Alignment::Center) - .style(Style::default().fg(MOCHA_LAVENDER).bold()), - chunks[0] + .style(Style::default().fg(MOCHA_LAVENDER).bold()), + chunks[0], ); let time_str = format_duration(app.time_remaining); let big_text = format_monolithic_ascii(&time_str); f.render_widget( - Paragraph::new(big_text).alignment(Alignment::Center).style(Style::default().fg(MOCHA_LAVENDER)), - chunks[2] + Paragraph::new(big_text) + .alignment(Alignment::Center) + .style(Style::default().fg(MOCHA_LAVENDER)), + chunks[2], ); render_session_dots(f, app, chunks[4]); @@ -99,46 +104,76 @@ fn format_monolithic_ascii(time: &str) -> Text<'static> { '7' => vec!["████████", " ██", " ██ ", " ██ ", " ██ "], '8' => vec![" ██████ ", "██ ██", " ██████ ", "██ ██", " ██████ "], '9' => vec![" ██████ ", "██ ██", " ████████", " ██", " ██████ "], - ':' => vec![" █ ", " ", " █ ", " ", " "], - _ => vec![" "; 5], + ':' => vec![" █ ", " ", " █ ", " ", " "], + _ => vec![" "; 5], }; for i in 0..5 { lines[i].push_str(art[i]); - if idx < time.len() - 1 { lines[i].push_str(" "); } + if idx < time.len() - 1 { + lines[i].push_str(" "); + } } } Text::from(lines.into_iter().map(Line::from).collect::<Vec<_>>()) } fn render_session_dots(f: &mut Frame, app: &Pomo, area: Rect) { - let modes = [(SessionMode::Work, "Focus"), (SessionMode::ShortBreak, "Short Break"), (SessionMode::LongBreak, "Long Break")]; - let spans = modes.iter().enumerate().map(|(i, (mode, label))| { - let is_active = app.mode == *mode; - let color = if is_active { MOCHA_LAVENDER } else { MOCHA_OVERLAY0 }; - let content = if is_active { format!("• {}", label) } else { label.to_string() }; - let mut s = vec![Span::styled(content, Style::default().fg(color))]; - if i < modes.len() - 1 { s.push(Span::raw(" ")); } - s - }).flatten().collect::<Vec<_>>(); - - f.render_widget(Paragraph::new(Line::from(spans)).alignment(Alignment::Center), area); + let modes = [ + (SessionMode::Work, "Focus"), + (SessionMode::ShortBreak, "Short Break"), + (SessionMode::LongBreak, "Long Break"), + ]; + let spans = modes + .iter() + .enumerate() + .map(|(i, (mode, label))| { + let is_active = app.mode == *mode; + let color = if is_active { + MOCHA_LAVENDER + } else { + MOCHA_OVERLAY0 + }; + let content = if is_active { + format!("• {}", label) + } else { + label.to_string() + }; + let mut s = vec![Span::styled(content, Style::default().fg(color))]; + if i < modes.len() - 1 { + s.push(Span::raw(" ")); + } + s + }) + .flatten() + .collect::<Vec<_>>(); + + f.render_widget( + Paragraph::new(Line::from(spans)).alignment(Alignment::Center), + area, + ); } pub fn render_task_screen(f: &mut Frame, app: &mut Pomo, footer_area: Rect) { let area = centered_rect(60, 80, f.area()); - let items: Vec<ListItem> = app.tasks.iter().map(|t| { - let symbol = if t.is_done { "" } else { "" }; - ListItem::new(Text::from(format!(" {} {}", symbol, t.title))) - }).collect(); + let items: Vec<ListItem> = app + .tasks + .iter() + .map(|t| { + let symbol = if t.is_done { "" } else { "" }; + ListItem::new(Text::from(format!(" {} {}", symbol, t.title))) + }) + .collect(); let list = List::new(items) - .block(Block::default() - .title(" Focus Priorities ") - .borders(Borders::ALL) - .border_type(BorderType::Rounded) - .padding(Padding::uniform(1)) - .border_style(Style::default().fg(MOCHA_LAVENDER))) + .block( + Block::default() + .title(" Focus Priorities ") + .borders(Borders::ALL) + .border_type(BorderType::Rounded) + .padding(Padding::uniform(1)) + .border_style(Style::default().fg(MOCHA_LAVENDER)), + ) .highlight_style(Style::default().bg(MOCHA_SURFACE0).fg(MOCHA_TEXT).bold()) .highlight_symbol(">> "); @@ -149,21 +184,21 @@ pub fn render_task_screen(f: &mut Frame, app: &mut Pomo, footer_area: Rect) { Paragraph::new(footer_text) .alignment(Alignment::Center) .style(Style::default().fg(MOCHA_OVERLAY0)), - footer_area + footer_area, ); } pub fn render_input_modal(f: &mut Frame, app: &Pomo) { - let (title, width) = match app.input_mode { - InputMode::TimerEdit => (" Set Minutes ", 30), - _ => (" Input ", 50), + let (title, width) = match app.input_mode { + InputMode::TimerEdit => (" Set Minutes ", 30), + _ => (" Input ", 50), }; // Instead of using the utility, we define the area directly to ensure zero drift. let terminal_area = f.area(); let modal_width = width.min(terminal_area.width.saturating_sub(4)); let modal_height = 5; // Tighter vertical profile - + let area = Rect { x: terminal_area.x + (terminal_area.width.saturating_sub(modal_width)) / 2, y: terminal_area.y + (terminal_area.height.saturating_sub(modal_height)) / 2, @@ -177,11 +212,14 @@ pub fn render_input_modal(f: &mut Frame, app: &Pomo) { InputMode::Insert => " New Task ", InputMode::Edit => " Edit Task ", InputMode::TimerEdit => " Set Minutes ", - _ => title + _ => title, }; let block = Block::default() - .title(Span::styled(title_text, Style::default().fg(MOCHA_LAVENDER).bold())) + .title(Span::styled( + title_text, + Style::default().fg(MOCHA_LAVENDER).bold(), + )) .borders(Borders::ALL) .border_type(BorderType::Rounded) .border_style(Style::default().fg(MOCHA_LAVENDER)); @@ -199,7 +237,9 @@ pub fn render_input_modal(f: &mut Frame, app: &Pomo) { let horizontal_padding = 2; let input_len = app.input_buffer.len() as u16; - let max_width = vertical_chunks[1].width.saturating_sub(horizontal_padding * 2); + let max_width = vertical_chunks[1] + .width + .saturating_sub(horizontal_padding * 2); let scroll = input_len.saturating_sub(max_width); f.render_widget(block, area); @@ -208,8 +248,8 @@ pub fn render_input_modal(f: &mut Frame, app: &Pomo) { Paragraph::new(app.input_buffer.as_str()) .scroll((0, scroll)) .block(Block::default().padding(Padding::horizontal(horizontal_padding))) - .style(Style::default().fg(MOCHA_TEXT).bold()), - vertical_chunks[1] + .style(Style::default().fg(MOCHA_TEXT).bold()), + vertical_chunks[1], ); f.set_cursor_position(( |