aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
-rw-r--r--crates/pomoru-core/src/task.rs164
-rw-r--r--crates/pomoru-core/src/timer.rs192
-rw-r--r--src/pomo/mod.rs1
-rw-r--r--src/pomo/state.rs162
4 files changed, 382 insertions, 137 deletions
diff --git a/crates/pomoru-core/src/task.rs b/crates/pomoru-core/src/task.rs
new file mode 100644
index 0000000..d5127e9
--- /dev/null
+++ b/crates/pomoru-core/src/task.rs
@@ -0,0 +1,164 @@
+use serde::{Deserialize, Serialize};
+
+#[derive(Debug, Clone, Serialize, Deserialize)]
+pub struct Task {
+ pub title: String,
+ pub is_done: bool,
+}
+
+#[derive(Debug, Clone, Serialize, Deserialize)]
+pub struct TaskList {
+ items: Vec<Task>,
+ #[serde(skip)]
+ selected: Option<usize>,
+}
+
+impl Default for TaskList {
+ fn default() -> Self {
+ Self::new()
+ }
+}
+
+impl TaskList {
+ pub fn new() -> Self {
+ Self {
+ items: Vec::new(),
+ selected: None,
+ }
+ }
+
+ pub fn from_tasks(tasks: Vec<Task>) -> Self {
+ let selected = if tasks.is_empty() { None } else { Some(0) };
+ Self {
+ items: tasks,
+ selected,
+ }
+ }
+
+ pub fn items(&self) -> &[Task] {
+ &self.items
+ }
+
+ pub fn into_items(self) -> Vec<Task> {
+ self.items
+ }
+
+ pub fn selected(&self) -> Option<usize> {
+ self.selected
+ }
+
+ pub fn select(&mut self, index: Option<usize>) {
+ self.selected = index;
+ }
+
+ // Returns the first incomplete task (the "active" focus task).
+ pub fn active_task(&self) -> Option<&Task> {
+ self.items.iter().find(|t| !t.is_done)
+ }
+
+ pub fn add(&mut self, title: String) {
+ self.items.push(Task {
+ title,
+ is_done: false,
+ });
+ if self.selected.is_none() {
+ self.selected = Some(0);
+ }
+ }
+
+ pub fn edit(&mut self, index: usize, title: String) {
+ if let Some(task) = self.items.get_mut(index) {
+ task.title = title;
+ }
+ }
+
+ pub fn delete(&mut self, index: usize) {
+ if index < self.items.len() {
+ self.items.remove(index);
+ if self.items.is_empty() {
+ self.selected = None;
+ } else if let Some(sel) = self.selected
+ && sel >= self.items.len()
+ {
+ self.selected = Some(self.items.len() - 1);
+ }
+ }
+ }
+
+ pub fn toggle(&mut self, index: usize) {
+ if let Some(task) = self.items.get_mut(index) {
+ task.is_done = !task.is_done;
+ }
+ }
+
+ pub fn select_next(&mut self) {
+ if self.items.is_empty() {
+ return;
+ }
+ let i = match self.selected {
+ Some(i) => {
+ if i >= self.items.len() - 1 {
+ 0
+ } else {
+ i + 1
+ }
+ }
+ None => 0,
+ };
+ self.selected = Some(i);
+ }
+
+ pub fn select_previous(&mut self) {
+ if self.items.is_empty() {
+ return;
+ }
+ let i = match self.selected {
+ Some(i) => {
+ if i == 0 {
+ self.items.len() - 1
+ } else {
+ i - 1
+ }
+ }
+ None => 0,
+ };
+ self.selected = Some(i);
+ }
+
+ pub fn move_up(&mut self) {
+ match self.selected {
+ Some(0) | None => (),
+ Some(i) => {
+ self.items.swap(i, i - 1);
+ self.selected = Some(i - 1);
+ }
+ }
+ }
+
+ pub fn move_down(&mut self) {
+ match self.selected {
+ None => (),
+ Some(i) if i >= self.items.len() - 1 => (),
+ Some(i) => {
+ self.items.swap(i, i + 1);
+ self.selected = Some(i + 1);
+ }
+ }
+ }
+
+ pub fn move_task(&mut self, from: usize, to: usize) {
+ if from < self.items.len() && to < self.items.len() && from != to {
+ let task = self.items.remove(from);
+ self.items.insert(to, task);
+ self.selected = Some(to);
+ }
+ }
+
+ pub fn len(&self) -> usize {
+ self.items.len()
+ }
+
+ pub fn is_empty(&self) -> bool {
+ self.items.is_empty()
+ }
+} \ No newline at end of file
diff --git a/crates/pomoru-core/src/timer.rs b/crates/pomoru-core/src/timer.rs
new file mode 100644
index 0000000..7c6ab81
--- /dev/null
+++ b/crates/pomoru-core/src/timer.rs
@@ -0,0 +1,192 @@
+use crate::session::SessionMode;
+use serde::{Deserialize, Serialize};
+use std::time::Duration;
+
+// Event emitted by `TimerState::tick()` when a session completes.
+#[derive(Debug, Clone, Serialize, Deserialize)]
+pub struct TimerEvent {
+ pub title: String,
+ pub message: String,
+}
+
+// Pure timer state
+#[derive(Debug, Clone, Serialize, Deserialize)]
+pub struct TimerState {
+ pub mode: SessionMode,
+ pub work_time_secs: u64,
+ pub short_break_secs: u64,
+ pub long_break_secs: u64,
+ pub time_remaining_secs: u64,
+ pub total_duration_secs: u64,
+ pub is_running: bool,
+ pub auto_switch_sessions: bool,
+ pub break_count: u32,
+}
+
+impl Default for TimerState {
+ fn default() -> Self {
+ let work_secs: u64 = 25 * 60;
+ Self {
+ mode: SessionMode::Work,
+ work_time_secs: work_secs,
+ short_break_secs: 5 * 60,
+ long_break_secs: 15 * 60,
+ time_remaining_secs: work_secs,
+ total_duration_secs: work_secs,
+ is_running: false,
+ auto_switch_sessions: true,
+ break_count: 0,
+ }
+ }
+}
+
+impl TimerState {
+ pub fn new() -> Self {
+ Self::default()
+ }
+
+ pub fn tick(&mut self) -> Option<TimerEvent> {
+ if self.is_running && self.time_remaining_secs > 0 {
+ self.time_remaining_secs -= 1;
+ None
+ } else if self.is_running && self.time_remaining_secs == 0 {
+ let focus_msg = [
+ "I'm tired, boss...",
+ "Congrats! You're him 🗿",
+ "Stand up. Touch grass.",
+ "Mission Passed! Respect+",
+ ];
+ let break_msg = [
+ "Ah shit, here we go again.",
+ "Wake up, Samurai. We have code to burn.",
+ "Lock back in.",
+ "Ref! Do Something! The break's over!",
+ ];
+
+ let idx = (self.break_count as usize) % 4;
+
+ let (title, msg) = match self.mode {
+ SessionMode::Work => ("Focus Block Complete", focus_msg[idx]),
+ _ => ("Break Over", break_msg[idx]),
+ };
+
+ let event = TimerEvent {
+ title: title.to_string(),
+ message: msg.to_string(),
+ };
+
+ if self.auto_switch_sessions {
+ self.transition_next_session();
+ self.is_running = true;
+ } else {
+ self.is_running = false;
+ self.reset_timer_to_mode();
+ }
+
+ Some(event)
+ } else {
+ None
+ }
+ }
+
+ fn transition_next_session(&mut self) {
+ match self.mode {
+ SessionMode::Work => {
+ self.break_count += 1;
+ if self.break_count.is_multiple_of(3) {
+ self.mode = SessionMode::LongBreak;
+ self.time_remaining_secs = self.long_break_secs;
+ self.total_duration_secs = self.long_break_secs;
+ } else {
+ self.mode = SessionMode::ShortBreak;
+ self.time_remaining_secs = self.short_break_secs;
+ self.total_duration_secs = self.short_break_secs;
+ }
+ }
+ _ => {
+ self.mode = SessionMode::Work;
+ self.time_remaining_secs = self.work_time_secs;
+ self.total_duration_secs = self.work_time_secs;
+ }
+ }
+ }
+
+ pub fn reset_timer_to_mode(&mut self) {
+ self.time_remaining_secs = match self.mode {
+ SessionMode::Work => self.work_time_secs,
+ SessionMode::ShortBreak => self.short_break_secs,
+ SessionMode::LongBreak => self.long_break_secs,
+ };
+ self.total_duration_secs = self.time_remaining_secs;
+ }
+
+ pub fn set_mode(&mut self, mode: SessionMode) {
+ if !self.is_running {
+ self.mode = mode;
+ self.reset_timer_to_mode();
+ }
+ }
+
+ pub fn cycle_mode(&mut self) {
+ if !self.is_running {
+ self.mode = self.mode.next();
+ self.reset_timer_to_mode();
+ }
+ }
+
+ pub fn toggle_running(&mut self) {
+ self.is_running = !self.is_running;
+ }
+
+ pub fn start(&mut self) {
+ self.is_running = true;
+ }
+
+ pub fn pause(&mut self) {
+ self.is_running = false;
+ }
+
+ pub fn reset(&mut self) {
+ self.is_running = false;
+ self.reset_timer_to_mode();
+ }
+
+ pub fn skip_session(&mut self) {
+ self.transition_next_session();
+ self.is_running = false;
+ }
+
+ pub fn toggle_auto_switch(&mut self) {
+ self.auto_switch_sessions = !self.auto_switch_sessions;
+ }
+
+ pub fn set_current_duration_mins(&mut self, mins: u64) {
+ let secs = mins * 60;
+ match self.mode {
+ SessionMode::Work => self.work_time_secs = secs,
+ SessionMode::ShortBreak => self.short_break_secs = secs,
+ SessionMode::LongBreak => self.long_break_secs = secs,
+ }
+ self.time_remaining_secs = secs;
+ self.total_duration_secs = secs;
+ }
+
+ pub fn time_remaining(&self) -> Duration {
+ Duration::from_secs(self.time_remaining_secs)
+ }
+ pub fn total_duration(&self) -> Duration {
+ Duration::from_secs(self.total_duration_secs)
+ }
+
+ pub fn progress(&self) -> f64 {
+ if self.total_duration_secs == 0 {
+ return 0.0;
+ }
+ 1.0 - (self.time_remaining_secs as f64 / self.total_duration_secs as f64)
+ }
+}
+
+pub fn format_duration(duration: Duration) -> String {
+ let secs = duration.as_secs();
+ format!("{:02}:{:02}", secs / 60, secs % 60)
+}
diff --git a/src/pomo/mod.rs b/src/pomo/mod.rs
index 404151b..259a18a 100644
--- a/src/pomo/mod.rs
+++ b/src/pomo/mod.rs
@@ -190,7 +190,6 @@ impl Pomo {
}
}
- /// Sync the ratatui ListState with the core TaskList selection.
fn sync_task_state(&mut self) {
self.task_state.select(self.tasks.selected());
}
diff --git a/src/pomo/state.rs b/src/pomo/state.rs
index 83e2038..674dd83 100644
--- a/src/pomo/state.rs
+++ b/src/pomo/state.rs
@@ -1,8 +1,11 @@
use directories::BaseDirs;
-use notify_rust::Notification;
+use pomoru_core::{
+ task::TaskList,
+ timer::TimerState,
+};
use ratatui::{style::Color, widgets::ListState};
-use serde::{Deserialize, Serialize};
-use std::{fs, time::Duration};
+use serde::Deserialize;
+use std::fs;
#[derive(Clone, Copy)]
pub struct Theme {
@@ -65,13 +68,6 @@ impl Default for Theme {
}
#[derive(PartialEq, Clone, Copy)]
-pub enum SessionMode {
- Work,
- ShortBreak,
- LongBreak,
-}
-
-#[derive(PartialEq, Clone, Copy)]
pub enum AppScreen {
Timer,
Tasks,
@@ -85,48 +81,13 @@ pub enum InputMode {
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>,
-
- #[serde(default = "default_auto_switch")]
- pub auto_switch_sessions: bool,
-}
-
-fn default_auto_switch() -> bool {
- true
-}
-
-#[derive(Serialize)]
-pub struct CurrentStatus {
- pub text: String,
- pub tooltip: String,
- pub class: String,
-}
-
-#[derive(Serialize, Deserialize, Clone)]
-pub struct Task {
- pub title: String,
- pub is_done: bool,
-}
-
+/// Thin TUI wrapper around core types.
pub struct Pomo {
+ pub timer: TimerState,
+ pub tasks: TaskList,
+ pub task_state: ListState,
pub screen: AppScreen,
- pub mode: SessionMode,
pub input_mode: InputMode,
- pub work_time: Duration,
- pub short_break_time: Duration,
- pub long_break_time: Duration,
- pub time_remaining: Duration,
- pub total_duration: Duration,
- pub is_running: bool,
- pub auto_switch_sessions: bool,
- pub break_count: u32,
- pub tasks: Vec<Task>,
- pub task_state: ListState,
pub input_buffer: String,
pub should_quit: bool,
pub theme: Theme,
@@ -134,101 +95,30 @@ pub struct Pomo {
impl Pomo {
pub fn new() -> Self {
- let work = Duration::from_secs(25 * 60);
Self {
+ timer: TimerState::new(),
+ tasks: TaskList::new(),
+ task_state: ListState::default(),
screen: AppScreen::Timer,
- mode: SessionMode::Work,
input_mode: InputMode::Normal,
- work_time: work,
- short_break_time: Duration::from_secs(5 * 60),
- long_break_time: Duration::from_secs(15 * 60),
- time_remaining: work,
- total_duration: work,
- is_running: false,
- auto_switch_sessions: true,
- break_count: 0,
- tasks: Vec::new(),
- task_state: ListState::default(),
input_buffer: String::new(),
should_quit: false,
theme: Theme::default(),
}
}
- 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.is_running && self.time_remaining.as_secs() == 0 {
- let focus_msg = [
- "I'm tired, boss...",
- "Congrats! You're him 🗿",
- "Stand up. Touch grass.",
- "Mission Passed! Respect+",
- ];
- let break_msg = [
- "Ah shit, here we go again.",
- "Wake up, Samurai. We have code to burn.",
- "Lock back in.",
- "Ref! Do Something! The break's over!",
- ];
-
- // Use the remaining duration/break count as a seed for simple 'random' selection
- let idx = (self.break_count as usize) % 4;
-
- let (title, msg) = match self.mode {
- SessionMode::Work => ("Focus Block Complete", focus_msg[idx]),
- _ => ("Break Over", break_msg[idx]),
- };
-
- self.send_notification(title, msg);
-
- if self.auto_switch_sessions {
- self.transition_next_session();
- self.is_running = true;
- } else {
- self.is_running = false;
- self.reset_timer_to_mode();
- }
- }
- }
-
- fn transition_next_session(&mut self) {
- match self.mode {
- SessionMode::Work => {
- self.break_count += 1;
- if self.break_count.is_multiple_of(3) {
- self.mode = SessionMode::LongBreak;
- self.time_remaining = self.long_break_time;
- self.total_duration = self.long_break_time;
- } else {
- self.mode = SessionMode::ShortBreak;
- self.time_remaining = self.short_break_time;
- self.total_duration = self.short_break_time;
- }
- }
- _ => {
- self.mode = SessionMode::Work;
- self.time_remaining = self.work_time;
- self.total_duration = self.work_time;
- }
- }
- }
-
- pub fn reset_timer_to_mode(&mut self) {
- self.time_remaining = match self.mode {
- SessionMode::Work => self.work_time,
- SessionMode::ShortBreak => self.short_break_time,
- SessionMode::LongBreak => self.long_break_time,
- };
- self.total_duration = self.time_remaining;
- }
-
- pub fn send_notification(&self, title: &str, message: &str) {
- let _ = Notification::new()
- .summary(title)
- .body(message)
- .appname("pomoru")
- .timeout(5000)
- .show();
+ pub fn load() -> Self {
+ let config = pomoru_core::config::load();
+ let mut app = Pomo::new();
+ app.timer.work_time_secs = config.work_time_mins * 60;
+ app.timer.short_break_secs = config.short_break_mins * 60;
+ app.timer.long_break_secs = config.long_break_mins * 60;
+ app.timer.auto_switch_sessions = config.auto_switch_sessions;
+ app.timer.reset_timer_to_mode();
+ app.tasks = TaskList::from_tasks(config.tasks);
+ app
}
}
+
+// Re-export core types used by other TUI modules.
+pub use pomoru_core::session::SessionMode;