Repository navigation
Expand file tree
/
Copy pathshared.lua
More file actions
68 lines (53 loc) · 2.05 KB
/
Copy pathshared.lua
File metadata and controls
68 lines (53 loc) · 2.05 KB
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
-- This file is released into the public domain. See COPYING.
-- shared.lua : Shared functions used by multiple scripts
local shared = {}
-- Takes in an array of /proc/meminfo keys and returns a map of the given keys to their values
shared.get_meminfo_values = function(keys)
local values = {}
local value_pattern = "%d+"
local num_remaining_keys = 0
for _, _ in ipairs(keys) do
num_remaining_keys = num_remaining_keys + 1
end
local proc_meminfo = io.open("/proc/meminfo", "r")
for line in proc_meminfo:lines() do
-- Search for any remaining keys
for key_index, key_string in ipairs(keys) do
local key_pattern = key_string .. ":"
if (string.match(line, key_pattern)) then
local value_string = string.match(line, value_pattern)
values[key_string] = tonumber(value_string)
table.remove(keys, key_index)
num_remaining_keys = num_remaining_keys - 1
break
end
end
-- Exit if there are no more keys to search for
if (num_remaining_keys == 0) then
break
end
end
return values
end
shared.format_output = function(usage_percent, icon, warning_percent, warning_color)
-- css tag
local tag_css = "<css>.genmon_value {padding-right: 8px;}</css>"
-- icon tag
local tag_icon = "<icon>" .. icon .. "</icon>"
-- text tag
local tag_text = nil
local usage_percent_rounded = math.floor(usage_percent)
local usage_percent_string = tostring(usage_percent_rounded)
if (string.len(usage_percent_string) == 1) then
usage_percent_string = " " .. usage_percent_string
end
usage_percent_string = usage_percent_string .. "%"
if (usage_percent >= warning_percent) then
tag_text = "<txt><span fgcolor='" .. warning_color .. "'>" .. usage_percent_string .. "</span></txt>"
else
tag_text = "<txt>" .. usage_percent_string .. "</txt>"
end
-- combine tags
return tag_css .. tag_icon .. tag_text
end
return shared