Make mpd widget type expose more info

By default, format time values in [hh:]mm:ss. Also use printf instead of
echo, which is shell-specific.
This commit is contained in:
Nguyễn Gia Phong 2018-12-14 17:26:36 +07:00
parent 5d2eda1225
commit e84be352c5
3 changed files with 61 additions and 23 deletions

View File

@ -335,8 +335,10 @@ Supported platforms: platform independent (required tools: `curl`).
* Argument: an array including password, hostname and port in that order. `nil` * Argument: an array including password, hostname and port in that order. `nil`
fields will be fallen back to default (`localhost:6600` without password). fields will be fallen back to default (`localhost:6600` without password).
* Returns a table with string keys: `${volume}`, `${state}`, `${Artist}`, * Returns a table with string keys: `${volume}`, `${bitrate}`, `${elapsed}`,
`${Title}`, `${Album}`, `${Genre}` and optionally `${Name}` and `${file}`. `${duration}` (in seconds), `${Elapsed}`, `${Duration}` (in [hh:]mm:ss),
`${random}`, `${repeat}`, `${state}`, `${Artist}`, `${Title}`, `${Album}`,
`${Genre}` and optionally `${Name}` and `${file}`.
### vicious.widgets.net ### vicious.widgets.net

View File

@ -136,6 +136,25 @@ function helpers.uformat(array, key, value, unit)
end end
-- }}} -- }}}
-- {{{ Format playing progress
function helpers.format_progress(elapsed, duration)
local em, es = elapsed / 60, elapsed % 60
local dm, ds = duration / 60, duration % 60
if dm < 10 then
return ("%d:%02d"):format(em, es), ("%d:%02d"):format(dm, ds)
elseif dm < 60 then
return ("%02d:%02d"):format(em, es), ("%02d:%02d"):format(dm, ds)
elseif dm < 600 then
return ("%d:%02d:%02d"):format(em / 60, em % 60, es),
("%d:%02d:%02d"):format(dm / 60, dm % 60, ds)
else
return ("%02d:%02d:%02d"):format(em / 60, em % 60, es),
("%02d:%02d:%02d"):format(dm / 60, dm % 60, ds)
end
end
-- }}}
-- {{{ Escape a string -- {{{ Escape a string
function helpers.escape(text) function helpers.escape(text)
local xml_entities = { local xml_entities = {

View File

@ -17,39 +17,52 @@ local helpers = require("vicious.helpers")
local mpd_all = {} local mpd_all = {}
-- {{{ Return true if number is nonzero
local function cbool(number)
return type(number) == "number" and number ~= 0 or number
end
-- }}}
-- {{{ MPD widget type -- {{{ MPD widget type
local function worker(format, warg) local function worker(format, warg)
local mpd_state = { -- Fallback values
["{volume}"] = 0, local mpd_state = {
["{state}"] = "N/A", ["{volume}"] = 0,
["{Artist}"] = "N/A", ["{bitrate}"] = 0,
["{Title}"] = "N/A", ["{elapsed}"] = 0,
["{Album}"] = "N/A", ["{duration}"] = 0,
["{Genre}"] = "N/A", ["{repeat}"] = false,
--["{Name}"] = "N/A", ["{random}"] = false,
--["{file}"] = "N/A", ["{state}"] = "N/A",
["{Artist}"] = "N/A",
["{Title}"] = "N/A",
["{Album}"] = "N/A",
["{Genre}"] = "N/A",
--["{Name}"] = "N/A",
--["{file}"] = "N/A",
} }
-- Fallback to MPD defaults -- Construct MPD client options, fallback to defaults when necessary
local pass = warg and (warg.password or warg[1]) or "\"\"" local query = ("printf 'password %s\nstatus\ncurrentsong\nclose\n'"):format(
local host = warg and (warg.host or warg[2]) or "127.0.0.1" warg and (warg.password or warg[1]) or '""')
local port = warg and (warg.port or warg[3]) or "6600" local connect = ("curl --connect-timeout 1 -fsm 3 telnet://%s:%s"):format(
warg and (warg.host or warg[2]) or "127.0.0.1",
-- Construct MPD client options warg and (warg.port or warg[3]) or "6600")
local mpdh = "telnet://"..host..":"..port
local echo = "echo 'password "..pass.."\nstatus\ncurrentsong\nclose'"
-- Get data from MPD server -- Get data from MPD server
local f = io.popen(echo.." | curl --connect-timeout 1 -fsm 3 "..mpdh) local f = io.popen(query .. "|" .. connect)
for line in f:lines() do for line in f:lines() do
for k, v in string.gmatch(line, "([%w]+):[%s](.*)$") do for k, v in string.gmatch(line, "([%w]+):[%s](.*)$") do
local key = "{" .. k .. "}" local key = "{" .. k .. "}"
if k == "volume" then if k == "volume" or k == "bitrate" or
k == "elapsed" or k == "duration" then
mpd_state[key] = v and tonumber(v) mpd_state[key] = v and tonumber(v)
elseif k == "repeat" or k == "random" then
mpd_state[key] = cbool(v)
elseif k == "state" then elseif k == "state" then
mpd_state[key] = helpers.capitalize(v) mpd_state[key] = helpers.capitalize(v)
elseif k == "Artist" or k == "Title" or elseif k == "Artist" or k == "Title" or
--k == "Name" or k == "file" or --k == "Name" or k == "file" or
k == "Album" or k == "Genre" then k == "Album" or k == "Genre" then
mpd_state[key] = v mpd_state[key] = v
@ -58,6 +71,10 @@ local function worker(format, warg)
end end
f:close() f:close()
-- Formatted elapsed and duration
mpd_state["{Elapsed}"], mpd_state["{Duration}"] = helpers.format_progress(
mpd_state["{elapsed}"], mpd_state["{duration}"])
return mpd_state return mpd_state
end end
-- }}} -- }}}