]> git.madduck.net Git - etc/awesome.git/blob - widgets/bat.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:

dynamic tagging: move_tag added
[etc/awesome.git] / widgets / bat.lua
1
2 --[[
3                                                   
4      Licensed under GNU General Public License v2 
5       * (c) 2013,      Luke Bonham                
6       * (c) 2010-2012, Peter Hofmann              
7                                                   
8 --]]
9
10 local newtimer     = require("lain.helpers").newtimer
11 local first_line   = require("lain.helpers").first_line
12
13 local naughty      = require("naughty")
14 local wibox        = require("wibox")
15
16 local math         = { floor  = math.floor }
17 local string       = { format = string.format }
18
19 local setmetatable = setmetatable
20
21 -- Battery infos
22 -- lain.widgets.bat
23 local bat = {}
24
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
30
31     bat.widget = wibox.widget.textbox('')
32
33     function update()
34         bat_now = {
35             status = "Not present",
36             perc   = "N/A",
37             time   = "N/A",
38             watt   = "N/A"
39         }
40
41         local bstr  = "/sys/class/power_supply/" .. battery
42
43         local present = first_line(bstr .. "/present")
44
45         if present == "1"
46         then
47             local rate  = first_line(bstr .. "/power_now") or
48                           first_line(bstr .. "/current_now")
49
50             local ratev = first_line(bstr .. "/voltage_now")
51
52             local rem   = first_line(bstr .. "/energy_now") or
53                           first_line(bstr .. "/charge_now")
54
55             local tot   = first_line(bstr .. "/energy_full") or
56                           first_line(bstr .. "/charge_full")
57
58             bat_now.status = first_line(bstr .. "/status") or "N/A"
59
60             local time_rat = 0
61             if bat_now.status == "Charging"
62             then
63                 time_rat = (tot - rem) / rate
64             elseif bat_now.status == "Discharging"
65             then
66                 time_rat = rem / rate
67             end
68
69             local hrs = math.floor(time_rat)
70             local min = (time_rat - hrs) * 60
71
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)
75
76             -- notifications for low and critical states
77             if bat_now.status == "Discharging"
78             then
79                 if bat_now.perc <= 5
80                 then
81                     bat.id = naughty.notify({
82                         text = "shutdown imminent",
83                         title = "battery nearly exhausted",
84                         position = "top_right",
85                         timeout = 15,
86                         fg="#000000",
87                         bg="#ffffff",
88                         ontop = true,
89                         replaces_id = bat.id
90                     }).id
91                 elseif bat_now.perc <= 15
92                 then
93                     bat.id = naughty.notify({
94                         text = "plug the cable",
95                         title = "battery low",
96                         position = "top_right",
97                         timeout = 15,
98                         fg="#202020",
99                         bg="#cdcdcd",
100                         ontop = true,
101                         replaces_id = bat.id
102                     }).id
103                 end
104             end
105
106             bat_now.perc = string.format("%d", bat_now.perc)
107         end
108
109         widget = bat.widget
110         settings()
111     end
112
113     newtimer("bat", timeout, update)
114
115     return bat.widget
116 end
117
118 return setmetatable(bat, { __call = function(_, ...) return worker(...) end })