All patches and comments are welcome. Please squash your changes to logical
commits before using git-format-patch and git-send-email to
patches@git.madduck.net.
If you'd read over the Git project's submission guidelines and adhered to them,
I'd be especially grateful.
4 Licensed under GNU General Public License v2
5 * (c) 2013, Luke Bonham
6 * (c) 2010-2012, Peter Hofmann
10 local markup = require("lain.util.markup")
11 local first_line = require("lain.helpers").first_line
13 local beautiful = require("beautiful")
14 local naughty = require("naughty")
15 local wibox = require("wibox")
17 local math = { floor = math.floor }
18 local string = { format = string.format }
20 local setmetatable = setmetatable
25 status = "not present",
31 local args = args or {}
32 local battery = args.battery or "BAT0"
33 local show_all = args.show_all or false
34 local refresh_timeout = args.refresh_timeout or 30
35 local header = args.header or " Bat "
36 local header_color = args.header_color or beautiful.fg_normal or "#FFFFFF"
37 local color = args.color or beautiful.fg_focus or "#FFFFFF"
38 local shadow = args.shadow or false
40 local mybattery = wibox.widget.textbox()
42 local mybatteryupdate = function()
43 local present = first_line("/sys/class/power_supply/"
49 local rate = first_line("/sys/class/power_supply/"
52 local ratev = first_line("/sys/class/power_supply/"
55 local rem = first_line("/sys/class/power_supply/"
58 local tot = first_line("/sys/class/power_supply/"
61 bat.status = first_line("/sys/class/power_supply/"
66 if bat.status == "Charging"
69 time_rat = (tot - rem) / rate
70 elseif bat.status == "Discharging"
78 local hrs = math.floor(time_rat)
79 local min = (time_rat - hrs) * 60
80 bat.time = string.format("%02d:%02d", hrs, min)
82 local amount = (rem / tot) * 100
86 bat.perc = string.format("%d", amount)
88 bat.perc = string.format("%d%%", amount)
91 local watt = string.format("%.2fW", (rate * ratev) / 1e12)
95 text = watt .. " " .. bat.perc .. " " .. bat.time .. " " .. bat.status
100 -- notifications for low and critical states
104 text = "shutdown imminent",
105 title = "battery nearly exhausted",
106 position = "top_right",
114 old_id = naughty.notify{
115 text = "plug the cable",
116 title = "battery low",
117 position = "top_right",
130 mybattery:set_text('')
132 mybattery:set_markup(markup(header_color, header)
133 .. markup(color, text) .. " ")
137 local mybatterytimer = timer({ timeout = refresh_timeout })
138 mybatterytimer:connect_signal("timeout", mybatteryupdate)
139 mybatterytimer:start()
140 mybatterytimer:emit_signal("timeout")
142 bat.widget = mybattery
144 return setmetatable(bat, { __index = bat.widget })
147 return setmetatable(bat, { __call = function(_, ...) return worker(...) end })