Take the formatted output of mpc status
and convert it to JOSN.
I'm running Übersicht and made some tweaks to another users mpd widget. The existing implementation used awk
and echoed values concatenating them together with @
. Then in JS/Coffeescript parsing the string on @
and accessing the values based on expected position. It occurred to me it would be more convenient to use JSON in Übersicht so I created this.
By default mpc status
return something like this:
❯ mpc status
I'm With Her - Ain't The Fine
[paused] #3/12 2:38/3:09 (83%)
volume: 64% repeat: off random: off single: off consume: off
This script will return results like this:
❯ ./mpc-status-json.sh
{
"artist": "I'm With Her",
"song": "Ain't The Fine",
"elapsed": "2:38/3:09",
"currentTime": "2:38",
"trackLength": "3:09",
"currentTrackNumber": "3",
"totalTrackCount": "12",
"status": "[paused]",
"trackCount": "",
"volume": "64%"
}
Then in a JS environment simply parse the resulting string. For example in Übersicht:
update: (output, domEl) ->
details = JSON.parse(output);
$(domEl).find('#artist').text(details.artist)
...