From 0e7f5e5bcbd4a3d77070ed52f2d7fe5e8a8fff68 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Nguy=E1=BB=85n=20Gia=20Phong?= Date: Sat, 22 Jun 2019 18:12:37 +0700 Subject: [PATCH] [hddtemp_linux] Deprecate io.popen --- Changes.md | 2 +- widgets/hddtemp_linux.lua | 44 ++++++++++++++------------------------- 2 files changed, 17 insertions(+), 29 deletions(-) diff --git a/Changes.md b/Changes.md index 0836c31..92c91d8 100644 --- a/Changes.md +++ b/Changes.md @@ -18,7 +18,7 @@ Added: Fixed: - Deprecate the use of `io.popen` in following widgets: - * wifi_linux, wifiiw_linux, hwmontemp_linux + * wifi_linux, wifiiw_linux, hwmontemp_linux, hddtemp_linux * bat_freebsd, mem_freebsd, net_freebsd * volume, gmail, mdir, mpd, fs - [mpd] Lua 5.3 compatibility (for real this time); also correct a typo diff --git a/widgets/hddtemp_linux.lua b/widgets/hddtemp_linux.lua index c46eb13..b381f2f 100644 --- a/widgets/hddtemp_linux.lua +++ b/widgets/hddtemp_linux.lua @@ -5,36 +5,24 @@ -- {{{ Grab environment local tonumber = tonumber -local io = { popen = io.popen } -local setmetatable = setmetatable -local string = { gmatch = string.gmatch } -local helpers = require("vicious.helpers") + +local helpers = require"vicious.helpers" +local spawn = require"vicious.spawn" -- }}} -- Hddtemp: provides hard drive temperatures using the hddtemp daemon -- vicious.widgets.hddtemp -local hddtemp_linux = {} - - --- {{{ HDD Temperature widget type -local function worker(format, warg) - -- Fallback to default hddtemp port - if warg == nil then warg = 7634 end - - local hdd_temp = {} -- Get info from the hddtemp daemon - local quoted = helpers.shellquote(warg) - local f = io.popen("echo | curl --connect-timeout 1 -fsm 3 telnet://127.0.0.1:"..quoted) - - for line in f:lines() do - for d, t in string.gmatch(line, "|([%/%a%d]+)|.-|([%d]+)|[CF]+|") do - hdd_temp["{"..d.."}"] = tonumber(t) - end - end - f:close() - - return hdd_temp -end --- }}} - -return setmetatable(hddtemp_linux, { __call = function(_, ...) return worker(...) end }) +return helpers.setasyncall{ + async = function(format, warg, callback) + if warg == nil then warg = 7634 end -- fallback to default hddtemp port + local hdd_temp = {} -- get info from the hddtemp daemon + spawn.with_line_callback_with_shell( + "echo | curl -fs telnet://127.0.0.1:" .. warg, + { stdout = function (line) + for d, t in line:gmatch"|([%/%w]+)|.-|(%d+)|[CF]|" do + hdd_temp["{"..d.."}"] = tonumber(t) + end + end, + output_done = function () callback(hdd_temp) end }) + end }