]> Sergey Matveev's repositories - public-inbox.git/commitdiff
lei forget-external: bash completion support
authorEric Wong <e@80x24.org>
Thu, 21 Jan 2021 19:46:24 +0000 (19:46 +0000)
committerEric Wong <e@80x24.org>
Fri, 22 Jan 2021 20:18:01 +0000 (16:18 -0400)
The tricky bit was getting around word splitting bash
does on URLs.  This may work with other shells, too.

lib/PublicInbox/LEI.pm
lib/PublicInbox/LeiExternal.pm

index 9c3d7279deffb53521229e91a62c74cd1ef2a126..ef3f90fce9883952ce82986947e64946df0a0260 100644 (file)
@@ -655,6 +655,10 @@ sub lei__complete {
        } elsif ($cmd eq 'config' && !@argv && !$CONFIG_KEYS{$cur}) {
                puts $self, grep(/$re/, keys %CONFIG_KEYS);
        }
+       $cmd =~ tr/-/_/;
+       if (my $sub = $self->can("_complete_$cmd")) {
+               puts $self, $sub->($self, @argv, $cur);
+       }
        # TODO: URLs, pathnames, OIDs, MIDs, etc...  See optparse() for
        # proto parsing.
 }
index 21071058a1a43d9aa6823b9092d85527334bd41e..59c3c3674cefa1dd159a5fb3a184b2e1561952b0 100644 (file)
@@ -93,4 +93,21 @@ 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;
+       # Workaround bash word-splitting URLs to ['https', ':', '//' ...]
+       # Maybe there's a better way to go about this in
+       # contrib/completion/lei-completion.bash
+       my $colon = ($argv[-1] // '') eq ':';
+       my $re = $cur =~ /\A[\w-]/ ? '' : '.*';
+       map {
+               my $x = substr($_, length('external.'));
+               # only return the part specified on the CLI
+               $colon && $x =~ /(\Q$cur\E.*)/ ? $1 : $x;
+       } grep(/\Aexternal\.$re\Q$cur/, @{$cfg->{-section_order}});
+}
+
 1;