mem.lua 1.7 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152
  1. --[[
  2. Licensed under GNU General Public License v2
  3. * (c) 2013, Luca CPZ
  4. * (c) 2010-2012, Peter Hofmann
  5. --]]
  6. local helpers = require("lain.helpers")
  7. local wibox = require("wibox")
  8. local gmatch, lines, floor = string.gmatch, io.lines, math.floor
  9. -- Memory usage (ignoring caches)
  10. -- lain.widget.mem
  11. local function factory(args)
  12. args = args or {}
  13. local mem = { widget = args.widget or wibox.widget.textbox() }
  14. local timeout = args.timeout or 2
  15. local settings = args.settings or function() end
  16. function mem.update()
  17. mem_now = {}
  18. for line in lines("/proc/meminfo") do
  19. for k, v in gmatch(line, "([%a]+):[%s]+([%d]+).+") do
  20. if k == "MemTotal" then mem_now.total = floor(v / 1024 + 0.5)
  21. elseif k == "MemFree" then mem_now.free = floor(v / 1024 + 0.5)
  22. elseif k == "Buffers" then mem_now.buf = floor(v / 1024 + 0.5)
  23. elseif k == "Cached" then mem_now.cache = floor(v / 1024 + 0.5)
  24. elseif k == "SwapTotal" then mem_now.swap = floor(v / 1024 + 0.5)
  25. elseif k == "SwapFree" then mem_now.swapf = floor(v / 1024 + 0.5)
  26. elseif k == "SReclaimable" then mem_now.srec = floor(v / 1024 + 0.5)
  27. end
  28. end
  29. end
  30. mem_now.used = mem_now.total - mem_now.free - mem_now.buf - mem_now.cache - mem_now.srec
  31. mem_now.swapused = mem_now.swap - mem_now.swapf
  32. mem_now.perc = math.floor(mem_now.used / mem_now.total * 100)
  33. widget = mem.widget
  34. settings()
  35. end
  36. helpers.newtimer("mem", timeout, mem.update)
  37. return mem
  38. end
  39. return factory