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

Merge branch 'master' of github.com:copycat-killer/lain
[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 helpers      = require("lain.helpers")
11 local wibox        = require("wibox")
12
13 local io           = { open = io.open }
14 local string       = { match  = string.match }
15
16 local setmetatable = setmetatable
17
18 -- System load
19 -- lain.widgets.sysload
20 local sysload = {}
21
22 local function worker(args)
23     local args = args or {}
24     local timeout = args.timeout or 1
25     local settings = args.settings or function() end
26
27     sysload.widget = wibox.widget.textbox('')
28     helpers.set_map("load_1", 0)
29     helpers.set_map("load_5", 0)
30     helpers.set_map("load_15", 0)
31
32     function update()
33         local f = io.open("/proc/loadavg")
34         local ret = f:read("*all")
35         f:close()
36
37         load_1, load_5, load_15 = string.match(ret, "([^%s]+) ([^%s]+) ([^%s]+)")
38
39         if load_1 ~= helpers.get_map("load_1")
40            or load_5 ~= helpers.get_map("load_5")
41            or load_15 ~= helpers.get_map("load_15")
42         then
43             widget = sysload.widget
44             settings()
45
46             helpers.set_map("load_1", load_1)
47             helpers.set_map("load_5", load_5)
48             helpers.set_map("load_15", load_15)
49         end
50     end
51
52     helpers.newtimer("sysload", timeout, update)
53
54     return sysload.widget
55 end
56
57 return setmetatable(sysload, { __call = function(_, ...) return worker(...) end })