Skip to content

Commit a1c4603

Browse files
Merge pull request #5 from QuentinGruber/feature/long_break
long breaks
2 parents af1c8fd + 6039961 commit a1c4603

File tree

4 files changed

+48
-3
lines changed

4 files changed

+48
-3
lines changed

lua/pomodoro/pomodoro.lua

+34-2
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,9 @@ local Phases = constants.Phases
99
---@class Pomodoro
1010
---@field work_duration number
1111
---@field break_duration number
12+
---@field long_break_duration number
13+
---@field breaks_before_long number
14+
---@field break_count number
1215
---@field timer_duration number
1316
---@field start_at_launch boolean
1417
---@field timer uv_timer_t
@@ -19,8 +22,12 @@ local pomodoro = {}
1922
pomodoro.work_duration = 25 * MIN_IN_MS
2023
-- Break duration in ms
2124
pomodoro.break_duration = 5 * MIN_IN_MS
25+
-- Break duration in ms
26+
pomodoro.long_break_duration = 15 * MIN_IN_MS
2227
-- Delay duration in ms
2328
pomodoro.delay_duration = 1 * MIN_IN_MS
29+
pomodoro.break_count = 0
30+
pomodoro.breaks_before_long = 4
2431
pomodoro.timer_duration = 0
2532
pomodoro.start_at_launch = true
2633
pomodoro.timer = uv.new_timer()
@@ -56,11 +63,25 @@ function pomodoro.closePomodoroUi()
5663
UI.close()
5764
end
5865

66+
---@return boolean
67+
function pomodoro.isInLongBreak()
68+
return pomodoro.break_count % (pomodoro.breaks_before_long + 1) == 0
69+
and pomodoro.phase == Phases.BREAK
70+
end
71+
5972
function pomodoro.startBreak()
60-
info("Break of " .. pomodoro.break_duration / MIN_IN_MS .. "m started!")
6173
pomodoro.phase = Phases.BREAK
74+
pomodoro.break_count = pomodoro.break_count + 1
75+
local break_duration
76+
if pomodoro.isInLongBreak() then
77+
break_duration = pomodoro.long_break_duration
78+
else
79+
break_duration = pomodoro.break_duration
80+
end
81+
82+
info("Break of " .. break_duration / MIN_IN_MS .. "m started!")
6283
vim.schedule(pomodoro.displayPomodoroUI)
63-
pomodoro.startTimer(pomodoro.break_duration, pomodoro.endBreak)
84+
pomodoro.startTimer(break_duration, pomodoro.endBreak)
6485
end
6586

6687
function pomodoro.endBreak()
@@ -79,7 +100,10 @@ end
79100

80101
function pomodoro.delayBreak()
81102
if pomodoro.phase == Phases.BREAK then
103+
info("Break delayed")
82104
pomodoro.phase = Phases.RUNNING
105+
-- So if a long break is delayed the next break is still a long one
106+
pomodoro.break_count = pomodoro.break_count - 1
83107
pomodoro.closePomodoroUi()
84108
pomodoro.startTimer(MIN_IN_MS, pomodoro.startBreak)
85109
end
@@ -117,7 +141,9 @@ end
117141
---@class PomodoroOpts
118142
---@field work_duration? number
119143
---@field break_duration? number
144+
---@field long_break_duration? number
120145
---@field delay_duration? number
146+
---@field breaks_before_long? number
121147
---@field start_at_launch? boolean
122148

123149
---@param opts PomodoroOpts
@@ -129,9 +155,15 @@ function pomodoro.setup(opts)
129155
if opts.break_duration ~= nil then
130156
pomodoro.break_duration = opts.break_duration * MIN_IN_MS
131157
end
158+
if opts.long_break_duration ~= nil then
159+
pomodoro.long_break_duration = opts.long_break_duration * MIN_IN_MS
160+
end
132161
if opts.delay_duration ~= nil then
133162
pomodoro.delay_duration = opts.delay_duration * MIN_IN_MS
134163
end
164+
if opts.breaks_before_long ~= nil then
165+
pomodoro.breaks_before_long = opts.breaks_before_long
166+
end
135167
if opts.start_at_launch ~= nil then
136168
pomodoro.start_at_launch = opts.start_at_launch
137169
end

lua/pomodoro/tests/pomodoro_spec.lua

+8
Original file line numberDiff line numberDiff line change
@@ -9,10 +9,18 @@ describe("useless tests", function()
99
opts.work_duration = 10
1010
opts.break_duration = 10
1111
opts.delay_duration = 10
12+
opts.long_break_duration = 15
13+
opts.breaks_before_long = 4
1214
opts.start_at_launch = false
1315
pomodoro.setup(opts)
16+
-- TODO: use the opts value directly
1417
assert(pomodoro.work_duration == 10 * MIN_IN_MS, "Opt work_duration")
1518
assert(pomodoro.break_duration == 10 * MIN_IN_MS, "Opt break_duration")
19+
assert(pomodoro.breaks_before_long == 4, "Opt breaks_before_long")
20+
assert(
21+
pomodoro.long_break_duration == 15 * MIN_IN_MS,
22+
"Opt long_break_duration"
23+
)
1624
assert(pomodoro.delay_duration == 10 * MIN_IN_MS, "Opt delay_duration")
1725
assert(pomodoro.start_at_launch == false, "Opt start_at_launch")
1826
end)

lua/pomodoro/ui.lua

+5-1
Original file line numberDiff line numberDiff line change
@@ -121,7 +121,11 @@ function UI.get_buffer_data(pomodoro)
121121
table.insert(data, "")
122122
table.insert(data, center("[B]reak [Q]uit", width))
123123
elseif pomodoro.phase == Phases.BREAK then
124-
table.insert(data, center("☕ BREAK TIME", width))
124+
if pomodoro.isInLongBreak() then
125+
table.insert(data, center("☕ LONG BREAK TIME", width))
126+
else
127+
table.insert(data, center("☕ BREAK TIME", width))
128+
end
125129
table.insert(data, center(time_left_string, width))
126130
table.insert(
127131
data,

lua/pomodoro/uv.lua

+1
Original file line numberDiff line numberDiff line change
@@ -6,4 +6,5 @@ else
66
uv = vim.loop
77
end
88

9+
-- TODO: need to be typed
910
return uv

0 commit comments

Comments
 (0)