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

version 1.0
[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 markup          = require("lain.util.markup")
13 local run_in_terminal = require("lain.helpers").run_in_terminal
14
15 local beautiful       = require("beautiful")
16 local wibox           = require("wibox")
17
18 local io              = { lines  = io.lines }
19 local math            = { floor  = math.floor }
20 local string          = { format = string.format,
21                           gmatch = string.gmatch,
22                           len    = string.len }
23
24 local setmetatable    = setmetatable
25
26 -- Memory usage (ignoring caches)
27 -- lain.widgets.mem
28 local mem = {}
29
30 function worker(args)
31     local args = args or {}
32     local refresh_timeout = args.refresh_timeout or 10
33     local show_swap = args.show_swap or false
34     local show_total = args.show_total or false
35     local header = args.header or " Mem "
36     local header_color = args.header or beautiful.fg_normal or "#FFFFFF"
37     local color = args.color or beautiful.fg_focus or "#FFFFFF"
38     local footer = args.footer or "MB"
39
40     local widg = wibox.widget.textbox()
41
42     local upd = function()
43         local mem = {}
44         for line in io.lines("/proc/meminfo")
45         do
46             for k, v in string.gmatch(line, "([%a]+):[%s]+([%d]+).+")
47             do
48                 if     k == "MemTotal"  then mem.total = math.floor(v / 1024)
49                 elseif k == "MemFree"   then mem.free  = math.floor(v / 1024)
50                 elseif k == "Buffers"   then mem.buf   = math.floor(v / 1024)
51                 elseif k == "Cached"    then mem.cache = math.floor(v / 1024)
52                 elseif k == "SwapTotal" then mem.swap  = math.floor(v / 1024)
53                 elseif k == "SwapFree"  then mem.swapf = math.floor(v / 1024)
54                 end
55             end
56         end
57
58         used = mem.total - (mem.free + mem.buf + mem.cache)
59         swapused = mem.swap - mem.swapf
60
61         if show_total
62         then
63             local fmt = "%" .. string.len(mem.total) .. ".0f/%.0f"
64             widg:set_markup(markup(header_color, header) ..
65                             markup(color, string.format(fmt, used, mem.total) .. footer .. " "))
66         else
67             widg:set_markup(markup(header_color, header) ..
68                             markup(color, used .. footer .. " "))
69         end
70
71         if show_swap
72         then
73             widg:set_markup(widg._layout.text .. ' ('
74                             .. string.format('%.0f '.. footer, swapused)
75                             .. ') ')
76         end
77     end
78
79     local tmr = timer({ timeout = refresh_timeout })
80     tmr:connect_signal("timeout", upd)
81     tmr:start()
82     tmr:emit_signal("timeout")
83
84     return widg
85 end
86
87 return setmetatable(mem, { __call = function(_, ...) return worker(...) end })