altdesktop / playerctl

🎧 mpris media player command-line controller for vlc, mpv, RhythmBox, web browsers, cmus, mpd, spotify and others.
GNU Lesser General Public License v3.0
2.42k stars 79 forks source link

Print string only if metadata is set #296

Open UtkarshVerma opened 1 year ago

UtkarshVerma commented 1 year ago

I wish to replicate the following mpc format in playerctl:

mpc -f "%title%[ - %artist%]" current

It only prints - %artist% if artist is set. How do I achieve this with playerctl?

hseg commented 12 months ago

Same here - am currently hackily using a combination of default() and stripping the default substitution I'm setting.

my $metadata = qx(playerctl $player_arg metadata \\
        --format '{{default(artist, "\004\005")}} - {{title}}' 2>/dev/null);
chomp $metadata;
$metadata =~ s/^\004\005 - //;

(that pair of characters is STX/ETX, probably semantically invalid but it makes some sense to me)