]> git.madduck.net Git - etc/awesome.git/blob - widgets/contrib/tpbat/init.lua

madduck's git repository

Every one of the projects in this repository is available at the canonical URL git://git.madduck.net/madduck/pub/<projectpath> — see each project's metadata for the exact URL.

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.

SSH access, as well as push access can be individually arranged.

If you use my repositories frequently, consider adding the following snippet to ~/.gitconfig and using the third clone URL listed for each project:

[url "git://git.madduck.net/madduck/"]
  insteadOf = madduck:

cal: terminal-colors.d independent highlight matching pattern
[etc/awesome.git] / widgets / contrib / tpbat / init.lua
1
2 --[[
3                                                                
4      tpbat.lua                                                 
5      Battery status widget for ThinkPad laptops that use SMAPI 
6      lain.widgets.contrib.tpbat                                
7                                                                
8      More on tp_smapi: http://www.thinkwiki.org/wiki/Tp_smapi  
9                                                                
10      Licensed under GNU General Public License v2              
11       * (c) 2013,      Conor Heine                             
12       * (c) 2013,      Luke Bonham                             
13       * (c) 2010-2012, Peter Hofmann                           
14                                                                
15 --]]
16
17 local debug        = { getinfo = debug.getinfo }
18 local newtimer     = require("lain.helpers").newtimer
19 local first_line   = require("lain.helpers").first_line
20 local naughty      = require("naughty")
21 local wibox        = require("wibox")
22
23 local string       = { format = string.format }
24 local math         = { floor = math.floor }
25 local tostring     = tostring
26 local setmetatable = setmetatable
27
28 package.path       = debug.getinfo(1,"S").source:match[[^@?(.*[\/])[^\/]-$]] .. "?.lua;" .. package.path
29 local smapi        = require("smapi")
30
31 -- ThinkPad SMAPI-enabled battery info widget
32 -- lain.widgets.contrib.tpbat
33 local tpbat = { }
34 local tpbat_notification = nil
35
36 function tpbat.hide()
37     if not tpbat.notification then return end
38     naughty.destroy(tpbat.notification)
39     tpbat.notification = nil
40 end
41
42 function tpbat.show(t_out)
43     tpbat.hide()
44
45     local bat   = tpbat.bat
46
47     if bat == nil or not bat:installed() then return end
48
49     local t_out = t_out or 0
50     local mfgr   = bat:get('manufacturer') or "no_mfgr"
51     local model  = bat:get('model') or "no_model"
52     local chem   = bat:get('chemistry') or "no_chem"
53     local status = bat:get('state') or "nil"
54     local time   = bat:remaining_time()
55     local msg    = "\t"
56
57     if status ~= "idle" and status ~= "nil"
58     then
59         if time == "N/A"
60         then
61             msg = "...Calculating time remaining..."
62         else
63             msg = time .. (status == "charging" and " until charged" or " remaining")
64         end
65     else
66         msg = "On AC Power"
67     end
68
69     local str = string.format("%s : %s %s (%s)\n", bat.name, mfgr, model, chem)
70                 .. string.format("\n%s \t\t\t %s", status:upper(), msg)
71
72     tpbat_notification = naughty.notify({
73         preset = naughty.config.defaults,
74         text = str,
75         timeout = t_out,
76         screen = client.focus and client.focus.screen or 1
77     })
78 end
79
80 function tpbat.register(args)
81     local args = args or {}
82     local timeout = args.timeout or 30
83     local battery = args.battery or "BAT0"
84     local settings = args.settings or function() end
85
86     tpbat.bat = smapi:battery(battery) -- Create a new battery
87     local bat = tpbat.bat
88
89     tpbat.widget = wibox.widget.textbox('')
90
91     bat_notification_low_preset = {
92         title = "Battery low",
93         text = "Plug the cable!",
94         timeout = 15,
95         fg = "#202020",
96         bg = "#CDCDCD"
97     }
98
99     bat_notification_critical_preset = {
100         title = "Battery exhausted",
101         text = "Shutdown imminent",
102         timeout = 15,
103         fg = "#000000",
104         bg = "#FFFFFF"
105     }
106
107     if bat:get('state') == nil
108     then
109         local n = naughty.notify({
110             preset = bat_notification_low_preset,
111             title = "SMAPI Battery Warning: Unable to read battery state!",
112             text = "This widget is intended for ThinkPads. Is tp_smapi installed? Check your configs & paths.",
113             screen = client.focus and client.focus.screen or 1
114         })
115     end
116
117     function update()
118         bat_now = {
119             status = "Not present",
120             perc   = "N/A",
121             time   = "N/A",
122             watt   = "N/A"
123         }
124
125         if bat:installed()
126         then
127             bat_now.status = bat:status() or "N/A"
128             bat_now.perc   = bat:percent()
129             bat_now.time   = bat:remaining_time()
130             -- bat_now.watt = string.format("%.2fW", (VOLTS * AMPS) / 1e12)
131
132             -- notifications for low and critical states (when discharging)
133             if bat_now.status == "discharging"
134             then
135                 if bat_now.perc <= 5
136                 then
137                     tpbat.id = naughty.notify({
138                         preset = bat_notification_critical_preset,
139                         replaces_id = tpbat.id,
140                         screen = client.focus and client.focus.screen or 1
141                     }).id
142                 elseif bat_now.perc <= 15
143                 then
144                     tpbat.id = naughty.notify({
145                         preset = bat_notification_low_preset,
146                         replaces_id = tpbat.id,
147                         screen = client.focus and client.focus.screen or 1
148                     }).id
149                 end
150             end
151
152             bat_now.perc = tostring(bat_now.perc)
153         end
154
155         widget = tpbat.widget
156         settings()
157     end
158
159     newtimer("tpbat-" .. bat.name, timeout, update)
160
161     widget:connect_signal('mouse::enter', function () tpbat.show() end)
162     widget:connect_signal('mouse::leave', function () tpbat.hide() end)
163
164     return tpbat.widget
165 end
166
167 return setmetatable(tpbat, { __call = function(_, ...) return tpbat.register(...) end })