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

version 1.0
[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 markup       = require("lain.util.markup")
11 local first_line   = require("lain.helpers").first_line
12
13 local beautiful    = require("beautiful")
14 local naughty      = require("naughty")
15 local wibox        = require("wibox")
16
17 local math         = { floor  = math.floor }
18 local string       = { format = string.format }
19
20 local setmetatable = setmetatable
21
22 -- Battery infos
23 -- lain.widgets.bat
24 local bat = {
25     status = "not present",
26     perc   = "N/A",
27     time   = "N/A",
28 }
29
30 function worker(args)
31     local args = args or {}
32     local battery = args.battery or "BAT0"
33     local show_all = args.show_all or false
34     local refresh_timeout = args.refresh_timeout or 30
35     local header = args.header or " Bat "
36     local header_color = args.header_color or beautiful.fg_normal or "#FFFFFF"
37     local color = args.color or beautiful.fg_focus or "#FFFFFF"
38     local shadow = args.shadow or false
39
40     local mybattery = wibox.widget.textbox()
41
42     local mybatteryupdate = function()
43         local present = first_line("/sys/class/power_supply/"
44                                    .. battery
45                                    .. "/present")
46
47         if present == "1"
48         then
49             local rate = first_line("/sys/class/power_supply/"
50                                     .. battery ..
51                                     "/power_now")
52             local ratev = first_line("/sys/class/power_supply/"
53                                     .. battery ..
54                                      "/voltage_now")
55             local rem = first_line("/sys/class/power_supply/"
56                                     .. battery ..
57                                    "/energy_now")
58             local tot = first_line("/sys/class/power_supply/"
59                                     .. battery ..
60                                    "/energy_full")
61             bat.status = first_line("/sys/class/power_supply/"
62                                     .. battery ..
63                                    "/status")
64
65             local time_rat = 0
66             if bat.status == "Charging"
67             then
68                 status = "(+)"
69                 time_rat = (tot - rem) / rate
70             elseif bat.status == "Discharging"
71             then
72                 status = "(-)"
73                 time_rat = rem / rate
74             else
75                 status = "(.)"
76             end
77
78             local hrs = math.floor(time_rat)
79             local min = (time_rat - hrs) * 60
80             bat.time = string.format("%02d:%02d", hrs, min)
81
82             local amount = (rem / tot) * 100
83
84             if shadow
85             then
86                 bat.perc = string.format("%d", amount)
87             else
88                 bat.perc = string.format("%d%%", amount)
89             end
90
91             local watt = string.format("%.2fW", (rate * ratev) / 1e12)
92
93             if show_all
94             then
95                 text = watt .. " " .. bat.perc .. " " .. bat.time .. " " .. bat.status
96             else
97                 text = bat.perc
98             end
99
100             -- notifications for low and critical states
101             if amount <= 5
102             then
103                 naughty.notify{
104                     text = "shutdown imminent",
105                     title = "battery nearly exhausted",
106                     position = "top_right",
107                     timeout = 15,
108                     fg="#000000",
109                     bg="#ffffff",
110                     ontop = true
111                 }
112             elseif amount <= 15
113             then
114                 old_id = naughty.notify{
115                     text = "plug the cable",
116                     title = "battery low",
117                     position = "top_right",
118                     timeout = 5,
119                     fg="#202020",
120                     bg="#cdcdcd",
121                     ontop = true
122                 }
123             end
124         else
125             text = "none"
126         end
127
128         if shadow
129         then
130             mybattery:set_text('')
131         else
132             mybattery:set_markup(markup(header_color, header)
133                                  .. markup(color, text) .. " ")
134         end
135     end
136
137     local mybatterytimer = timer({ timeout = refresh_timeout })
138     mybatterytimer:connect_signal("timeout", mybatteryupdate)
139     mybatterytimer:start()
140     mybatterytimer:emit_signal("timeout")
141
142     bat.widget = mybattery
143
144     return setmetatable(bat, { __index = bat.widget })
145 end
146
147 return setmetatable(bat, { __call = function(_, ...) return worker(...) end })