user/dev discussion of public-inbox itself
 help / color / mirror / code / Atom feed
From: Eric Wong <e@80x24.org>
To: meta@public-inbox.org
Subject: [PATCH 6/9] lei: complete option switch args
Date: Wed, 27 Jan 2021 03:42:27 -0600	[thread overview]
Message-ID: <20210127094230.31174-7-e@80x24.org> (raw)
In-Reply-To: <20210127094230.31174-1-e@80x24.org>

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<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 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}) {

  parent reply	other threads:[~2021-01-27  9:42 UTC|newest]

Thread overview: 10+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2021-01-27  9:42 [PATCH 0/9] lei completion, some small updates Eric Wong
2021-01-27  9:42 ` [PATCH 1/9] eml: favor index() over regexp match Eric Wong
2021-01-27  9:42 ` [PATCH 2/9] lei: drop "git" command forwarding Eric Wong
2021-01-27  9:42 ` [PATCH 3/9] lei: fix comment regarding client payload Eric Wong
2021-01-27  9:42 ` [PATCH 4/9] lei: set PWD correctly for path expansion Eric Wong
2021-01-27  9:42 ` [PATCH 5/9] gcf2: rely on Perl 5.10 to avoid needless ++ Eric Wong
2021-01-27  9:42 ` Eric Wong [this message]
2021-01-27  9:42 ` [PATCH 7/9] lei_overview: clear redundant ovv_buf definition Eric Wong
2021-01-27  9:42 ` [PATCH 8/9] v2writable: nproc: use sysconf() on Linux and FreeBSD Eric Wong
2021-01-27  9:42 ` [PATCH 9/9] lei: dclose: fix typo Eric Wong

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

  List information: https://public-inbox.org/README

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=20210127094230.31174-7-e@80x24.org \
    --to=e@80x24.org \
    --cc=meta@public-inbox.org \
    --subject='Re: [PATCH 6/9] lei: complete option switch args' \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link

Code repositories for project(s) associated with this inbox:

	https://80x24.org/public-inbox.git

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for read-only IMAP folder(s) and NNTP newsgroup(s).