]> Sergey Matveev's repositories - public-inbox.git/commitdiff
lei: support implicit stdin by default
authorEric Wong <e@80x24.org>
Sun, 30 May 2021 06:33:58 +0000 (06:33 +0000)
committerEric Wong <e@80x24.org>
Sun, 30 May 2021 08:09:10 +0000 (08:09 +0000)
This adds implicit stdin suppport for p2q and lcat,
while rm and rediff no longer need explicit support
for it.

lib/PublicInbox/LEI.pm
lib/PublicInbox/LeiRediff.pm
lib/PublicInbox/LeiRm.pm
t/lei-p2q.t

index f2dfc320efaf2bc07e4a9c9eed8b50190e2749c1..3527cf09afb2a7932e4b0030a1624a1b70ad27da 100644 (file)
@@ -638,7 +638,15 @@ sub optparse ($$$) {
                        my $ok;
                        for my $o (@or) {
                                if ($o =~ /\A--([a-z0-9\-]+)/) {
-                                       $ok = defined($OPT->{$1});
+                                       my $sw = $1;
+                                       # assume pipe/regular file on stdin
+                                       # w/o args means stdin
+                                       if ($sw eq 'stdin' && !@$argv &&
+                                                       (-p $self->{0} ||
+                                                        -f _) && -r _) {
+                                               $OPT->{stdin} //= 1;
+                                       }
+                                       $ok = defined($OPT->{$sw});
                                        last if $ok;
                                } elsif (defined($argv->[$i])) {
                                        $ok = 1;
@@ -906,7 +914,7 @@ sub start_mua {
        }
        push @cmd, $mfolder unless defined($replaced);
        if ($self->{sock}) { # lei(1) client process runs it
-               # restore terminal: echo $query | lei q -stdin --mua=...
+               # restore terminal: echo $query | lei q --stdin --mua=...
                my $io = [];
                $io->[0] = $self->{1} if $self->{opt}->{stdin} && -t $self->{1};
                send_exec_cmd($self, $io, \@cmd, {});
index 2e793df585ae82c9f6419b52848202cb21bc345c..c8bd0dfbe19ead99c9673ab91ad154f0a5504590 100644 (file)
@@ -201,7 +201,6 @@ sub input_eml_cb { # callback for all emails
 sub lei_rediff {
        my ($lei, @inputs) = @_;
        $lei->_lei_store(1)->write_prepare($lei);
-       $lei->{opt}->{stdin} = 1 if !@inputs;
        $lei->{opt}->{'in-format'} //= 'eml';
        # maybe it's a non-email (code) blob from a coderepo
        my $git_dirs = $lei->{opt}->{'git-dir'} //= [];
index 185b6a1554f34ba4fd581818a768658777ae2499..c6d280452f12c70bed10a3e694bc63fe6f8c16ae 100644 (file)
@@ -31,7 +31,6 @@ sub input_maildir_cb {
 sub lei_rm {
        my ($lei, @inputs) = @_;
        $lei->_lei_store(1)->write_prepare($lei);
-       $lei->{opt}->{stdin} = 1 if !@inputs;
        $lei->{opt}->{'in-format'} //= 'eml';
        my $self = bless { -wq_nr_workers => 1 }, __PACKAGE__;
        $self->prepare_inputs($lei, \@inputs) or return;
index f8b073cfc679a67fa68e5ea352ccf8674d5b358b..58506f944a208ad58501cd2f2724c8b5e6fcb7d5 100644 (file)
@@ -14,6 +14,10 @@ test_lei(sub {
        lei_ok([qw(p2q -w dfpost -)], undef, { %$lei_opt, 0 => $fh });
        is($lei_out, "dfpost:6e006fd73b1d\n", '--stdin') or diag $lei_err;
 
+       sysseek($fh, 0, 0) or xbail "lseek: $!";
+       lei_ok([qw(p2q -w dfpost)], undef, { %$lei_opt, 0 => $fh });
+       is($lei_out, "dfpost:6e006fd73b1d\n", 'implicit --stdin');
+
        lei_ok(qw(p2q --uri t/data/0001.patch -w), 'dfpost,dfn');
        is($lei_out, "dfpost%3A6e006fd73b1d+".
                "dfn%3Alib%2FPublicInbox%2FSearch.pm\n",