lain/widgets/mem.lua

57 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
--]]
2017-01-25 17:13:14 +01:00
local helpers = require("lain.helpers")
local wibox = require("wibox")
local gmatch = string.gmatch
local lines = io.lines
2017-01-24 16:38:28 +01:00
local floor = math.floor
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
2017-01-26 20:53:55 +01:00
local mem = {}
2013-09-07 12:06:42 +02:00
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-26 20:53:55 +01:00
mem.widget = wibox.widget.textbox()
2017-01-27 14:35:27 +01:00
function mem.update()
2013-09-13 00:07:44 +02:00
mem_now = {}
for line in lines("/proc/meminfo") do
for k, v in gmatch(line, "([%a]+):[%s]+([%d]+).+") do
2017-01-24 16:38:28 +01:00
if k == "MemTotal" then mem_now.total = floor(v / 1024 + 0.5)
elseif k == "MemFree" then mem_now.free = floor(v / 1024 + 0.5)
elseif k == "Buffers" then mem_now.buf = floor(v / 1024 + 0.5)
elseif k == "Cached" then mem_now.cache = floor(v / 1024 + 0.5)
elseif k == "SwapTotal" then mem_now.swap = floor(v / 1024 + 0.5)
elseif k == "SwapFree" then mem_now.swapf = floor(v / 1024 + 0.5)
elseif k == "SReclaimable" then mem_now.srec = floor(v / 1024 + 0.5)
2013-09-07 12:06:42 +02:00
end
end
end
mem_now.used = mem_now.total - mem_now.free - mem_now.buf - mem_now.cache - mem_now.srec
2015-10-20 15:17:44 +02:00
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
2017-01-27 14:35:27 +01:00
helpers.newtimer("mem", timeout, mem.update)
2013-09-07 12:06:42 +02:00
2017-01-25 17:13:14 +01:00
return mem
2013-09-07 12:06:42 +02:00
end
return setmetatable(mem, { __call = function(_, ...) return worker(...) end })