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
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
|
/* 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 notify_rust::Notification;
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,
pub notify: bool,
}
impl Pomo {
pub fn new() -> Self {
Pomo {
secs_elapsed: 0,
counter: 1,
current_state: State::WorkIdle,
notify: get_envbool("NOTIFY", true),
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) {
if matches!(self.current_state, State::WorkIdle | State::BreakIdle | State::LongBreakIdle) {
if self.notify {
let _ = Notification::new()
.summary("Polydoro")
.body(match self.current_state {
State::WorkIdle => "The timer has been started. Get to work.",
State::BreakIdle => "The timer has been started. Take a break!",
State::LongBreakIdle => "The timer has been started. It's time for the much awaited long break!",
_ => "", // won't hit
})
.show();
}
}
if matches!(self.current_state, State::WorkPaused | State::BreakPaused | State::LongBreakPaused) {
if self.notify {
let _ = Notification::new()
.summary("Polydoro")
.body("Unpaused the timer")
.show();
}
}
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) {
if matches!(self.current_state, State::Work | State::Break | State::LongBreak) {
if self.notify {
let _ = Notification::new()
.summary("Polydoro")
.body("Paused the timer")
.show();
}
}
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
};
if self.notify {
let _ = Notification::new()
.summary("Polydoro")
.body(if self.current_state == State::BreakIdle {
"Time to take a break!"
} else {
"Time to take a long break!"
})
.show();
}
},
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;
if self.notify {
let _ = Notification::new()
.summary("Polydoro")
.body(format!("Your break has ended. Get to work!").as_str())
.show();
}
}
State::LongBreak => {
// just a hard reset but different notification
self.secs_elapsed = 0;
self.counter = 1;
self.current_state = State::WorkIdle;
if self.notify {
let _ = Notification::new()
.summary("Polydoro")
.body(format!("Long break has ended. Get to work!").as_str())
.show();
}
},
_ => 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)
};
if self.notify {
let _ = Notification::new()
.summary("Polydoro")
.body("Current timer has been reset")
.show();
}
}
// reset all cycles
pub fn hard_reset(&mut self) {
self.secs_elapsed = 0;
self.counter = 1;
self.current_state = State::WorkIdle;
if self.notify {
let _ = Notification::new()
.summary("Polydoro")
.body("All timers have been reset")
.show();
}
}
// 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(),
}
}
pub fn toggle_notify(&mut self) {
self.notify = !self.notify;
let _ = Notification::new()
.summary("Polydoro")
.body(if self.notify {
"Notifications have been turned on"
} else {
"Notifications have been turned off"
})
.show();
}
}
fn get_envvar(key: &str, def: u16) -> u16 {
env::var(key)
.ok()
.and_then(|v| v.parse::<u16>().ok())
.unwrap_or(def)
}
fn get_envbool(key: &str, def: bool) -> bool {
env::var(key)
.ok()
.and_then(|v| v.parse::<bool>().ok())
.unwrap_or(def)
}
|