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 newtimer = require("lain.helpers").newtimer
11 local first_line = require("lain.helpers").first_line
13 local naughty = require("naughty")
14 local wibox = require("wibox")
16 local math = { floor = math.floor }
17 local string = { format = string.format }
19 local setmetatable = setmetatable
25 local function worker(args)
26 local args = args or {}
27 local timeout = args.timeout or 30
28 local battery = args.battery or "BAT0"
29 local settings = args.settings or function() end
31 bat.widget = wibox.widget.textbox('')
35 status = "Not present",
41 local bstr = "/sys/class/power_supply/" .. battery
43 local present = first_line(bstr .. "/present")
47 local rate = first_line(bstr .. "/power_now") or
48 first_line(bstr .. "/current_now")
50 local ratev = first_line(bstr .. "/voltage_now")
52 local rem = first_line(bstr .. "/energy_now") or
53 first_line(bstr .. "/charge_now")
55 local tot = first_line(bstr .. "/energy_full") or
56 first_line(bstr .. "/charge_full")
58 bat_now.status = first_line(bstr .. "/status") or "N/A"
61 if bat_now.status == "Charging"
63 time_rat = (tot - rem) / rate
64 elseif bat_now.status == "Discharging"
69 local hrs = math.floor(time_rat)
70 local min = (time_rat - hrs) * 60
72 bat_now.time = string.format("%02d:%02d", hrs, min)
73 bat_now.perc = (rem / tot) * 100
74 bat_now.watt = string.format("%.2fW", (rate * ratev) / 1e12)
76 -- notifications for low and critical states
79 bat.id = naughty.notify({
80 text = "shutdown imminent",
81 title = "battery nearly exhausted",
82 position = "top_right",
89 elseif bat_now.perc <= 15
91 bat.id = naughty.notify({
92 text = "plug the cable",
93 title = "battery low",
94 position = "top_right",
103 bat_now.perc = string.format("%d", bat_now.perc)
110 newtimer("bat", timeout, update)
115 return setmetatable(bat, { __call = function(_, ...) return worker(...) end })