[hwmontemp] Bring back sysfs path cache

Cache sysfs path resolved from given name index in local table. This avoids
spawning a `grep` process over and over again for each invocation.

Co-authored-by: Nguyễn Gia Phong <mcsinyx@disroot.org>
This commit is contained in:
Alexander Koch 2020-08-26 13:53:15 +02:00 committed by Alexander Koch
parent a2f83bf9d6
commit a48e75b45b
1 changed files with 25 additions and 12 deletions

View File

@ -1,5 +1,5 @@
-- widget type providing name-indexed temperatures from /sys/class/hwmon
-- Copyright (C) 2019 Alexander Koch <lynix47@gmail.com>
-- Copyright (C) 2019, 2020 Alexander Koch <lynix47@gmail.com>
-- Copyright (C) 2019 Nguyễn Gia Phong <vn.mcsinyx@gmail.com>
--
-- This file is part of Vicious.
@ -25,6 +25,14 @@ local io = { open = io.open }
local helpers = require"vicious.helpers"
local spawn = require"vicious.spawn"
local pathcache = {}
local function read_sensor(path, callback)
local f = io.open(path, "r")
callback{ tonumber(f:read"*line") / 1000 }
f:close()
end
-- hwmontemp: provides name-indexed temps from /sys/class/hwmon
-- vicious.widgets.hwmontemp
return helpers.setasyncall{
@ -32,6 +40,7 @@ return helpers.setasyncall{
if type(warg) ~= "table" or type(warg[1]) ~= "string" then
return callback{}
end
local input = warg[2]
if type(input) == "number" then
input = ("temp%d_input"):format(input)
@ -39,16 +48,20 @@ return helpers.setasyncall{
input = "temp1_input"
end
spawn.easy_async_with_shell(
"grep " .. warg[1] .. " -wl /sys/class/hwmon/*/name",
function (stdout, stderr, exitreason, exitcode)
if exitreason == "exit" and exitcode == 0 then
local f = io.open(stdout:gsub("name%s+", input), "r")
callback{ tonumber(f:read"*line") / 1000 }
f:close()
else
callback{}
end
end)
local sensor = warg[1]
if pathcache[sensor] then
read_sensor(pathcache[sensor] .. input, callback)
else
spawn.easy_async_with_shell(
"grep " .. sensor .. " -wl /sys/class/hwmon/*/name",
function (stdout, stderr, exitreason, exitcode)
if exitreason == "exit" and exitcode == 0 then
pathcache[sensor] = stdout:gsub("name%s+", "")
read_sensor(pathcache[sensor] .. input, callback)
else
callback{}
end
end)
end
end }
-- vim: ts=4:sw=4:expandtab