]> git.draconx.ca Git - mpdhacks.git/commitdiff
mpdexec: Add "verbose" option to print commands sent to MPD.
authorNick Bowler <nbowler@draconx.ca>
Sun, 25 Jul 2021 17:04:02 +0000 (13:04 -0400)
committerNick Bowler <nbowler@draconx.ca>
Sun, 25 Jul 2021 17:04:02 +0000 (13:04 -0400)
In order to add readpicture support to the thumbnailer in addition to
albumart, we need a better way to distinguish which commands returned
what results from the server.

An easy way to do that is to just print the commands.

mpdexec.pl

index 72f81fe3b6336cbfcfa4f88d4299f7369eeb683a..e83146bcdc065489b6f6e5e4777db430cb19743a 100755 (executable)
@@ -27,11 +27,11 @@ use FindBin;
 use lib "$FindBin::Bin";
 use MPDHacks;
 
 use lib "$FindBin::Bin";
 use MPDHacks;
 
-my ($quiet, $binary, $ignore_errors, $download);
+my ($quiet, $verbose, $binary, $ignore_errors, $download);
 
 sub print_version {
        print <<EOF
 
 sub print_version {
        print <<EOF
-mpdexec.pl 0.8
+mpdexec.pl 0.9
 Copyright © 2021 Nick Bowler
 License GPLv3+: GNU General Public License version 3 or any later version.
 This is free software: you are free to change and redistribute it.
 Copyright © 2021 Nick Bowler
 License GPLv3+: GNU General Public License version 3 or any later version.
 This is free software: you are free to change and redistribute it.
@@ -56,6 +56,8 @@ Options:
   -p, --port=PORT   Connect to the MPD server on PORT, overriding defaults.
   -q, --quiet       Do not output any response messages.  Only errors (on
                     standard error) or binary data (if enabled) are output.
   -p, --port=PORT   Connect to the MPD server on PORT, overriding defaults.
   -q, --quiet       Do not output any response messages.  Only errors (on
                     standard error) or binary data (if enabled) are output.
+  -v, --verbose     Print commands as they are submitted.  If both --quiet
+                    and --verbose are specified, --verbose has no effect.
   -b, --binary[=FILE]
                     Output raw binary response data, which is normally not
                     written.  If FILE is specified, the data is written there.
   -b, --binary[=FILE]
                     Output raw binary response data, which is normally not
                     written.  If FILE is specified, the data is written there.
@@ -79,6 +81,8 @@ GetOptions(
 
        'quiet|q'          => \$quiet,
        'no-quiet'         => sub { $quiet = 0; },
 
        'quiet|q'          => \$quiet,
        'no-quiet'         => sub { $quiet = 0; },
+       'verbose|v'        => \$verbose,
+       'no-verbose'       => sub { $verbose = 0 },
        'binary|b:s'       => \$binary,
        'no-binary'        => sub { $binary = undef; },
 
        'binary|b:s'       => \$binary,
        'no-binary'        => sub { $binary = undef; },
 
@@ -100,6 +104,7 @@ if ($binary) {
        }
 }
 $quiet = 1 if (defined($binary) && $binary eq "");
        }
 }
 $quiet = 1 if (defined($binary) && $binary eq "");
+$verbose = 0 if ($quiet);
 
 my $sock = MPD::connect(binmode => ":raw");
 
 
 my $sock = MPD::connect(binmode => ":raw");
 
@@ -118,6 +123,12 @@ sub read_binary {
        return $rc;
 }
 
        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;
 my %downloadcmds = map { $_ => 1 } ( "albumart", "readpicture" );
 sub mpd_exec {
        my $downloadseq;
@@ -130,7 +141,7 @@ sub mpd_exec {
                $downloadseq = 2;
        }
 
                $downloadseq = 2;
        }
 
-       print $sock encode('UTF-8', join(' ', @_), Encode::FB_QUIET), $/;
+       mpd_send(@_);
        while (<$sock>) {
                $_ = decode('UTF_8', $_, Encode::FB_QUIET);
 
        while (<$sock>) {
                $_ = decode('UTF_8', $_, Encode::FB_QUIET);
 
@@ -162,12 +173,13 @@ sub mpd_exec {
 }
 
 if (@ARGV) {
 }
 
 if (@ARGV) {
-       mpd_exec(map { MPD::escape($_) } @ARGV)
+       mpd_exec(map { MPD::escape } @ARGV)
+} elsif ($ignore_errors) {
+       while (<>) { chomp; mpd_exec($_); }
 } else {
 } else {
-       while (<>) {
-               chomp;
-               mpd_exec($_);
-       }
+       mpd_send("command_list_begin");
+       while (<>) { chomp; mpd_send($_); }
+       mpd_exec("command_list_end");
 }
 
 print $sock "close\n";
 }
 
 print $sock "close\n";