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

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