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

dc72279d800e787ea684062d9138e0c6d7c6ab1a
[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 helpers      = require("lain.helpers")
11 local wibox        = require("wibox")
12
13 local io           = { lines  = io.lines }
14 local math         = { floor  = math.floor }
15 local string       = { gmatch = string.gmatch }
16
17 local setmetatable = setmetatable
18
19 -- Memory usage
20 -- lain.widgets.mem
21 local mem = {}
22
23 local function worker(args)
24     local args     = args or {}
25     local timeout  = args.timeout or 1
26     local settings = args.settings or function() end
27
28     mem.widget = wibox.widget.textbox('')
29
30     helpers.set_map("mem_last_total", 0)
31     helpers.set_map("mem_last_free", 0)
32     helpers.set_map("mem_last_buf", 0)
33     helpers.set_map("mem_last_cache", 0)
34     helpers.set_map("mem_last_swap", 0)
35     helpers.set_map("mem_last_swapf", 0)
36
37     function update()
38         mem_now = {}
39         for line in io.lines("/proc/meminfo")
40         do
41             for k, v in string.gmatch(line, "([%a]+):[%s]+([%d]+).+")
42             do
43                 if     k == "MemTotal"  then mem_now.total = math.floor(v / 1024)
44                 elseif k == "MemFree"   then mem_now.free  = math.floor(v / 1024)
45                 elseif k == "Buffers"   then mem_now.buf   = math.floor(v / 1024)
46                 elseif k == "Cached"    then mem_now.cache = math.floor(v / 1024)
47                 elseif k == "SwapTotal" then mem_now.swap  = math.floor(v / 1024)
48                 elseif k == "SwapFree"  then mem_now.swapf = math.floor(v / 1024)
49                 end
50             end
51         end
52
53         if mem_now.total ~= helpers.set_map("mem_last_total")
54         or mem_now.free  ~= helpers.set_map("mem_last_free")
55         or mem_now.buf   ~= helpers.set_map("mem_last_buf")
56         or mem_now.cache ~= helpers.set_map("mem_last_cache")
57         or mem_now.swap  ~= helpers.set_map("mem_last_swap")
58         or mem_now.swapf ~= helpers.set_map("mem_last_swapf")
59         then
60             mem_now.used = mem_now.total - (mem_now.free + mem_now.buf + mem_now.cache)
61             mem_now.swapused = mem_now.swap - mem_now.swapf
62
63             widget = mem.widget
64             settings()
65
66             helpers.set_map("mem_last_total", mem_now.total)
67             helpers.set_map("mem_last_free", mem_now.free)
68             helpers.set_map("mem_last_buf", mem_now.buf)
69             helpers.set_map("mem_last_cache", mem_now.cache)
70             helpers.set_map("mem_last_swap", mem_now.swap)
71             helpers.set_map("mem_last_swapf", mem_now.swapf)
72         end
73     end
74
75     helpers.newtimer("mem", timeout, update)
76
77     return mem.widget
78 end
79
80 return setmetatable(mem, { __call = function(_, ...) return worker(...) end })