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 A9AA11FF9D for ; Thu, 21 Jan 2021 19:46:25 +0000 (UTC) From: Eric Wong To: meta@public-inbox.org Subject: [PATCH 12/12] lei forget-external: bash completion support Date: Thu, 21 Jan 2021 19:46:24 +0000 Message-Id: <20210121194624.32002-13-e@80x24.org> In-Reply-To: <20210121194624.32002-1-e@80x24.org> References: <20210121194624.32002-1-e@80x24.org> MIME-Version: 1.0 Content-Transfer-Encoding: 8bit List-Id: The tricky bit was getting around word splitting bash does on URLs. This may work with other shells, too. --- lib/PublicInbox/LEI.pm | 4 ++++ lib/PublicInbox/LeiExternal.pm | 17 +++++++++++++++++ 2 files changed, 21 insertions(+) diff --git a/lib/PublicInbox/LEI.pm b/lib/PublicInbox/LEI.pm index 9c3d7279..ef3f90fc 100644 --- a/lib/PublicInbox/LEI.pm +++ b/lib/PublicInbox/LEI.pm @@ -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. } diff --git a/lib/PublicInbox/LeiExternal.pm b/lib/PublicInbox/LeiExternal.pm index 21071058..59c3c367 100644 --- a/lib/PublicInbox/LeiExternal.pm +++ b/lib/PublicInbox/LeiExternal.pm @@ -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;