From c870691157ee542abec9a177ef64a8bade31580c Mon Sep 17 00:00:00 2001 From: "Adrian C. (anrxc)" Date: Wed, 18 Nov 2009 00:52:55 +0100 Subject: 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. --- README | 3 ++- fs.lua | 27 +++++++++++++++++++-------- 2 files changed, 21 insertions(+), 9 deletions(-) diff --git a/README b/README index 3af7492..9ea569f 100644 --- a/README +++ b/README @@ -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 diff --git a/fs.lua b/fs.lua index a56a282..704ef93 100644 --- a/fs.lua +++ b/fs.lua @@ -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() -- cgit v1.2.3