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