Debian Codegraph @

vicious/contrib/sensors_linux.lua

raw symbols SCIP
1 -- contrib/sensors_linux.lua
2 -- Copyright (C) 2010 Greg D. <[email protected]>
3 -- Copyright (C) 2017 Jörg Thalheim <[email protected]>
4 --
5 -- This file is part of Vicious.
6 --
7 -- Vicious is free software: you can redistribute it and/or modify
8 -- it under the terms of the GNU General Public License as
9 -- published by the Free Software Foundation, either version 2 of the
10 -- License, or (at your option) any later version.
11 --
12 -- Vicious is distributed in the hope that it will be useful,
13 -- but WITHOUT ANY WARRANTY; without even the implied warranty of
14 -- MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 -- GNU General Public License for more details.
16 --
17 -- You should have received a copy of the GNU General Public License
18 -- along with Vicious. If not, see <https://www.gnu.org/licenses/>.
19
20 -- {{{ Grab environment
21 local tonumber = tonumber
22 local io = { popen = io.popen }
23 local setmetatable = setmetatable
24 local table = { insert = table.insert }
25 local string = {
26 gsub = string.gsub,
27 match = string.match
28 }
29 -- }}}
30
31
32 -- Sensors: provides access to lm_sensors data
33 -- vicious.contrib.sensors
34 local sensors_linux = {}
35
36
37 -- {{{ Split helper function
38 local function datasplit(str)
39 -- Splitting strings into associative array
40 -- with some magic to get the values right.
41 str = string.gsub(str, "\n", ":")
42
43 local tbl = {}
44 string.gsub(str, "([^:]*)", function (v)
45 if string.match(v, ".") then
46 table.insert(tbl, v)
47 end
48 end)
49
50 local assoc = {}
51 for c = 1, #tbl, 2 do
52 local k = string.gsub(tbl[c], ".*_", "")
53 local v = tonumber(string.match(tbl[c+1], "[%d]+"))
54 assoc[k] = v
55 end
56
57 return assoc
58 end
59 -- }}}
60
61 -- {{{ Sensors widget type
62 local function worker(format, warg)
63 -- Get data from all sensors
64 local f = io.popen("LANG=C sensors -uA")
65 local lm_sensors = f:read("*all")
66 f:close()
67
68 local sensor_data = string.gsub(
69 string.match(lm_sensors, warg..":\n(%s%s.-)\n[^ ]"), " ", "")
70
71 -- One of: crit, max
72 local divisor = "crit"
73 local s_data = datasplit(sensor_data)
74
75 if s_data[divisor] and s_data[divisor] > 0 then
76 s_data.percent = s_data.input / s_data[divisor] * 100
77 end
78
79 return {s_data.input, tonumber(s_data.percent)}
80 end
81 -- }}}
82
83 return setmetatable(sensors_linux, { __call = function(_, ...) return worker(...) end })