From db446c35d902659ce117ade88b789acae9b2f8fe Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Nguy=E1=BB=85n=20Gia=20Phong?= Date: Wed, 5 Jun 2019 10:57:18 +0700 Subject: [PATCH] [mpd] Deprecate io.popen --- Changes.md | 2 +- widgets/mpd_all.lua | 65 +++++++++++++++++++++------------------------ 2 files changed, 32 insertions(+), 35 deletions(-) diff --git a/Changes.md b/Changes.md index be479f5..22fb202 100644 --- a/Changes.md +++ b/Changes.md @@ -20,7 +20,7 @@ Fixed: - Deprecate the use of `io.popen` in following widgets: * wifi_linux, wifiiw_linux * bat_freebsd, mem_freebsd, net_freebsd - * volume, gmail, mdir + * volume, gmail, mdir, mpd - [mpd] Lua 5.3 compatibility (for real this time); also correct a typo - [pkg,weather,contrib/btc] Allow function call without Awesome - [pkg] Use more updated front-ends for Debian/Ubuntu (apt) and Fedora (dnf) diff --git a/widgets/mpd_all.lua b/widgets/mpd_all.lua index 0504c4a..4c2f524 100644 --- a/widgets/mpd_all.lua +++ b/widgets/mpd_all.lua @@ -5,11 +5,10 @@ -- {{{ Grab environment local tonumber = tonumber -local io = { popen = io.popen } -local setmetatable = setmetatable -local string = { gmatch = string.gmatch } -local helpers = require("vicious.helpers") local math = { floor = math.floor } + +local helpers = require"vicious.helpers" +local spawn = require"vicious.spawn" -- }}} @@ -55,7 +54,7 @@ end -- }}} -- {{{ MPD widget type -local function worker(format, warg) +function mpd_all.async(format, warg, callback) -- Fallback values local mpd_state = { ["{volume}"] = 0, @@ -81,36 +80,34 @@ local function worker(format, warg) warg and (warg.port or warg[3]) or "6600") -- Get data from MPD server - local f = io.popen(query .. "|" .. connect) - for line in f:lines() do - for k, v in string.gmatch(line, "([%w]+):[%s](.*)$") do - local key = "{" .. k .. "}" - if k == "volume" or k == "bitrate" or - k == "elapsed" or k == "duration" then - mpd_state[key] = v and tonumber(v) - elseif k == "repeat" or k == "random" then - mpd_state[key] = cbool(v) - elseif k == "state" then - mpd_state[key] = helpers.capitalize(v) - elseif k == "Artist" or k == "Title" or - --k == "Name" or k == "file" or - k == "Album" or k == "Genre" then - mpd_state[key] = v + spawn.with_line_callback_with_shell(query .. "|" .. connect, { + stdout = function (line) + for k, v in line:gmatch"([%w]+):[%s](.*)$" do + local key = "{" .. k .. "}" + if k == "volume" or k == "bitrate" or + k == "elapsed" or k == "duration" then + mpd_state[key] = v and tonumber(v) + elseif k == "repeat" or k == "random" then + mpd_state[key] = cbool(v) + elseif k == "state" then + mpd_state[key] = helpers.capitalize(v) + elseif k == "Artist" or k == "Title" or + --k == "Name" or k == "file" or + k == "Album" or k == "Genre" then + mpd_state[key] = v + end end - end - end - f:close() - - -- Formatted elapsed and duration - mpd_state["{Elapsed}"], mpd_state["{Duration}"] = format_progress( - mpd_state["{elapsed}"], mpd_state["{duration}"]) - - -- Formatted playing progress percentage - mpd_state["{Progress}"] = format_progress_percentage( - mpd_state["{elapsed}"], mpd_state["{duration}"]) - - return mpd_state + end, + output_done = function () + -- Formatted elapsed and duration + mpd_state["{Elapsed}"], mpd_state["{Duration}"] = format_progress( + mpd_state["{elapsed}"], mpd_state["{duration}"]) + -- Formatted playing progress percentage + mpd_state["{Progress}"] = format_progress_percentage( + mpd_state["{elapsed}"], mpd_state["{duration}"]) + callback(mpd_state) + end }) end -- }}} -return setmetatable(mpd_all, { __call = function(_, ...) return worker(...) end }) +return helpers.setasyncall(mpd_all)