helpers: uformat helper replaces formatting done by widgets

This commit is contained in:
Adrian C. (anrxc) 2010-02-20 03:24:42 +01:00
parent f4cd746188
commit e29ea6288a
5 changed files with 38 additions and 52 deletions

2
README
View File

@ -187,7 +187,7 @@ vicious.widgets.dio
- provides I/O statistics for requested storage devices - provides I/O statistics for requested storage devices
- takes the disk as an argument, i.e. "sda" - takes the disk as an argument, i.e. "sda"
- returns a table with string keys: {total_s}, {total_kb}, {total_mb}, - 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 vicious.widgets.hddtemp
- provides hard drive temperatures using the hddtemp daemon - provides hard drive temperatures using the hddtemp daemon

22
dio.lua
View File

@ -7,11 +7,8 @@
local ipairs = ipairs local ipairs = ipairs
local setmetatable = setmetatable local setmetatable = setmetatable
local table = { insert = table.insert } local table = { insert = table.insert }
local string = { gmatch = string.gmatch }
local helpers = require("vicious.helpers") local helpers = require("vicious.helpers")
local string = {
gmatch = string.gmatch,
format = string.format
}
-- }}} -- }}}
@ -22,15 +19,8 @@ module("vicious.dio")
-- Initialise function tables -- Initialise function tables
local disk_usage = {} local disk_usage = {}
local disk_total = {} local disk_total = {}
-- Variable definitions
-- {{{ Helper functions local unit = { ["s"] = 1, ["kb"] = 2, ["mb"] = 2048 }
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
-- }}}
-- {{{ Disk I/O widget type -- {{{ Disk I/O widget type
local function worker(format, disk) local function worker(format, disk)
@ -64,9 +54,9 @@ local function worker(format, disk)
end end
-- Calculate and store I/O -- Calculate and store I/O
uformat(disk_usage[disk], "read", diff_total[disk][3]) helpers.uformat(disk_usage[disk], "read", diff_total[disk][3], unit)
uformat(disk_usage[disk], "write", diff_total[disk][7]) helpers.uformat(disk_usage[disk], "write", diff_total[disk][7], unit)
uformat(disk_usage[disk], "total", diff_total[disk][7] + diff_total[disk][3]) helpers.uformat(disk_usage[disk], "total", diff_total[disk][7] + diff_total[disk][3], unit)
return disk_usage[disk] return disk_usage[disk]
end end

21
fs.lua
View File

@ -8,10 +8,8 @@
local tonumber = tonumber local tonumber = tonumber
local io = { popen = io.popen } local io = { popen = io.popen }
local setmetatable = setmetatable local setmetatable = setmetatable
local string = { local string = { match = string.match }
match = string.match, local helpers = require("vicious.helpers")
format = string.format
}
-- }}} -- }}}
@ -19,13 +17,8 @@ local string = {
module("vicious.fs") module("vicious.fs")
-- {{{ Helper functions -- Variable definitions
local function uformat(array, key, value) local unit = { ["mb"] = 1024, ["gb"] = 1024^2 }
array["{"..key.."_mb}"] = string.format("%.1f", value/1024)
array["{"..key.."_gb}"] = string.format("%.1f", value/1024/1024)
return array
end
-- }}}
-- {{{ Filesystem widget type -- {{{ Filesystem widget type
local function worker(format, nfs) 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) 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]+)$") "^[%w%p]+[%s]+([%d]+)[%s]+([%d]+)[%s]+([%d]+)[%s]+([%d]+)%%[%s]+([%w%p]+)$")
uformat(fs_info, m .. " size", s) helpers.uformat(fs_info, m .. " size", s, unit)
uformat(fs_info, m .. " used", u) helpers.uformat(fs_info, m .. " used", u, unit)
uformat(fs_info, m .. " avail", a) helpers.uformat(fs_info, m .. " avail", a, unit)
fs_info["{" .. m .. " used_p}"] = tonumber(p) fs_info["{" .. m .. " used_p}"] = tonumber(p)
end end
end end

View File

@ -12,7 +12,8 @@ local io = { open = io.open }
local setmetatable = setmetatable local setmetatable = setmetatable
local string = { local string = {
sub = string.sub, sub = string.sub,
gsub = string.gsub gsub = string.gsub,
format = string.format
} }
-- }}} -- }}}
@ -51,6 +52,16 @@ function format(format, args)
end 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 -- {{{ Escape a string
function escape(text) function escape(text)
local xml_entities = { local xml_entities = {

32
net.lua
View File

@ -10,10 +10,8 @@ local tonumber = tonumber
local os = { time = os.time } local os = { time = os.time }
local io = { open = io.open } local io = { open = io.open }
local setmetatable = setmetatable local setmetatable = setmetatable
local string = { local string = { match = string.match }
match = string.match, local helpers = require("vicious.helpers")
format = string.format
}
-- }}} -- }}}
@ -23,16 +21,10 @@ module("vicious.net")
-- Initialise function tables -- Initialise function tables
local nets = {} local nets = {}
-- Variable definitions
-- {{{ Helper functions local unit = { ["b"] = 1, ["kb"] = 1024,
local function uformat(array, key, value) ["mb"] = 1024^2, ["gb"] = 1024^3
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
-- }}}
-- {{{ Net widget type -- {{{ Net widget type
local function worker(format) local function worker(format)
@ -50,14 +42,14 @@ local function worker(format)
local send = tonumber(string.match(line, local send = tonumber(string.match(line,
"([%d]+)%s+%d+%s+%d+%s+%d+%s+%d+%s+%d+%s+%d+%s+%d$")) "([%d]+)%s+%d+%s+%d+%s+%d+%s+%d+%s+%d+%s+%d+%s+%d$"))
uformat(args, name .. " rx", recv) helpers.uformat(args, name .. " rx", recv, unit)
uformat(args, name .. " tx", send) helpers.uformat(args, name .. " tx", send, unit)
if nets[name] == nil then if nets[name] == nil then
-- Default values on the first run -- Default values on the first run
nets[name] = {} nets[name] = {}
uformat(args, name .. " down", 0) helpers.uformat(args, name .. " down", 0, unit)
uformat(args, name .. " up", 0) helpers.uformat(args, name .. " up", 0, unit)
nets[name].time = os.time() nets[name].time = os.time()
else -- Net stats are absolute, substract our last reading 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 down = (recv - nets[name][1]) / interval
local up = (send - nets[name][2]) / interval local up = (send - nets[name][2]) / interval
uformat(args, name .. " down", down) helpers.uformat(args, name .. " down", down, unit)
uformat(args, name .. " up", up) helpers.uformat(args, name .. " up", up, unit)
end end
-- Store totals -- Store totals