Reimplement the load average plugin in pure Lua.
[wmiirc-lua.git] / plugins / loadavg.lua
blob1bd5f323d95e602706d3f7272d3b2e18e6b90175
1 --
2 -- Copyright (c) 2008, Dave O'Neill <dmo@dmo.ca>
3 --
4 -- Simple load applet for wmii bar.
5 --
6 local wmii = require("wmii")
7 local io = require("io")
8 local math = require("math")
9 local string = require("string")
10 local tonumber = tonumber
11 local type = type
13 module("loadavg")
14 api_version=0.1
16 local palette = { "#888888",
17 "#999988",
18 "#AAAA88",
19 "#BBBB88",
21 "#CCCC88",
22 "#CCBB88",
23 "#CCAA88",
25 "#DD9988",
26 "#EE8888",
27 "#FF4444",
30 local widget = wmii.widget:new ("800_loadavg")
32 local function loadavg_timer (time_since_update)
34 local file = io.open("/proc/loadavg", "r")
35 local bartext = nil
36 local colors = nil
37 if file then
38 local txt = file:read("*all")
39 file:close()
40 if type(txt) == 'string' then
41 local one,five,ten = txt:match("^([%d%.]+)%s+([%d%.]+)%s+([%d%.]+)%s+")
42 if type(one) == 'string' then
43 bartext = string.format("%.1f %.1f %.1f", one, five, ten)
44 end
46 -- Now, colorization
47 local current_avg = tonumber(one)
48 if type(current_avg) == "number" then
49 local index = math.min(math.floor(current_avg * (#palette-1)) + 1, #palette)
50 local normal = wmii.get_ctl("normcolors")
51 colors = string.gsub(normal, "^%S+", palette[index], 1)
52 end
53 end
54 end
56 widget:show(bartext, colors)
57 -- Returns:
58 -- positive number of seconds before next wakeup
59 -- nil, or no return, to repeat the last schedule
60 -- -1 to stop the timer
61 return 5
62 end
64 local timer = wmii.timer:new( loadavg_timer, 1)