1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
|
/* Polydoro - Pomodoro widget for polybar and friends
* Copyright (C) 2025 Vidhu Kant Sharma <vidhukant@vidhukant.com>
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program. If not, see <https://www.gnu.org/licenses/>.
*/
use bincode::{Encode, Decode};
use std::env;
#[derive(Encode, Decode, PartialEq, Clone, Copy)]
pub enum State {
WorkIdle,
BreakIdle,
LongBreakIdle,
Work,
Break,
LongBreak,
WorkPaused,
BreakPaused,
LongBreakPaused,
}
#[derive(Encode, Decode)]
pub struct Pomo {
long_break_interval: u16,
pub work_duration: u16,
pub break_duration: u16,
pub long_break_duration: u16,
// only these are supposed to change
pub secs_elapsed: u16,
pub counter: u16,
pub current_state: State,
}
impl Pomo {
pub fn new() -> Self {
Pomo {
secs_elapsed: 0,
counter: 1,
current_state: State::WorkIdle,
work_duration: get_envvar("WORK_DURATION", 25 * 60),
break_duration: get_envvar("BREAK_DURATION", 5 * 60),
long_break_duration: get_envvar("LONG_BREAK_DURATION", 20 * 60),
long_break_interval: get_envvar("LONG_BREAK_INTERVAL", 4),
}
}
pub fn tick(&mut self) {
// only tick on running states
if matches!(self.current_state, State::Work | State::Break | State::LongBreak) {
self.secs_elapsed += 1;
let duration = match self.current_state {
State::Work => self.work_duration,
State::Break => self.break_duration,
State::LongBreak => self.long_break_duration,
_ => 0, // will never hit this case
};
if self.secs_elapsed >= duration {
self.end_cycle();
}
}
}
// start/unpause a cycle
pub fn run(&mut self) {
self.current_state = match self.current_state {
State::WorkIdle => State::Work,
State::BreakIdle => State::Break,
State::LongBreakIdle => State::LongBreak,
State::WorkPaused => State::Work,
State::BreakPaused => State::Break,
State::LongBreakPaused => State::LongBreak,
_ => return // already running
};
}
pub fn pause(&mut self) {
self.current_state = match self.current_state {
State::Work => State::WorkPaused,
State::Break => State::BreakPaused,
State::LongBreak => State::LongBreakPaused,
_ => return, // idle and paused don't have a place here
};
}
// conditionally pause/run
pub fn toggle(&mut self) {
match self.current_state {
State::Work | State::Break | State::LongBreak => self.pause(),
_ => self.run(),
}
}
// also used in the skip command
pub fn end_cycle(&mut self) {
match self.current_state {
State::Work => {
self.secs_elapsed = 0;
self.current_state = if self.counter == self.long_break_interval {
State::LongBreakIdle
} else {
State::BreakIdle
};
},
State::Break => {
self.secs_elapsed = 0;
self.current_state = State::WorkIdle;
// because we move on to the next task after the break
self.counter += 1;
}
State::LongBreak => self.hard_reset(),
_ => return, // paused and idle state can't be ended
}
}
// reset current cycle
pub fn soft_reset(&mut self) {
self.secs_elapsed = 0;
self.current_state = match self.current_state {
State::Work | State::WorkPaused => State::WorkIdle,
State::Break | State::BreakPaused => State::BreakIdle,
State::LongBreak | State::LongBreakPaused => State::LongBreakIdle,
_ => self.current_state, // if already in an idle state, don't change (probably won't hit anyways)
};
}
// reset all cycles
pub fn hard_reset(&mut self) {
self.secs_elapsed = 0;
self.counter = 1;
self.current_state = State::WorkIdle;
}
// conditionally do a soft or a hard reset
pub fn reset(&mut self) {
match self.current_state {
State::WorkIdle | State::BreakIdle | State::LongBreakIdle => self.hard_reset(),
_ => self.soft_reset(),
}
}
}
fn get_envvar(key: &str, def: u16) -> u16 {
env::var(key)
.ok()
.and_then(|v| v.parse::<u16>().ok())
.unwrap_or(def)
}
|