fs: switched to 1K blocks and new keys

Previous version could return 1 on one update and 900 on the next (1st
being GB, 2nd MB) a user appending "GB" to the value suddenly has a
900GB disk available. Returned keys are now: size_mb, size_gb,
used_mb, used_gb, avail_mb, avail_gb, and percentage is now: used_p.
This commit is contained in:
Adrian C. (anrxc) 2009-11-18 00:52:55 +01:00
parent 5cbd75996b
commit c870691157
2 changed files with 21 additions and 9 deletions

3
README
View File

@ -177,7 +177,8 @@ vicious.widgets.fs
- takes an (optional) argument which, if true, includes remote file
systems, only local file systems are included by default
- returns a table with custom keys, using mount points as a base:
{/ size}, {/ used}, {/ avail}, {/ usep}, {/home size} etc.
{/ size_mb}, {/ size_gb}, {/ used_mb}, {/ used_gb}, {/ used_p},
{/ avail_mb}, {/ avail_gb}, {/home size_mb} etc.
vicious.widgets.dio
- provides I/O statistics for requested storage devices

27
fs.lua
View File

@ -8,7 +8,10 @@
local tonumber = tonumber
local io = { popen = io.popen }
local setmetatable = setmetatable
local string = { match = string.match }
local string = {
match = string.match,
format = string.format
}
-- }}}
@ -16,24 +19,32 @@ local string = { match = string.match }
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
-- }}}
-- {{{ Filesystem widget type
local function worker(format, nfs)
-- Fallback to listing only local file systems
if nfs then nfs = "" else nfs = "--local" end
-- Get data from df
local f = io.popen("LANG=C df -hP " .. nfs)
local f = io.popen("LANG=C df -kP " .. nfs)
local fs_info = {}
for line in f:lines() do
if not string.match(line, "^Filesystem.*") then
local size, used, avail, usep, mount = string.match(line, -- Match all (network file systems too)
"^[%w%p]+[%s]+([%d%.]+)[%a]?[%s]+([%d%.]+)[%a]?[%s]+([%d%.]+)[%a]?[%s]+([%d]+)%%[%s]+([%w%p]+)$")
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]+)$")
fs_info["{"..mount.." size}"] = tonumber(size)
fs_info["{"..mount.." used}"] = tonumber(used)
fs_info["{"..mount.." avail}"] = tonumber(avail)
fs_info["{"..mount.." usep}"] = tonumber(usep)
uformat(fs_info, m .. " size", s)
uformat(fs_info, m .. " used", u)
uformat(fs_info, m .. " avail", a)
fs_info["{" .. m .. " used_p}"] = tonumber(p)
end
end
f:close()