summaryrefslogtreecommitdiff
path: root/.config/awesome/pomodoro.lua
blob: 26ae2a019e9370988f0fcd3e1b7c7f46dffb8e6d (plain)
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
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("<span color='red' weight='bold'> " .. val .. " </span>")
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)