-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathbattery.lua
81 lines (64 loc) · 2.18 KB
/
battery.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
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
-----------------------------------------------------------------------------------
--/ battery stuffs /--
-----------------------------------------------------------------------------------
local imagePath = os.getenv("HOME") .. '/.hammerspoon/assets/';
local battery = {
rem = hs.battery.percentage(),
source = hs.battery.powerSource(),
icon = imagePath ..'battery-charging.pdf',
title = "Battery Status",
sound = "Sosumi",
min = 50,
showPercentage = false
}
-- notify when battery is full
function notifyWhenBatteryFullyCharged()
local currentPercentage = hs.battery.percentage()
if currentPercentage == 100 and battery.rem ~= currentPercentage and battery.source == 'AC Power' then
battery.rem = currentPercentage
hs.notify.new({
title = battery.title,
subTitle = 'Charged completely!',
contentImage = battery.icon,
soundName = battery.sound
}):send()
end
end
-- notify when battery is less than battery.min
function notifyWhenBatteryLow()
local currentPercentage = hs.battery.percentage()
if currentPercentage <= battery.min and battery.rem ~= currentPercentage and (currentPercentage % 5 == 0 ) then
battery.rem = currentPercentage
hs.notify.new({
title = battery.title,
informativeText = 'Battery left: '..battery.rem.."%\nPower Source: "..battery.source,
contentImage = battery.icon,
soundName = battery.sound
}):send()
end
end
-- alert battery source when it changes
function alertPowerSource()
local currentPowerSource= hs.battery.powerSource()
if battery.source ~= currentPowerSource then
battery.source = currentPowerSource
hs.alert.show(battery.source);
end
end
-- display battery percentage on menu bar
function showPercentageonNavbar()
local menuItem = hs.menubar.new(true)
local currentPercentage = hs.battery.percentage()
local remBatteryString = string.format("%.0f", currentPercentage)
menuItem:setTitle(remBatteryString.."%")
end
function watchBattery()
if battery.showPercentage then
showPercentageonNavbar()
end
alertPowerSource()
notifyWhenBatteryLow()
notifyWhenBatteryFullyCharged()
end
-- start watching
hs.battery.watcher.new(watchBattery):start()