Debian Codegraph @

vicious/widgets/mem_linux.lua

raw symbols SCIP
1 -- RAM and swap usage widget type for GNU/Linux
2 -- Copyright (C) 2010 Adrian C. <[email protected]>
3 -- Copyright (C) 2009 Lucas de Vries <[email protected]>
4 -- Copyright (C) 2017 mutlusun <[email protected]>
5 -- Copyright (C) 2018 Jay Kamat <[email protected]>
6 --
7 -- This file is part of Vicious.
8 --
9 -- Vicious is free software: you can redistribute it and/or modify
10 -- it under the terms of the GNU General Public License as
11 -- published by the Free Software Foundation, either version 2 of the
12 -- License, or (at your option) any later version.
13 --
14 -- Vicious is distributed in the hope that it will be useful,
15 -- but WITHOUT ANY WARRANTY; without even the implied warranty of
16 -- MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
17 -- GNU General Public License for more details.
18 --
19 -- You should have received a copy of the GNU General Public License
20 -- along with Vicious. If not, see <https://www.gnu.org/licenses/>.
21
22 -- {{{ Grab environment
23 local io = { lines = io.lines }
24 local math = { floor = math.floor }
25 local string = { gmatch = string.gmatch }
26 local helpers = require"vicious.helpers"
27 -- }}}
28
29 -- {{{ Memory widget type
30 return helpers.setcall(function ()
31 local _mem = { buf = {}, swp = {} }
32
33 -- Get MEM info
34 for line in io.lines("/proc/meminfo") do
35 for k, v in string.gmatch(line, "([%a]+):[%s]+([%d]+).+") do
36 if k == "MemTotal" then _mem.total = math.floor(v/1024)
37 elseif k == "MemFree" then _mem.buf.f = math.floor(v/1024)
38 elseif k == "MemAvailable" then _mem.buf.a = math.floor(v/1024)
39 elseif k == "Buffers" then _mem.buf.b = math.floor(v/1024)
40 elseif k == "Cached" then _mem.buf.c = math.floor(v/1024)
41 elseif k == "SwapTotal" then _mem.swp.t = math.floor(v/1024)
42 elseif k == "SwapFree" then _mem.swp.f = math.floor(v/1024)
43 end
44 end
45 end
46
47 -- Calculate memory percentage
48 _mem.free = _mem.buf.a
49 _mem.inuse = _mem.total - _mem.free
50 _mem.bcuse = _mem.total - _mem.buf.f
51 _mem.usep = math.floor(_mem.inuse / _mem.total * 100)
52 -- Calculate swap percentage
53 _mem.swp.inuse = _mem.swp.t - _mem.swp.f
54 _mem.swp.usep = math.floor(_mem.swp.inuse / _mem.swp.t * 100)
55
56 return {_mem.usep, _mem.inuse, _mem.total, _mem.free,
57 _mem.swp.usep, _mem.swp.inuse, _mem.swp.t, _mem.swp.f,
58 _mem.bcuse }
59 end)
60 -- }}}