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

ba1fa58f0aea99c57444c8c75b810f3ec2fe50aa
[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 bat.update()
34         bat_now = {
35             status = "not present",
36             perc   = "N/A",
37             time   = "N/A",
38             watt   = "N/A"
39         }
40
41         local present = first_line("/sys/class/power_supply/"
42                                    .. battery
43                                    .. "/present")
44
45         if present == "1"
46         then
47             local rate = first_line("/sys/class/power_supply/"
48                                     .. battery ..
49                                     "/power_now")
50             local ratev = first_line("/sys/class/power_supply/"
51                                     .. battery ..
52                                      "/voltage_now")
53             local rem = first_line("/sys/class/power_supply/"
54                                     .. battery ..
55                                    "/energy_now")
56             local tot = first_line("/sys/class/power_supply/"
57                                     .. battery ..
58                                    "/energy_full")
59             bat_now.status = first_line("/sys/class/power_supply/"
60                                     .. battery ..
61                                    "/status")
62
63             local time_rat = 0
64             if bat_now.status == "Charging"
65             then
66                 time_rat = (tot - rem) / rate
67             elseif bat_now.status == "Discharging"
68             then
69                 time_rat = rem / rate
70             end
71
72             local hrs = math.floor(time_rat)
73             local min = (time_rat - hrs) * 60
74
75             bat_now.time = string.format("%02d:%02d", hrs, min)
76             bat_now.perc = (rem / tot) * 100
77             bat_now.watt = string.format("%.2fW", (rate * ratev) / 1e12)
78
79             -- notifications for low and critical states
80             if bat_now.perc <= 5
81             then
82                 bat.id = naughty.notify({
83                     text = "shutdown imminent",
84                     title = "battery nearly exhausted",
85                     position = "top_right",
86                     timeout = 15,
87                     fg="#000000",
88                     bg="#ffffff",
89                     ontop = true,
90                     replaces_id = bat.id
91                 }).id
92             elseif bat.perc <= 15
93             then
94                 bat.id = naughty.notify({
95                     text = "plug the cable",
96                     title = "battery low",
97                     position = "top_right",
98                     timeout = 15,
99                     fg="#202020",
100                     bg="#cdcdcd",
101                     ontop = true,
102                     replaces_id = bat.id
103                 }).id
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, bat.update)
114
115     return bat.widget
116 end
117
118 return setmetatable(bat, { __call = function(_, ...) return worker(...) end })