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

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