aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorAdrian C. (anrxc) <anrxc@sysphere.org>2010-03-11 02:57:07 +0100
committerAdrian C. (anrxc) <anrxc@sysphere.org>2010-03-11 02:57:07 +0100
commit3178068b962bca4f97d5e69a96375b865feb8d01 (patch)
tree588f8a0a1932824d5bb0a8db318179343b708d66
parent77d1a0ba2ff7e75d6feb4ec1c9c4efd2a95bd133 (diff)
downloadvicious-legacy-3178068b962bca4f97d5e69a96375b865feb8d01.tar.xz
mem: rewrite and simplify
-rw-r--r--mem.lua33
1 files changed, 14 insertions, 19 deletions
diff --git a/mem.lua b/mem.lua
index f3352b6..f331ae4 100644
--- a/mem.lua
+++ b/mem.lua
@@ -8,7 +8,7 @@
local io = { open = io.open }
local setmetatable = setmetatable
local math = { floor = math.floor }
-local string = { match = string.match }
+local string = { gmatch = string.gmatch }
-- }}}
@@ -23,33 +23,28 @@ local function worker(format)
local mem = { buf = {}, swp = {} }
for line in f:lines() do
- if string.match(line, "^MemTotal.*") then
- mem.total = math.floor(string.match(line, "([%d]+)")/1024)
- elseif string.match(line, "^MemFree.*") then
- mem.buf.f = math.floor(string.match(line, "([%d]+)")/1024)
- elseif string.match(line, "^Buffers.*") then
- mem.buf.b = math.floor(string.match(line, "([%d]+)")/1024)
- elseif string.match(line, "^Cached.*") then
- mem.buf.c = math.floor(string.match(line, "([%d]+)")/1024)
- -- Get swap stats while we are at it
- elseif string.match(line, "^SwapTotal.*") then
- mem.swp.total = math.floor(string.match(line, "([%d]+)")/1024)
- elseif string.match(line, "^SwapFree.*") then
- mem.swp.free = math.floor(string.match(line, "([%d]+)")/1024)
+ for k, v in string.gmatch(line, "([%a]+):[%s]+([%d]+).+") do
+ if k == "MemTotal" then mem.total = math.floor(v/1024)
+ elseif k == "MemFree" then mem.buf.f = math.floor(v/1024)
+ elseif k == "Buffers" then mem.buf.b = math.floor(v/1024)
+ elseif k == "Cached" then mem.buf.c = math.floor(v/1024)
+ elseif k == "SwapTotal" then mem.swp.t = math.floor(v/1024)
+ elseif k == "SwapFree" then mem.swp.f = math.floor(v/1024)
+ end
end
end
f:close()
- -- Calculate percentage
+ -- Calculate memory percentage
mem.free = mem.buf.f + mem.buf.b + mem.buf.c
mem.inuse = mem.total - mem.free
mem.usep = math.floor(mem.inuse / mem.total * 100)
-- Calculate swap percentage
- mem.swp.inuse = mem.swp.total - mem.swp.free
- mem.swp.usep = math.floor(mem.swp.inuse / mem.swp.total * 100)
+ mem.swp.inuse = mem.swp.t - mem.swp.f
+ mem.swp.usep = math.floor(mem.swp.inuse / mem.swp.t * 100)
- return {mem.usep, mem.inuse, mem.total, mem.free,
- mem.swp.usep, mem.swp.inuse, mem.swp.total, mem.swp.free}
+ return {mem.usep, mem.inuse, mem.total, mem.free,
+ mem.swp.usep, mem.swp.inuse, mem.swp.t, mem.swp.f}
end
-- }}}