]> git.madduck.net Git - etc/awesome.git/blob - widget/mem.lua

madduck's git repository

Every one of the projects in this repository is available at the canonical URL git://git.madduck.net/madduck/pub/<projectpath> — see each project's metadata for the exact URL.

All patches and comments are welcome. Please squash your changes to logical commits before using git-format-patch and git-send-email to patches@git.madduck.net. If you'd read over the Git project's submission guidelines and adhered to them, I'd be especially grateful.

SSH access, as well as push access can be individually arranged.

If you use my repositories frequently, consider adding the following snippet to ~/.gitconfig and using the third clone URL listed for each project:

[url "git://git.madduck.net/madduck/"]
  insteadOf = madduck:

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