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

Merge pull request #5 from cahna/tp-bat-contrib
[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.perc <= 5
78             then
79                 bat.id = naughty.notify({
80                     text = "shutdown imminent",
81                     title = "battery nearly exhausted",
82                     position = "top_right",
83                     timeout = 15,
84                     fg="#000000",
85                     bg="#ffffff",
86                     ontop = true,
87                     replaces_id = bat.id
88                 }).id
89             elseif bat_now.perc <= 15
90             then
91                 bat.id = naughty.notify({
92                     text = "plug the cable",
93                     title = "battery low",
94                     position = "top_right",
95                     timeout = 15,
96                     fg="#202020",
97                     bg="#cdcdcd",
98                     ontop = true,
99                     replaces_id = bat.id
100                 }).id
101             end
102
103             bat_now.perc = string.format("%d", bat_now.perc)
104         end
105
106         widget = bat.widget
107         settings()
108     end
109
110     newtimer("bat", timeout, update)
111
112     return bat.widget
113 end
114
115 return setmetatable(bat, { __call = function(_, ...) return worker(...) end })