Break Buddy

@xzenile / break-buddyv1.2.0Public

Shows how long you've been going without a real pause, and nudges you to rest your hands.

Files

Version 1.2.0 (current)

break-buddy.luau5.6 KB

--[[
  rebind: min_sdk=3.19.0
  rebind: name=Break Buddy
  rebind: version=1.2.0
  rebind: author=PLACEHOLDER
  rebind: description=Shows how long you've been going without a real pause, and nudges you to rest your hands.
  rebind: permissions=none
  rebind: instance=single
]]

-- Break Buddy
--   The timer strip shows how long you've typed without a real break.
--   After "Work stretch" minutes with no "Counts as a break" pause: a toast and a beep.
--   A real pause resets the clock. The Snooze hotkey pushes the next nudge back.
--   All settings live in Rebind's Options tab and apply on the next tick (the hotkey after a restart).
-- Observes input only: no blocking, no key names stored, counts only. Needs no permissions.

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

-- "Today" counts (numbers only; reset when the date changes or the script restarts).
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))) -- UTC day if os.date is unavailable
end
local today = { day = dayKey(), activeMs = 0, breaks = 0 }

-- Keys, clicks and scrolls via observers (they never capture or block input).
local function activity() lastInput = now() end
rb.on("input_down", activity)
rb.on("input_scroll", activity)
-- Mouse movement: poll the cursor once a second instead of an input_move observer.
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

-- Hotkey is read once at start, as the tooltip says.
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 -- back from a real break: fresh clock, quietly
      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 -- still heard when a full-screen game hides overlays
      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))

3 files, 8.0 KB.