-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathbattery.lua
More file actions
87 lines (73 loc) · 2.7 KB
/
battery.lua
File metadata and controls
87 lines (73 loc) · 2.7 KB
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
local wibox = require("wibox")
local awful = require("awful")
local naughty = require("naughty")
local helpers = require("./helpers")
local lain = require("lain")
markup = lain.util.markup
local widget = {}
-- {{{ Define adapter
local adapter = "BAT0"
local charge = "charge"
-- Test identifier
widget.hasbattery = helpers:test("cat /sys/class/power_supply/" .. adapter .. "/" .. charge .. "_now")
-- Try another identifier
if not widget.hasbattery then
charge = "energy"
widget.hasbattery = helpers:test("cat /sys/class/power_supply/" .. adapter .. "/" .. charge .. "_now")
end
-- }}}
-- {{{ Define subwidgets
widget.text = wibox.widget.textbox()
-- {{{ Update method
function widget:update()
local fcur = io.popen("cat /sys/class/power_supply/" ..adapter .. "/" .. charge .. "_now")
local fcap = io.popen("cat /sys/class/power_supply/" ..adapter .. "/" .. charge .. "_full")
local fsta = io.popen("cat /sys/class/power_supply/" ..adapter .. "/status")
local cur = fcur:read()
local cap = fcap:read()
local sta = fsta:read()
if cur and cap then
local battery = math.floor(cur * 100 / cap)
if(battery < 10) then
widget.text:set_markup(markup("#ff0000"," " .. battery .. "%"))
if sta:match("Charging") then
widget.text:set_markup(markup("#00ff00", " " .. "Charging"))
end
elseif(battery < 25) then
widget.text:set_markup(markup("#ff6600"," " .. battery .. "%"))
if sta:match("Charging") then
widget.text:set_markup(markup("#00ff00", " " .. "Charging"))
end
elseif(battery < 50) then
widget.text:set_markup(markup("#ffff00"," " .. battery .. "%"))
if sta:match("Charging") then
widget.text:set_markup(markup("#00ff00", " " .. "Charging"))
end
elseif(battery < 75) then
widget.text:set_markup(markup("#66ff00"," " .. battery .. "%"))
if sta:match("Charging") then
widget.text:set_markup(markup("#00ff00", " " .. "Charging"))
end
else
widget.text:set_markup(markup("#00ff00"," " .. battery .. "%"))
if sta:match("Charging") then
widget.text:set_markup(markup("#00ff00", " " .. "Charging"))
end
end
if(battery >= 100) then
widget.text:set_markup(markup("#00ff00", " " .. "full"))
end
else
widget.text:set_markup("N/A")
end
fcur:close()
fcap:close()
fsta:close()
end
-- }}}
-- {{{ Listen if signal was found
if widget.hasbattery then
helpers:listen(widget)
end
-- }}}
return widget