From e29ea6288a04100850efad7f1aa0cd747b82dc78 Mon Sep 17 00:00:00 2001 From: "Adrian C. (anrxc)" Date: Sat, 20 Feb 2010 03:24:42 +0100 Subject: [PATCH] helpers: uformat helper replaces formatting done by widgets --- README | 2 +- dio.lua | 22 ++++++---------------- fs.lua | 21 +++++++-------------- helpers.lua | 13 ++++++++++++- net.lua | 32 ++++++++++++-------------------- 5 files changed, 38 insertions(+), 52 deletions(-) diff --git a/README b/README index 82b4587..1a0ef97 100644 --- a/README +++ b/README @@ -187,7 +187,7 @@ vicious.widgets.dio - provides I/O statistics for requested storage devices - takes the disk as an argument, i.e. "sda" - returns a table with string keys: {total_s}, {total_kb}, {total_mb}, - {read_s}, {read_kb}, {read_mb}, {write_s},{write_kb} and {write_mb} + {read_s}, {read_kb}, {read_mb}, {write_s}, {write_kb} and {write_mb} vicious.widgets.hddtemp - provides hard drive temperatures using the hddtemp daemon diff --git a/dio.lua b/dio.lua index 57b786d..c7f6dce 100644 --- a/dio.lua +++ b/dio.lua @@ -7,11 +7,8 @@ local ipairs = ipairs local setmetatable = setmetatable local table = { insert = table.insert } +local string = { gmatch = string.gmatch } local helpers = require("vicious.helpers") -local string = { - gmatch = string.gmatch, - format = string.format -} -- }}} @@ -22,15 +19,8 @@ module("vicious.dio") -- Initialise function tables local disk_usage = {} local disk_total = {} - --- {{{ Helper functions -local function uformat(array, key, value) - array["{"..key.."_s}"] = string.format("%.1f", value) - array["{"..key.."_kb}"] = string.format("%.1f", value/2) - array["{"..key.."_mb}"] = string.format("%.1f", value/2/1024) - return array -end --- }}} +-- Variable definitions +local unit = { ["s"] = 1, ["kb"] = 2, ["mb"] = 2048 } -- {{{ Disk I/O widget type local function worker(format, disk) @@ -64,9 +54,9 @@ local function worker(format, disk) end -- Calculate and store I/O - uformat(disk_usage[disk], "read", diff_total[disk][3]) - uformat(disk_usage[disk], "write", diff_total[disk][7]) - uformat(disk_usage[disk], "total", diff_total[disk][7] + diff_total[disk][3]) + helpers.uformat(disk_usage[disk], "read", diff_total[disk][3], unit) + helpers.uformat(disk_usage[disk], "write", diff_total[disk][7], unit) + helpers.uformat(disk_usage[disk], "total", diff_total[disk][7] + diff_total[disk][3], unit) return disk_usage[disk] end diff --git a/fs.lua b/fs.lua index 4905314..276ca92 100644 --- a/fs.lua +++ b/fs.lua @@ -8,10 +8,8 @@ local tonumber = tonumber local io = { popen = io.popen } local setmetatable = setmetatable -local string = { - match = string.match, - format = string.format -} +local string = { match = string.match } +local helpers = require("vicious.helpers") -- }}} @@ -19,13 +17,8 @@ local string = { module("vicious.fs") --- {{{ Helper functions -local function uformat(array, key, value) - array["{"..key.."_mb}"] = string.format("%.1f", value/1024) - array["{"..key.."_gb}"] = string.format("%.1f", value/1024/1024) - return array -end --- }}} +-- Variable definitions +local unit = { ["mb"] = 1024, ["gb"] = 1024^2 } -- {{{ Filesystem widget type local function worker(format, nfs) @@ -41,9 +34,9 @@ local function worker(format, nfs) local s, u, a, p, m = string.match(line, -- Match all at once (including NFS) "^[%w%p]+[%s]+([%d]+)[%s]+([%d]+)[%s]+([%d]+)[%s]+([%d]+)%%[%s]+([%w%p]+)$") - uformat(fs_info, m .. " size", s) - uformat(fs_info, m .. " used", u) - uformat(fs_info, m .. " avail", a) + helpers.uformat(fs_info, m .. " size", s, unit) + helpers.uformat(fs_info, m .. " used", u, unit) + helpers.uformat(fs_info, m .. " avail", a, unit) fs_info["{" .. m .. " used_p}"] = tonumber(p) end end diff --git a/helpers.lua b/helpers.lua index c731ae8..e8d6d6c 100644 --- a/helpers.lua +++ b/helpers.lua @@ -12,7 +12,8 @@ local io = { open = io.open } local setmetatable = setmetatable local string = { sub = string.sub, - gsub = string.gsub + gsub = string.gsub, + format = string.format } -- }}} @@ -51,6 +52,16 @@ function format(format, args) end -- }}} +-- {{{ Format units to one decimal point +function uformat(array, key, value, unit) + for u, v in pairs(unit) do + array["{"..key.."_"..u.."}"] = string.format("%.1f", value/v) + end + + return array +end +-- }}} + -- {{{ Escape a string function escape(text) local xml_entities = { diff --git a/net.lua b/net.lua index 1fec49d..bd46664 100644 --- a/net.lua +++ b/net.lua @@ -10,10 +10,8 @@ local tonumber = tonumber local os = { time = os.time } local io = { open = io.open } local setmetatable = setmetatable -local string = { - match = string.match, - format = string.format -} +local string = { match = string.match } +local helpers = require("vicious.helpers") -- }}} @@ -23,16 +21,10 @@ module("vicious.net") -- Initialise function tables local nets = {} - --- {{{ Helper functions -local function uformat(array, key, value) - array["{"..key.."_b}"] = string.format("%.1f", value) - array["{"..key.."_kb}"] = string.format("%.1f", value/1024) - array["{"..key.."_mb}"] = string.format("%.1f", value/1024/1024) - array["{"..key.."_gb}"] = string.format("%.1f", value/1024/1024/1024) - return array -end --- }}} +-- Variable definitions +local unit = { ["b"] = 1, ["kb"] = 1024, + ["mb"] = 1024^2, ["gb"] = 1024^3 +} -- {{{ Net widget type local function worker(format) @@ -50,14 +42,14 @@ local function worker(format) local send = tonumber(string.match(line, "([%d]+)%s+%d+%s+%d+%s+%d+%s+%d+%s+%d+%s+%d+%s+%d$")) - uformat(args, name .. " rx", recv) - uformat(args, name .. " tx", send) + helpers.uformat(args, name .. " rx", recv, unit) + helpers.uformat(args, name .. " tx", send, unit) if nets[name] == nil then -- Default values on the first run nets[name] = {} - uformat(args, name .. " down", 0) - uformat(args, name .. " up", 0) + helpers.uformat(args, name .. " down", 0, unit) + helpers.uformat(args, name .. " up", 0, unit) nets[name].time = os.time() else -- Net stats are absolute, substract our last reading @@ -68,8 +60,8 @@ local function worker(format) local down = (recv - nets[name][1]) / interval local up = (send - nets[name][2]) / interval - uformat(args, name .. " down", down) - uformat(args, name .. " up", up) + helpers.uformat(args, name .. " down", down, unit) + helpers.uformat(args, name .. " up", up, unit) end -- Store totals