From f55ff76f543387dece9ff2ccab5737df6b2b242c Mon Sep 17 00:00:00 2001 From: Dave O'Neill Date: Mon, 14 Jul 2008 07:30:45 -0400 Subject: [PATCH] Reimplement the load average plugin in pure Lua. --- doc/bundled-plugins | 6 ++--- doc/configuration | 2 +- plugins/dstat_load.lua | 56 ------------------------------------------- plugins/loadavg.lua | 65 ++++++++++++++++++++++++++++++++++++++++++++++++++ wmiirc.lua | 2 +- 5 files changed, 70 insertions(+), 61 deletions(-) delete mode 100644 plugins/dstat_load.lua create mode 100644 plugins/loadavg.lua diff --git a/doc/bundled-plugins b/doc/bundled-plugins index 2e95f2e..c54cb26 100644 --- a/doc/bundled-plugins +++ b/doc/bundled-plugins @@ -20,9 +20,9 @@ clock location. Clock is driven by a timer which fires once a second by default. Both timer tick and clock format can be set in wmiirc. -dstat_load -=========== - Uses the dstat utility to generate a load ticker. +loadavg +======= + Reads /proc/loadavg to display load average messages ========= diff --git a/doc/configuration b/doc/configuration index f62309a..4f43df5 100644 --- a/doc/configuration +++ b/doc/configuration @@ -128,7 +128,7 @@ To load plugins into wmiirc, simply call the load_plugin function: -- load some plugins wmii.load_plugin ("messages") wmii.load_plugin ("clock") - wmii.load_plugin ("dstat_load") + wmii.load_plugin ("loadavg") wmii.load_plugin ("browser") As mentioned above plugins can have configuration options. To set them diff --git a/plugins/dstat_load.lua b/plugins/dstat_load.lua deleted file mode 100644 index 68cd63b..0000000 --- a/plugins/dstat_load.lua +++ /dev/null @@ -1,56 +0,0 @@ --- --- Copyright (c) 2007, Bart Trojanowski --- --- Simple load applet for wmii bar. --- --- NOTE: dstat is required. --- -local wmii = require("wmii") -local os = require("os") -local math = require("math") -local string = require("string") -local type = type -local tonumber = tonumber -local tostring = tostring - -module("dstat_load") -api_version=0.1 - -local palette = { "#888888", - "#999988", - "#AAAA88", - "#BBBB88", - - "#CCCC88", - "#CCBB88", - "#CCAA88", - - "#DD9988", - "#EE8888", - "#FF4444", - } - -local widget = wmii.widget:new ("800_dstat_load") -wmii.add_exec ("TERM=vt100 dstat --load --nocolor --noheaders --noupdate 1", - function (line) - if type(line) ~= "string" then - return - end - - local line = line:gsub ("%W%W+", " ") - if line:len() < 5 then - return - end - - local tmp = line:match("([%d.]+)%D") - local current = tonumber(tmp) - - local colors = nil - if type(current) == "number" then - local index = math.min (math.floor(current * (#palette-1)) + 1, #palette) - local normal = wmii.get_ctl("normcolors") - colors = string.gsub(normal, "^%S+", palette[index], 1) - end - - widget:show (line, colors) - end) diff --git a/plugins/loadavg.lua b/plugins/loadavg.lua new file mode 100644 index 0000000..1bd5f32 --- /dev/null +++ b/plugins/loadavg.lua @@ -0,0 +1,65 @@ +-- +-- Copyright (c) 2008, Dave O'Neill +-- +-- Simple load applet for wmii bar. +-- +local wmii = require("wmii") +local io = require("io") +local math = require("math") +local string = require("string") +local tonumber = tonumber +local type = type + +module("loadavg") +api_version=0.1 + +local palette = { "#888888", + "#999988", + "#AAAA88", + "#BBBB88", + + "#CCCC88", + "#CCBB88", + "#CCAA88", + + "#DD9988", + "#EE8888", + "#FF4444", + } + +local widget = wmii.widget:new ("800_loadavg") + +local function loadavg_timer (time_since_update) + + local file = io.open("/proc/loadavg", "r") + local bartext = nil + local colors = nil + if file then + local txt = file:read("*all") + file:close() + if type(txt) == 'string' then + local one,five,ten = txt:match("^([%d%.]+)%s+([%d%.]+)%s+([%d%.]+)%s+") + if type(one) == 'string' then + bartext = string.format("%.1f %.1f %.1f", one, five, ten) + end + + -- Now, colorization + local current_avg = tonumber(one) + if type(current_avg) == "number" then + local index = math.min(math.floor(current_avg * (#palette-1)) + 1, #palette) + local normal = wmii.get_ctl("normcolors") + colors = string.gsub(normal, "^%S+", palette[index], 1) + end + end + end + + widget:show(bartext, colors) + -- Returns: + -- positive number of seconds before next wakeup + -- nil, or no return, to repeat the last schedule + -- -1 to stop the timer + return 5 +end + +local timer = wmii.timer:new( loadavg_timer, 1) + diff --git a/wmiirc.lua b/wmiirc.lua index 2178f4d..acc8bec 100755 --- a/wmiirc.lua +++ b/wmiirc.lua @@ -101,7 +101,7 @@ wmii.write ("/tagrules", "/XMMS.*/ -> ~\n" -- load some plugins wmii.load_plugin ("messages") wmii.load_plugin ("clock") -wmii.load_plugin ("dstat_load") +wmii.load_plugin ("loadavg") wmii.load_plugin ("volume") wmii.load_plugin ("browser") -- 2.11.4.GIT