File: init.lua

package info (click to toggle)
awesome-extra 2023010601
  • links: PTS, VCS
  • area: main
  • in suites: bookworm, forky, sid, trixie
  • size: 1,340 kB
  • sloc: cpp: 112; sh: 84; makefile: 25; python: 11
file content (76 lines) | stat: -rw-r--r-- 2,191 bytes parent folder | download | duplicates (2)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
-----------------------------------
-- Author: Uli Schlachter        --
-- Copyright 2009 Uli Schlachter --
-----------------------------------

local io = io
local tonumber = tonumber
local setmetatable = setmetatable
local lib = {
  widget = require("obvious.lib.widget")
}

local function mem_usage()
  local f = io.open("/proc/meminfo")
  local ret = { }

  for line in f:lines() do
    if line:match('^MemTotal:') then
      ret.total = tonumber(line:match('(%d+)'))
    elseif line:match('^MemFree:') then
      ret.free = tonumber(line:match('(%d+)'))
    elseif line:match('^Buffers:') then
      ret.bufs = tonumber(line:match('(%d+)'))
    elseif line:match('^Cached:') then
      ret.cached = tonumber(line:match('(%d+)'))
    elseif line:match('^SwapTotal:') then
      ret.swap_total = tonumber(line:match('(%d+)'))
    elseif line:match('^SwapFree:') then
      ret.swap_free = tonumber(line:match('(%d+)'))
    end
  end

  f:close()

  ret.avail = ret.free + ret.bufs + ret.cached
  ret.used = ret.total - ret.avail
  ret.perc = (100 / ret.total) * ret.used
  ret.perc_not_free = (100 / ret.total) * (ret.total - ret.free)

  ret.swap_used = ret.swap_total - ret.swap_free
  ret.swap_perc = (100 / ret.swap_total) * ret.swap_used

  -- The following values are returned:
  -- * total         Total Memory
  -- * free          Total free memory
  -- * bufs          Memory used for buffers
  -- * cached        Memory used for caches
  -- * avail         Memory not used by user space
  -- * used          Memory used by user space
  -- * perc          Percentage of memory used by user space
  -- * perc_not_free Percentage of memory which is not free (different from 100 - perc!)
  -- * swap_total    Total swap space
  -- * swap_free     Free swap space
  -- * swap_used     Used swap space
  -- * swap_perc     Percentage of swap space used

  return ret
end

local function get()
  return mem_usage().perc
end

local function get_data_source()
  local ret = {}

  ret.max = 100
  ret.get = get

  return lib.widget.from_data_source(ret)
end

return setmetatable({
}, { __call = function (_, ...) return get_data_source(...) end })

-- vim:ft=lua:ts=2:sw=2:sts=2:tw=80:et