From mboxrd@z Thu Jan 1 00:00:00 1970 Return-Path: X-Spam-Checker-Version: SpamAssassin 3.4.2 (2018-09-13) on dcvr.yhbt.net X-Spam-Level: X-Spam-Status: No, score=-4.0 required=3.0 tests=ALL_TRUSTED,BAYES_00 shortcircuit=no autolearn=ham autolearn_force=no version=3.4.2 Received: from localhost (dcvr.yhbt.net [127.0.0.1]) by dcvr.yhbt.net (Postfix) with ESMTP id 97EEB1FA19 for ; Wed, 27 Jan 2021 09:42:31 +0000 (UTC) From: Eric Wong To: meta@public-inbox.org Subject: [PATCH 6/9] lei: complete option switch args Date: Wed, 27 Jan 2021 03:42:27 -0600 Message-Id: <20210127094230.31174-7-e@80x24.org> In-Reply-To: <20210127094230.31174-1-e@80x24.org> References: <20210127094230.31174-1-e@80x24.org> MIME-Version: 1.0 Content-Transfer-Encoding: 8bit List-Id: And add tests for existing completion cases --- lib/PublicInbox/LEI.pm | 36 ++++++++++++++++++++++++--------- t/lei.t | 46 ++++++++++++++++++++++++++++++++++++++++++ 2 files changed, 72 insertions(+), 10 deletions(-) diff --git a/lib/PublicInbox/LEI.pm b/lib/PublicInbox/LEI.pm index 0ce6a00b..d5d9cf1f 100644 --- a/lib/PublicInbox/LEI.pm +++ b/lib/PublicInbox/LEI.pm @@ -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) + 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 69338257..3f6702e6 100644 --- 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}) {