local setmetatable = setmetatable local math = math local string = string local gears = require("gears") local naughty = require("naughty") local textbox = require("wibox.widget.textbox") local glib = require("lgi").GLib local DateTime = glib.DateTime local TimeZone = glib.TimeZone local pomodoro = { mt = {} } local work_time = 25 * 60 local positions = {"bottom_right", "bottom_left", "bottom_right"} -- pomodoro is a little pomodoro timer "app" that will show a notification on -- all screens with a picture of a tomato after 25 minutes. It facilitates -- using the pomodoro technique with nothing more than awesome. function pomodoro:_timer_running() return self._timer ~= nil and self._timer.data.source_id ~= nil end function pomodoro:_set_text(val) self:set_markup(" " .. val .. " ") end function pomodoro:_clear_text() self:set_markup("") end function pomodoro:stop() self:_set_text("Pomodoro Stopped") self._timer:stop() self:_reset_text_later() end function pomodoro:_reset_text_later() gears.timer { timeout = 30, autostart = true, single_shot = true, callback = function() self:_clear_text() end } end function pomodoro:_notify_complete() for s = 1, screen.count() do naughty.notify({ icon=gears.filesystem.get_dir("config") .. "icons/pomodoro.png", position=positions[s], timeout=30, screen=s, title = "Pomodoro Over", }) end self:_set_text("Pomodoro Over") end function pomodoro:_update_callback() local now = DateTime.new_now(TimeZone.new_local()):to_unix() local time_left = work_time - (now - self._start_time) local mins_left = math.floor(time_left / 60) local secs_left = math.floor(time_left % 60) if mins_left == 0 and secs_left == 0 then self._timer:stop() self:_notify_complete() self:_reset_text_later() return end self:_set_text(string.format("%02d:%02d", mins_left, secs_left)) end function pomodoro:start() if self:_timer_running() then return end self._start_time = DateTime.new_now(TimeZone.new_local()):to_unix() self._timer = gears.timer { timeout = 1, autostart = true, callback = function() return self:_update_callback() end } end local function new() local w = textbox() gears.table.crush(w, pomodoro, true) return w end function pomodoro.mt.__call(...) return new(...) end return setmetatable(pomodoro, pomodoro.mt)