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

eb0682852888d85fa0d9632d15f2fef307962444
[etc/awesome.git] / widgets / sysload.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 helpers      = require("lain.helpers")
12
13 local awful        = require("awful")
14 local beautiful    = require("beautiful")
15 local wibox        = require("wibox")
16
17 local io           = io
18 local string       = { format = string.format,
19                        match  = string.match }
20
21 local setmetatable = setmetatable
22
23 -- System load
24 -- lain.widgets.sysload
25 local sysload = {}
26
27 function worker(args)
28     local args = args or {}
29     local refresh_timeout = args.refresh_timeout or 5
30     local show_all = args.show_all or false
31     local header = args.header or " Load "
32     local header_color = args.header_color or beautiful.fg_normal or "#FFFFFF"
33     local color = args.color or beautiful.fg_focus or "#FFFFFF" 
34     local app = args.app or "top"
35
36     local mysysload = wibox.widget.textbox()
37
38     local mysysloadupdate = function()
39         local f = io.open("/proc/loadavg")
40         local ret = f:read("*all")
41         f:close()
42
43         if show_all
44         then
45             local a, b, c = string.match(ret, "([^%s]+) ([^%s]+) ([^%s]+)")
46             mysysload:set_text(string.format("%s %s %s", a, b, c))
47         else
48             local a = string.match(ret, "([^%s]+) ")
49             mysysload:set_text(string.format("%s", a))
50         end
51         mysysload:set_markup(markup(header_color, header) ..
52                              markup(color, mysysload._layout.text .. " "))
53     end
54
55     local mysysloadtimer = timer({ timeout = refresh_timeout })
56     mysysloadtimer:connect_signal("timeout", mysysloadupdate)
57     mysysloadtimer:start()
58     mysysloadtimer:emit_signal("timeout")
59
60     mysysload:buttons(awful.util.table.join(
61         awful.button({}, 0,
62             function()
63                 helpers.run_in_terminal(app)
64             end)
65     ))
66
67     return mysysload
68 end
69
70 return setmetatable(sysload, { __call = function(_, ...) return worker(...) end })