This repository has been archived by the owner on May 25, 2023. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathbattery.lua
83 lines (77 loc) · 1.91 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
82
83
-- Source: http://awesome.naquadah.org/wiki/Closured_Battery_Widget
local io = io
local math = math
local naughty = naughty
local beautiful = beautiful
local tonumber = tonumber
local tostring = tostring
local print = print
local pairs = pairs
module("battery")
local limits = {{25, 5},
{12, 3},
{ 7, 1},
{0}}
function get_bat_state (adapter)
local fcur = io.open("/sys/class/power_supply/"..adapter.."/charge_now")
local fcap = io.open("/sys/class/power_supply/"..adapter.."/charge_full")
local fsta = io.open("/sys/class/power_supply/"..adapter.."/status")
local cur = fcur:read()
local cap = fcap:read()
local sta = fsta:read()
fcur:close()
fcap:close()
fsta:close()
local battery = math.floor(cur * 100 / cap)
if sta:match("Charging") then
dir = 1
elseif sta:match("Discharging") then
dir = -1
else
dir = 0
battery = ""
end
return battery, dir
end
function getnextlim (num)
for ind, pair in pairs(limits) do
lim = pair[1]; step = pair[2]; nextlim = limits[ind+1][1] or 0
if num > nextlim then
repeat
lim = lim - step
until num > lim
if lim < nextlim then
lim = nextlim
end
return lim
end
end
end
function batclosure (adapter)
local nextlim = limits[1][1]
return function ()
local prefix = "⚡"
local battery, dir = get_bat_state(adapter)
if dir == -1 then
dirsign = "↓"
prefix = "Bat:"
if battery <= nextlim then
naughty.notify({title = "⚡ Beware! ⚡",
text = "Battery charge is low ( ⚡ "..battery.."%)!",
timeout = 7,
position = "bottom_right",
fg = beautiful.fg_focus,
bg = beautiful.bg_focus
})
nextlim = getnextlim(battery)
end
elseif dir == 1 then
dirsign = "↑"
nextlim = limits[1][1]
else
dirsign = ""
end
if dir ~= 0 then battery = battery.."%" end
return " "..prefix.." "..dirsign..battery..dirsign.." "
end
end