]> Sergey Matveev's repositories - public-inbox.git/commitdiff
lei: complete option switch args
authorEric Wong <e@80x24.org>
Wed, 27 Jan 2021 09:42:27 +0000 (03:42 -0600)
committerEric Wong <e@80x24.org>
Fri, 29 Jan 2021 05:04:40 +0000 (05:04 +0000)
And add tests for existing completion cases

lib/PublicInbox/LEI.pm
t/lei.t

index 0ce6a00bb1ad60088047f141ddd8df80bdcb86d9..d5d9cf1f85ab86f8dec3ae111359ca12c02b403c 100644 (file)
@@ -104,8 +104,9 @@ sub _config_path ($) {
 our %CMD = ( # sorted in order of importance/use:
 'q' => [ 'SEARCH_TERMS...', 'search for messages matching terms', qw(
        save-as=s output|mfolder|o=s format|f=s dedupe|d=s thread|t augment|a
-       sort|s=s reverse|r offset=i remote! local! external! pretty mua-cmd=s
-       torsocks=s no-torsocks verbose|v since|after=s until|before=s),
+       sort|s=s reverse|r offset=i remote! local! external! pretty
+       mua-cmd|mua=s no-torsocks torsocks=s verbose|v
+       received-after=s received-before=s sent-after=s sent-since=s),
        PublicInbox::LeiQuery::curl_opt(), opt_dash('limit|n=i', '[0-9]+') ],
 
 'show' => [ 'MID|OID', 'show a given object (Message-ID or object ID)',
@@ -200,7 +201,11 @@ my $ls_format = [ 'OUT|plain|json|null', 'listing output format' ];
 my %OPTDESC = (
 'help|h' => 'show this built-in help',
 'quiet|q' => 'be quiet',
+'verbose|v' => 'be more verbose',
 'solve!' => 'do not attempt to reconstruct blobs from emails',
+'torsocks=s' => ['auto|no|yes',
+               'whether or not to wrap git and curl commands with torsocks'],
+'no-torsocks' => 'alias for --torsocks=no',
 'save-as=s' => ['NAME', 'save a search terms by given name'],
 
 'type=s' => [ 'any|mid|git', 'disambiguate type' ],
@@ -212,7 +217,7 @@ my %OPTDESC = (
        'return all messages in the same thread as the actual match(es)',
 'augment|a' => 'augment --output destination instead of clobbering',
 
-'output|o=s' => [ 'DEST',
+'output|mfolder|o=s' => [ 'DEST',
        "destination (e.g. `/path/to/Maildir', or `-' for stdout)" ],
 'mua-cmd|mua=s' => [ 'COMMAND',
        "MUA to run on --output Maildir or mbox (e.g. `mutt -f %f'" ],
@@ -222,7 +227,7 @@ my %OPTDESC = (
 'mark  format|f=s' => $stdin_formats,
 'forget        format|f=s' => $stdin_formats,
 'q     format|f=s' => [
-       'OUT|maildir|mboxrd|mboxcl2|mboxcl|html|json|jsonl|concatjson',
+       'OUT|maildir|mboxrd|mboxcl2|mboxcl|mboxo|html|json|jsonl|concatjson',
                'specify output format, default depends on --output'],
 'ls-query      format|f=s' => $ls_format,
 'ls-external   format|f=s' => $ls_format,
@@ -673,22 +678,33 @@ sub lei__complete {
                                get-color-name get-colorbool);
                        # fall-through
                }
-               # TODO: arg support
                puts $self, grep(/$re/, map { # generate short/long names
-                       my $eq = '';
-                       if (s/=.+\z//) { # required arg, e.g. output|o=i
-                               $eq = '=';
-                       } elsif (s/:.+\z//) { # optional arg, e.g. mid:s
+                       if (s/[:=].+\z//) { # req/optional args, e.g output|o=i
                        } else { # negation: solve! => no-solve|solve
                                s/\A(.+)!\z/no-$1|$1/;
                        }
                        map {
-                               length > 1 ? "--$_$eq" : "-$_"
+                               my $x = length > 1 ? "--$_" : "-$_";
+                               $x eq $cur ? () : $x;
                        } split(/\|/, $_, -1) # help|h
                } grep { $OPTDESC{"$cmd\t$_"} || $OPTDESC{$_} } @spec);
        } elsif ($cmd eq 'config' && !@argv && !$CONFIG_KEYS{$cur}) {
                puts $self, grep(/$re/, keys %CONFIG_KEYS);
        }
+
+       # switch args (e.g. lei q -f mbox<TAB>)
+       if (($argv[-1] // $cur // '') =~ /\A--?([\w\-]+)\z/) {
+               my $opt = quotemeta $1;
+               puts $self, map {
+                       my $v = $OPTDESC{$_};
+                       $v = $v->[0] if ref($v);
+                       my @v = split(/\|/, $v);
+                       # get rid of ALL CAPS placeholder (e.g "OUT")
+                       # (TODO: completion for external paths)
+                       shift(@v) if uc($v[0]) eq $v[0];
+                       @v;
+               } grep(/\A(?:$cmd\t|)(?:[\w-]+\|)*$opt\b/, keys %OPTDESC);
+       }
        $cmd =~ tr/-/_/;
        if (my $sub = $self->can("_complete_$cmd")) {
                puts $self, $sub->($self, @argv, $cur);
diff --git a/t/lei.t b/t/lei.t
index 693382579faac97c87b55dfa5b17160641efc8bf..3f6702e6785b041092386816e3f4e0d577faa650 100644 (file)
--- a/t/lei.t
+++ b/t/lei.t
@@ -216,6 +216,24 @@ my $test_external = sub {
        like($out, qr/boost=0\n/s, 'ls-external has output');
        ok($lei->(qw(add-external -q https://EXAMPLE.com/ibx)), 'add remote');
        is($err, '', 'no warnings after add-external');
+
+       ok($lei->(qw(_complete lei forget-external)), 'complete for externals');
+       my %comp = map { $_ => 1 } split(/\s+/, $out);
+       ok($comp{'https://example.com/ibx/'}, 'forget external completion');
+       $cfg->each_inbox(sub {
+               my ($ibx) = @_;
+               ok($comp{$ibx->{inboxdir}}, "local $ibx->{name} completion");
+       });
+       for my $u (qw(h http https https: https:/ https:// https://e
+                       https://example https://example. https://example.co
+                       https://example.com https://example.com/
+                       https://example.com/i https://example.com/ibx)) {
+               ok($lei->(qw(_complete lei forget-external), $u),
+                       "partial completion for URL $u");
+               is($out, "https://example.com/ibx/\n",
+                       "completed partial URL $u");
+       }
+
        $lei->('ls-external');
        like($out, qr!https://example\.com/ibx/!s, 'added canonical URL');
        is($err, '', 'no warnings on ls-external');
@@ -304,11 +322,39 @@ my $test_external = sub {
        }
 };
 
+my $test_completion = sub {
+       ok($lei->(qw(_complete lei)), 'no errors on complete');
+       my %out = map { $_ => 1 } split(/\s+/s, $out);
+       ok($out{'q'}, "`lei q' offered as completion");
+       ok($out{'add-external'}, "`lei add-external' offered as completion");
+
+       ok($lei->(qw(_complete lei q)), 'complete q (no args)');
+       %out = map { $_ => 1 } split(/\s+/s, $out);
+       for my $sw (qw(-f --format -o --output --mfolder --augment -a
+                       --mua --mua-cmd --no-local --local --verbose -v
+                       --save-as --no-remote --remote --torsocks
+                       --reverse -r )) {
+               ok($out{$sw}, "$sw offered as completion");
+       }
+
+       ok($lei->(qw(_complete lei q --form)), 'complete q --format');
+       is($out, "--format\n", 'complete lei q --format');
+       for my $sw (qw(-f --format)) {
+               ok($lei->(qw(_complete lei q), $sw), "complete q $sw ARG");
+               %out = map { $_ => 1 } split(/\s+/s, $out);
+               for my $f (qw(mboxrd mboxcl2 mboxcl mboxo json jsonl
+                               concatjson maildir)) {
+                       ok($out{$f}, "got $sw $f as output format");
+               }
+       }
+};
+
 my $test_lei_common = sub {
        $test_help->();
        $test_config->();
        $test_init->();
        $test_external->();
+       $test_completion->();
 };
 
 if ($ENV{TEST_LEI_ONESHOT}) {