dumping ground for random patches and texts
 help / color / mirror / Atom feed
* [PATCH] WIP lei complete add-external
@ 2021-02-03  2:12 Eric Wong
  0 siblings, 0 replies; only message in thread
From: Eric Wong @ 2021-02-03  2:12 UTC (permalink / raw)
  To: spew

---
 lib/PublicInbox/LeiExternal.pm | 43 +++++++++++++++++++++++++++-------
 1 file changed, 35 insertions(+), 8 deletions(-)

diff --git a/lib/PublicInbox/LeiExternal.pm b/lib/PublicInbox/LeiExternal.pm
index 6b4c7fb0..23027e66 100644
--- a/lib/PublicInbox/LeiExternal.pm
+++ b/lib/PublicInbox/LeiExternal.pm
@@ -133,17 +133,15 @@ sub lei_forget_external {
 	}
 }
 
-# shell completion helper called by lei__complete
-sub _complete_forget_external {
-	my ($self, @argv) = @_;
-	my $cfg = $self->_lei_cfg(0);
-	my $cur = pop @argv;
+sub _complete_url_common ($) {
+	my ($argv) = @_;
 	# Workaround bash word-splitting URLs to ['https', ':', '//' ...]
 	# Maybe there's a better way to go about this in
 	# contrib/completion/lei-completion.bash
 	my $re = '';
-	if (@argv) {
-		my @x = @argv;
+	my $cur = pop @$argv;
+	if (@$argv) {
+		my @x = @$argv;
 		if ($cur eq ':' && @x) {
 			push @x, $cur;
 			$cur = '';
@@ -154,10 +152,18 @@ sub _complete_forget_external {
 		if (@x >= 2) { # qw(https : hostname : 443) or qw(http :)
 			$re = join('', @x);
 		} else { # just filter out the flags and hope for the best
-			$re = join('', grep(!/^-/, @argv));
+			$re = join('', grep(!/^-/, @$argv));
 		}
 		$re = quotemeta($re);
 	}
+	($cur, $re);
+}
+
+# shell completion helper called by lei__complete
+sub _complete_forget_external {
+	my ($self, @argv) = @_;
+	my $cfg = $self->_lei_cfg(0);
+	my ($cur, $re) = _complete_url_common(\@argv);
 	# FIXME: bash completion off "http:" or "https:" when the last
 	# character is a colon doesn't work properly even if we're
 	# returning "//$HTTP_HOST/$PATH_INFO/", not sure why, could
@@ -174,4 +180,25 @@ sub _complete_forget_external {
 	} grep(/\Aexternal\.$re\Q$cur/, @{$cfg->{-section_order}});
 }
 
+sub _complete_add_external {
+	my ($self, @argv) = @_;
+	my $cfg = $self->_lei_cfg(0);
+	my ($cur, $re) = _complete_url_common(\@argv);
+	my @c;
+	require URI;
+open my $dbg, '>>', '/tmp/dbg'; $dbg->autoflush(1); use Data::Dumper;
+	for (grep(m!\Aexternal\.https?://!, @{$cfg->{-section_order}})) {
+		my $uri = URI->new(substr($_, length('external.')));
+		my ($base) = ($uri->path =~ m!((?:/?.*)?/)[^/]+/?\z!);
+		$uri->path($base);
+		my $url = $uri->as_string;
+print $dbg $url, "  re=$re -- cur=$cur\n";
+		if ($url =~ /\A$re(\Q$cur\E.*)/) {
+			push(@c, $cur eq $1 ? () : $1);
+		}
+	}
+	print $dbg Dumper([$re, $cur, \@c]);
+	@c;
+}
+
 1;

^ permalink raw reply related	[flat|nested] only message in thread

only message in thread, other threads:[~2021-02-03  2:12 UTC | newest]

Thread overview: (only message) (download: mbox.gz follow: Atom feed
-- links below jump to the message on this page --
2021-02-03  2:12 [PATCH] WIP lei complete add-external Eric Wong

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).