-
Notifications
You must be signed in to change notification settings - Fork 0
/
caffeine.lua
62 lines (57 loc) · 1.63 KB
/
caffeine.lua
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
local caffeine = {
checked = hs.caffeinate.get('displayIdle'),
timer = nil,
}
-- Toggle the caffeinate state.
function caffeineToggle()
if hs.caffeinate.toggle('displayIdle') then
caffeine.checked = 1
hs.alert.show('Caffeine is now active.')
log({ 'Caffeine activated.' })
else
caffeine.checked = 0
hs.alert.show('Caffeine disabled.')
log({ 'Caffeine disabled.' })
end
end
-- Return checked state.
function caffeineChecked()
return caffeine.checked == 1 and true or false
end
-- Check remaining timer time.
function caffeineTimerTime()
if caffeine.checked == 1 and caffeine.timer then
local number = math.floor(tonumber(caffeine.timer:nextTrigger()))
if number < 1 then
number = 0
end
return number .. ' sec.'
else
return 'off'
end
end
-- Stop the timer.
function caffeineTimerStop()
caffeine.checked = 0
hs.caffeinate.set('displayIdle', false, true)
caffeine.timer:stop()
log({ 'Caffeine disabled.' })
end
-- Start the timer to finish in given seconds.
function caffeineTimerNew(interval)
return function()
if caffeine.checked == 1 and caffeine.timer then
caffeine.timer:stop()
end
caffeine.checked = 1
hs.caffeinate.set('displayIdle', true, true)
log({ 'Caffeine activated.' })
hs.alert.show('Caffeine is now active.')
if interval > 0 then
caffeine.timer = hs.timer.doAfter(interval, function()
hs.alert.show('Caffeine disabled.')
caffeineTimerStop()
end)
end
end
end