-
Notifications
You must be signed in to change notification settings - Fork 0
/
battery.lua
92 lines (68 loc) · 2.35 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
84
85
86
87
88
89
90
91
92
local wibox = require("wibox")
local awful = require("awful")
local naughty = require("naughty")
local helpers = require("./helpers")
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()
widget.icon = wibox.widget.imagebox()
-- Change the draw method so icons can be drawn smaller
helpers:set_draw_method(widget.icon)
-- }}}
-- {{{ Define interactive behaviour
widget.icon:buttons(awful.util.table.join(
-- awful.button({ }, 1, function () awful.util.spawn("gnome-control-center power") end)
))
-- }}}
-- {{{ 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)
local iconpath = "/home/marc/.config/awesome/themes/current/icons/gnome/scalable/status/battery"
if(battery < 10) then
iconpath = iconpath .. "-caution"
elseif(battery < 25) then
iconpath = iconpath .. "-low"
elseif(battery < 75) then
iconpath = iconpath .. "-good"
else
iconpath = iconpath .. "-full"
end
if sta:match("Charging") then
iconpath = iconpath .. "-charging"
elseif sta:match("Discharging") then
end
iconpath = iconpath .. "-symbolic.svg"
widget.icon:set_image(iconpath)
widget.text:set_markup(battery .. "%")
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