[mpd] Deprecate io.popen

This commit is contained in:
Nguyễn Gia Phong 2019-06-05 10:57:18 +07:00
parent b0b6d46937
commit db446c35d9
2 changed files with 32 additions and 35 deletions

View File

@ -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)

View File

@ -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)