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.
5 Battery status widget for ThinkPad laptops that use SMAPI
6 lain.widgets.contrib.tpbat
8 More on tp_smapi: http://www.thinkwiki.org/wiki/Tp_smapi
10 Licensed under GNU General Public License v2
11 * (c) 2013, Conor Heine
12 * (c) 2013, Luke Bonham
13 * (c) 2010-2012, Peter Hofmann
17 local debug = { getinfo = debug.getinfo }
18 local newtimer = require("lain.helpers").newtimer
19 local first_line = require("lain.helpers").first_line
20 local beautiful = require("beautiful")
21 local naughty = require("naughty")
22 local wibox = require("wibox")
24 local string = { format = string.format }
25 local math = { floor = math.floor }
26 local tostring = tostring
27 local setmetatable = setmetatable
29 package.path = debug.getinfo(1,"S").source:match[[^@?(.*[\/])[^\/]-$]] .. "?.lua;" .. package.path
30 local smapi = require("smapi")
32 -- ThinkPad SMAPI-enabled battery info widget
33 -- lain.widgets.contrib.tpbat
35 local tpbat_notification = nil
38 if tpbat_notification ~= nil
40 naughty.destroy(tpbat_notification)
41 tpbat_notification = nil
45 function tpbat:show(t_out)
49 local t_out = t_out or 0
51 if bat == nil or not bat:installed() then return end
53 local mfgr = bat:get('manufacturer') or "no_mfgr"
54 local model = bat:get('model') or "no_model"
55 local chem = bat:get('chemistry') or "no_chem"
56 local status = bat:get('state') or "nil"
57 local time = bat:remaining_time()
60 if status ~= "idle" and status ~= "nil"
64 msg = "...Calculating time remaining..."
66 msg = time .. (status == "charging" and " until charged" or " remaining")
72 local str = string.format("%s : %s %s (%s)\n", bat.name, mfgr, model, chem)
73 .. string.format("\n%s \t\t\t %s", status:upper(), msg)
75 tpbat_notification = naughty.notify({
76 preset = { fg = beautiful.fg_normal },
82 function tpbat.register(args)
83 local args = args or {}
84 local timeout = args.timeout or 30
85 local battery = args.battery or "BAT0"
86 local settings = args.settings or function() end
88 tpbat.bat = smapi:battery(battery) -- Create a new battery
91 tpbat.widget = wibox.widget.textbox('')
93 if bat:get('state') == nil
95 local n = naughty.notify({
96 title = "SMAPI Battery Warning: Unable to read battery state!",
97 text = "This widget is intended for ThinkPads. Is tp_smapi installed? Check your configs & paths.",
98 position = "top_right",
108 status = "Not present",
116 bat_now.status = bat:status()
117 bat_now.perc = bat:percent()
118 bat_now.time = bat:remaining_time()
119 -- bat_now.watt = string.format("%.2fW", (VOLTS * AMPS) / 1e12)
121 -- notifications for low and critical states
124 tpbat.id = naughty.notify({
125 text = "shutdown imminent",
126 title = "battery nearly exhausted",
127 position = "top_right",
132 replaces_id = tpbat.id
134 elseif bat_now.perc <= 15
136 tpbat.id = naughty.notify({
137 text = "plug the cable",
138 title = "battery low",
139 position = "top_right",
144 replaces_id = tpbat.id
148 bat_now.perc = tostring(bat_now.perc)
151 widget = tpbat.widget
155 newtimer("tpbat", timeout, update)
157 widget:connect_signal('mouse::enter', function () tpbat:show() end)
158 widget:connect_signal('mouse::leave', function () tpbat:hide() end)
163 return setmetatable(tpbat, { __call = function(_, ...) return tpbat.register(...) end })