]> 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:

readme updated
[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 local 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     mem.widget = wibox.widget.textbox('')
34
35     function mem.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         widget = mem.widget
55         settings()
56     end
57
58     newtimer("mem", timeout, mem.update)
59
60     return mem.widget
61 end
62
63 return setmetatable(mem, { __call = function(_, ...) return worker(...) end })