X-Git-Url: https://git.draconx.ca/gitweb/mpdhacks.git/blobdiff_plain/8a39a0fd4ed8d545759e07937fa4c66f5d324987..HEAD:/mpdexec.pl diff --git a/mpdexec.pl b/mpdexec.pl index 4d25ead..e83146b 100755 --- a/mpdexec.pl +++ b/mpdexec.pl @@ -1,6 +1,6 @@ #!/usr/bin/env perl # -# Copyright © 2012,2019-2020 Nick Bowler +# Copyright © 2012,2019-2021 Nick Bowler # # Send commands to MPD. Each command-line argument is quoted as necessary # so it appears as a single argument at the protocol level. The result is @@ -27,12 +27,12 @@ use FindBin; use lib "$FindBin::Bin"; use MPDHacks; -my ($quiet, $binary, $ignore_errors, $download); +my ($quiet, $verbose, $binary, $ignore_errors, $download); sub print_version { print < \$quiet, 'no-quiet' => sub { $quiet = 0; }, + 'verbose|v' => \$verbose, + 'no-verbose' => sub { $verbose = 0 }, 'binary|b:s' => \$binary, 'no-binary' => sub { $binary = undef; }, @@ -100,6 +104,7 @@ if ($binary) { } } $quiet = 1 if (defined($binary) && $binary eq ""); +$verbose = 0 if ($quiet); my $sock = MPD::connect(binmode => ":raw"); @@ -118,18 +123,25 @@ sub read_binary { return $rc; } +sub mpd_send { + my $cmd = encode('UTF-8', join(' ', @_), Encode::FB_QUIET); + print "$cmd\n" if ($verbose); + print $sock $cmd, $/; +} + +my %downloadcmds = map { $_ => 1 } ( "albumart", "readpicture" ); sub mpd_exec { my $downloadseq; - # special case for "albumart"; if no offset is specified - # (invalid command) we synthesize a sequence of albumart + # special case for "albumart" and "readpicture"; if no offset is + # specified (invalid command) we synthesize a sequence of albumart # commands to retrieve the entire file. - if ($download && $_[0] eq "albumart" && @_ == 2) { + if ($download && $downloadcmds{$_[0]} && @_ == 2) { $_[2] = 0; $downloadseq = 2; } - print $sock encode('UTF-8', join(' ', @_), Encode::FB_QUIET), $/; + mpd_send(@_); while (<$sock>) { $_ = decode('UTF_8', $_, Encode::FB_QUIET); @@ -161,12 +173,13 @@ sub mpd_exec { } if (@ARGV) { - mpd_exec(map { MPD::escape($_) } @ARGV) + mpd_exec(map { MPD::escape } @ARGV) +} elsif ($ignore_errors) { + while (<>) { chomp; mpd_exec($_); } } else { - while (<>) { - chomp; - mpd_exec($_); - } + mpd_send("command_list_begin"); + while (<>) { chomp; mpd_send($_); } + mpd_exec("command_list_end"); } print $sock "close\n";