]> git.madduck.net Git - etc/awesome.git/blob - widgets/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:

totally reworked widgets
[etc/awesome.git] / widgets / 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       * (c) 2010,      Adrian C.      <anrxc@sysphere.org> 
8       * (c) 2009,      Lucas de Vries <lucas@glacicle.com> 
9                                                            
10 --]]
11
12 local newtimer        = require("lain.helpers").newtimer
13
14 local wibox           = require("wibox")
15
16 local io              = { lines  = io.lines }
17 local math            = { floor  = math.floor }
18 local string          = { format = string.format,
19                           gmatch = string.gmatch,
20                           len    = string.len }
21
22 local setmetatable    = setmetatable
23
24 -- Memory usage (ignoring caches)
25 -- lain.widgets.mem
26 local mem = {}
27
28 function worker(args)
29     local args     = args or {}
30     local timeout  = args.timeout or 3
31     local settings = args.settings or function() end
32
33     widget = wibox.widget.textbox('')
34
35     function update()
36         mem = {}
37         for line in io.lines("/proc/meminfo")
38         do
39             for k, v in string.gmatch(line, "([%a]+):[%s]+([%d]+).+")
40             do
41                 if     k == "MemTotal"  then mem.total = math.floor(v / 1024)
42                 elseif k == "MemFree"   then mem.free  = math.floor(v / 1024)
43                 elseif k == "Buffers"   then mem.buf   = math.floor(v / 1024)
44                 elseif k == "Cached"    then mem.cache = math.floor(v / 1024)
45                 elseif k == "SwapTotal" then mem.swap  = math.floor(v / 1024)
46                 elseif k == "SwapFree"  then mem.swapf = math.floor(v / 1024)
47                 end
48             end
49         end
50
51         used = mem.total - (mem.free + mem.buf + mem.cache)
52         swapused = mem.swap - mem.swapf
53
54         settings()
55     end
56
57     newtimer("mem", timeout, update)
58
59     return widget
60 end
61
62 return setmetatable(mem, { __call = function(_, ...) return worker(...) end })