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
local DEFAULTS = {
snooze_key = "LCtrl+LAlt+B",
work_min = 25, break_min = 2, repeat_min = 10, snooze_min = 60,
sound = true, show_strip = true,
}
local cfg = rb.ui.schema({
snooze_key = rb.ui.keybind(DEFAULTS.snooze_key, { label = "Snooze",
tooltip = "Takes effect after you restart the script.", group = "Hotkey" }),
work_min = rb.ui.slider(DEFAULTS.work_min, { label = "Work stretch", min = 10, max = 90, step = 5, suffix = " min",
tooltip = "Minutes of nonstop input before Break Buddy nudges you.", group = "Behavior" }),
break_min = rb.ui.slider(DEFAULTS.break_min, { label = "Counts as a break", min = 1, max = 15, step = 1, suffix = " min",
tooltip = "Idle time that resets the clock.", group = "Behavior" }),
repeat_min = rb.ui.slider(DEFAULTS.repeat_min, { label = "Remind again every", min = 1, max = 30, step = 1, suffix = " min",
group = "Behavior" }),
snooze_min = rb.ui.slider(DEFAULTS.snooze_min, { label = "Snooze for", min = 5, max = 120, step = 5, suffix = " min",
group = "Behavior" }),
sound = rb.ui.toggle(DEFAULTS.sound, { label = "Play a beep", group = "Sound & strip" }),
show_strip = rb.ui.toggle(DEFAULTS.show_strip, { label = "Show the timer strip", group = "Sound & strip" }),
today = rb.ui.button("Today: 0m active · 0 breaks", { group = "Today" }),
reset = rb.ui.toggle(false, { label = "Reset to defaults", group = "Reset" }),
})
local MIN = 60 * 1000
local now = rb.system.time
local lastInput = now()
local streakStart = now()
local snoozeUntil = 0
local lastNudge = 0
local onBreak = false
local restedUntil = 0
local function dayKey()
local ok, d = pcall(function() return os.date("%Y-%m-%d") end)
if ok and d then return d end
return tostring(math.floor(now() / (24 * 60 * MIN)))
end
local today = { day = dayKey(), activeMs = 0, breaks = 0 }
local function activity() lastInput = now() end
rb.on("input_down", activity)
rb.on("input_scroll", activity)
local mx, my = rb.system.mouse()
local function fmt(ms)
local m = math.floor(ms / MIN)
if m < 60 then return m .. "m" end
return math.floor(m / 60) .. "h " .. (m % 60) .. "m"
end
local status = nil
local function syncStrip()
if cfg.show_strip and not status then
status = rb.overlay.status("Break Buddy", { anchor = "top-right" })
elseif not cfg.show_strip and status then
status.remove(); status = nil
end
end
pcall(rb.bind, cfg.snooze_key, function()
snoozeUntil = now() + cfg.snooze_min * MIN
rb.overlay.toast("Break Buddy snoozed for " .. fmt(cfg.snooze_min * MIN), { title = "Break Buddy" })
end)
local lastTick = now()
rb.timer.every(1000, function()
local t = now()
local dt = t - lastTick
lastTick = t
if cfg.reset then
for k, v in DEFAULTS do rb.ui.set(k, v) end
cfg.reset = false
rb.ui.notify("Settings reset to defaults.", "success")
end
local x, y = rb.system.mouse()
if x ~= mx or y ~= my then mx, my = x, y; lastInput = t end
if dayKey() ~= today.day then today = { day = dayKey(), activeMs = 0, breaks = 0 } end
syncStrip()
local workMs = cfg.work_min * MIN
if t - lastInput >= cfg.break_min * MIN then
if not onBreak then
onBreak = true
today.breaks += 1
if status then status.set({ { "On a break", "dim" } }) end
end
else
if onBreak then
onBreak = false
streakStart = lastInput
lastNudge = 0
restedUntil = t + 5000
end
today.activeMs += dt
local worked = t - streakStart
if worked >= workMs and t >= snoozeUntil and (lastNudge == 0 or t - lastNudge >= cfg.repeat_min * MIN) then
rb.overlay.toast(math.floor(worked / MIN) .. " minutes straight. Stand up, roll your wrists, look at something far away.",
{ title = "Break Buddy", duration = 8000 })
if cfg.sound then rb.audio.beep() end
lastNudge = t
end
if status then
if t < restedUntil then
status.set({ { "Rested ✓", "positive", true } })
else
local warn = worked >= workMs * 0.8
status.set({ { "Typing " .. fmt(worked), warn and "warning" or "dim", warn }, { " · break at " .. cfg.work_min .. "m", "dim" } })
end
end
end
cfg.today = { text = "Today: " .. fmt(today.activeMs) .. " active · " .. today.breaks .. (today.breaks == 1 and " break" or " breaks") }
end)
syncStrip()
rb.log.info("Break Buddy ready: nudge after " .. cfg.work_min .. "m, snooze with " .. tostring(cfg.snooze_key))