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

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