]> 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:

issue #11 typos fixed
[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 beautiful    = require("beautiful")
21 local naughty      = require("naughty")
22 local wibox        = require("wibox")
23
24 local string       = { format = string.format }
25 local math         = { floor = math.floor }
26 local tostring     = tostring
27 local setmetatable = setmetatable
28
29 package.path       = debug.getinfo(1,"S").source:match[[^@?(.*[\/])[^\/]-$]] .. "?.lua;" .. package.path
30 local smapi        = require("smapi")
31
32 -- ThinkPad SMAPI-enabled battery info widget
33 -- lain.widgets.contrib.tpbat
34 local tpbat = { }
35 local tpbat_notification = nil
36
37 function tpbat:hide()
38     if tpbat_notification ~= nil
39     then
40         naughty.destroy(tpbat_notification)
41         tpbat_notification = nil
42     end
43 end
44
45 function tpbat:show(t_out)
46     tpbat:hide()
47
48     local bat   = self.bat
49     local t_out = t_out or 0
50
51     if bat == nil or not bat:installed() then return end
52
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()
58     local msg    = "\t"
59
60     if status ~= "idle" and status ~= "nil"
61     then
62         if time == "N/A"
63         then
64             msg = "...Calculating time remaining..."
65         else
66             msg = time .. (status == "charging" and " until charged" or " remaining")
67         end
68     else
69         msg = "On AC Power"
70     end
71
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)
74
75     tpbat_notification = naughty.notify({
76         preset = { fg = beautiful.fg_normal },
77         text = str,
78         timeout = t_out
79     })
80 end
81
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
87
88     tpbat.bat = smapi:battery(battery) -- Create a new battery
89     local bat = tpbat.bat
90
91     tpbat.widget = wibox.widget.textbox('')
92
93     if bat:get('state') == nil
94     then
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",
99             timeout = 15,
100             fg="#202020",
101             bg="#cdcdcd",
102             ontop = true
103         })
104     end
105
106     function update()
107         bat_now = {
108             status = "Not present",
109             perc   = "N/A",
110             time   = "N/A",
111             watt   = "N/A"
112         }
113
114         if bat:installed()
115         then
116             bat_now.status = bat:status() or "N/A"
117             bat_now.perc   = bat:percent()
118             bat_now.time   = bat:remaining_time()
119             -- bat_now.watt = string.format("%.2fW", (VOLTS * AMPS) / 1e12)
120
121             -- notifications for low and critical states (when discharging)
122             if bat_now.status == "discharging"
123             then
124                 if bat_now.perc <= 5
125                 then
126                     tpbat.id = naughty.notify({
127                         text = "shutdown imminent",
128                         title = "battery nearly exhausted",
129                         position = "top_right",
130                         timeout = 15,
131                         fg="#000000",
132                         bg="#ffffff",
133                         ontop = true,
134                         replaces_id = tpbat.id
135                     }).id
136                 elseif bat_now.perc <= 15
137                 then
138                     tpbat.id = naughty.notify({
139                         text = "plug the cable",
140                         title = "battery low",
141                         position = "top_right",
142                         timeout = 15,
143                         fg="#202020",
144                         bg="#cdcdcd",
145                         ontop = true,
146                         replaces_id = tpbat.id
147                     }).id
148                 end
149             end
150
151             bat_now.perc = tostring(bat_now.perc)
152         end
153
154         widget = tpbat.widget
155         settings()
156     end
157
158     newtimer("tpbat", timeout, update)
159
160     widget:connect_signal('mouse::enter', function () tpbat:show() end)
161     widget:connect_signal('mouse::leave', function () tpbat:hide() end)
162
163     return tpbat.widget
164 end
165
166 return setmetatable(tpbat, { __call = function(_, ...) return tpbat.register(...) end })