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

22
dio.lua
View File

@ -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

21
fs.lua
View File

@ -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

View File

@ -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 = {

32
net.lua
View File

@ -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