lain/widgets/mem.lua

56 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
--]]
local newtimer = require("lain.helpers").newtimer
local wibox = require("wibox")
local io = { lines = io.lines }
local math = { floor = math.floor }
local string = { gmatch = string.gmatch }
local setmetatable = setmetatable
2015-10-20 15:17:44 +02:00
-- Memory usage (ignoring caches)
2013-09-07 12:06:42 +02:00
-- 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 {}
2015-10-20 15:17:44 +02:00
local timeout = args.timeout or 2
2013-09-10 23:02:11 +02:00
local settings = args.settings or function() end
2013-09-07 12:06:42 +02:00
2017-01-22 01:15:57 +01: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 = {}
2017-01-22 01:15:57 +01: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
2015-10-20 15:17: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
2016-07-27 11:40:41 +02:00
mem_now.perc = math.floor(mem_now.used / mem_now.total * 100)
2013-09-07 12:06:42 +02:00
2015-10-20 15:17:44 +02:00
widget = mem.widget
settings()
2013-09-07 12:06:42 +02:00
end
2015-10-20 15:17: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 })