lain/widgets/mem.lua

62 lines
1.9 KiB
Lua
Raw Normal View History

2013-09-07 12:06:42 +02:00
--[[
2013-09-13 18:15:25 +02:00
Licensed under GNU General Public License v2
* (c) 2013, Luke Bonham
* (c) 2010-2012, Peter Hofmann
2013-09-07 12:06:42 +02:00
--]]
2013-09-10 23:02:11 +02:00
local newtimer = require("lain.helpers").newtimer
2013-09-07 12:06:42 +02:00
local wibox = require("wibox")
local io = { lines = io.lines }
local math = { floor = math.floor }
local string = { format = string.format,
gmatch = string.gmatch,
len = string.len }
local setmetatable = setmetatable
-- Memory usage (ignoring caches)
-- lain.widgets.mem
local mem = {}
2013-09-11 19:39:14 +02:00
local function worker(args)
2013-09-10 23:02:11 +02:00
local args = args or {}
local timeout = args.timeout or 3
local settings = args.settings or function() end
2013-09-07 12:06:42 +02:00
2013-09-11 19:39:14 +02:00
mem.widget = wibox.widget.textbox('')
2013-09-07 12:06:42 +02:00
2013-09-13 00:07:44 +02:00
function update()
mem_now = {}
2013-09-07 12:06:42 +02:00
for line in io.lines("/proc/meminfo")
do
for k, v in string.gmatch(line, "([%a]+):[%s]+([%d]+).+")
do
2013-09-13 00:07:44 +02:00
if k == "MemTotal" then mem_now.total = math.floor(v / 1024)
elseif k == "MemFree" then mem_now.free = math.floor(v / 1024)
elseif k == "Buffers" then mem_now.buf = math.floor(v / 1024)
elseif k == "Cached" then mem_now.cache = math.floor(v / 1024)
elseif k == "SwapTotal" then mem_now.swap = math.floor(v / 1024)
elseif k == "SwapFree" then mem_now.swapf = math.floor(v / 1024)
2013-09-07 12:06:42 +02:00
end
end
end
2013-09-13 00:07:44 +02:00
mem_now.used = mem_now.total - (mem_now.free + mem_now.buf + mem_now.cache)
mem_now.swapused = mem_now.swap - mem_now.swapf
2013-09-07 12:06:42 +02:00
2013-09-11 19:39:14 +02:00
widget = mem.widget
2013-09-10 23:02:11 +02:00
settings()
2013-09-07 12:06:42 +02:00
end
2013-09-13 00:07:44 +02:00
newtimer("mem", timeout, update)
2013-09-07 12:06:42 +02:00
2013-09-11 19:39:14 +02:00
return mem.widget
2013-09-07 12:06:42 +02:00
end
return setmetatable(mem, { __call = function(_, ...) return worker(...) end })