* [PATCH 09/11] lei add-external: completion for existing URL basenames
2021-02-03 8:11 7% [PATCH 00/11] lei q --stdin, shortcut names, etc Eric Wong
@ 2021-02-03 8:11 5% ` Eric Wong
0 siblings, 0 replies; 2+ results
From: Eric Wong @ 2021-02-03 8:11 UTC (permalink / raw)
To: meta
Given the presence of one external on a certain host or prefix
path, it's logical other inboxes would share a common prefix.
For bash users, attempt to complete that using the "-o nospace"
option of bash
---
contrib/completion/lei-completion.bash | 6 ++++
lib/PublicInbox/LeiExternal.pm | 44 ++++++++++++++++++--------
t/lei.t | 3 ++
3 files changed, 39 insertions(+), 14 deletions(-)
diff --git a/contrib/completion/lei-completion.bash b/contrib/completion/lei-completion.bash
index 0b82b109..fbda474c 100644
--- a/contrib/completion/lei-completion.bash
+++ b/contrib/completion/lei-completion.bash
@@ -4,6 +4,12 @@
# preliminary bash completion support for lei (Local Email Interface)
# Needs a lot of work, see `lei__complete' in lib/PublicInbox::LEI.pm
_lei() {
+ case ${COMP_WORDS[@]} in
+ *' add-external http'*)
+ compopt -o nospace
+ ;;
+ *) compopt +o nospace ;; # the default
+ esac
COMPREPLY=($(compgen -W "$(lei _complete ${COMP_WORDS[@]})" \
-- "${COMP_WORDS[COMP_CWORD]}"))
return 0
diff --git a/lib/PublicInbox/LeiExternal.pm b/lib/PublicInbox/LeiExternal.pm
index 6b4c7fb0..accacf1a 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
@@ -165,13 +171,23 @@ sub _complete_forget_external {
map {
my $x = substr($_, length('external.'));
# only return the part specified on the CLI
- if ($x =~ /\A$re(\Q$cur\E.*)/) {
- # don't duplicate if already 100% completed
- $cur eq $1 ? () : $1;
- } else {
- ();
- }
+ # don't duplicate if already 100% completed
+ $x =~ /\A$re(\Q$cur\E.*)/ ? ($cur eq $1 ? () : $1) : ();
} grep(/\Aexternal\.$re\Q$cur/, @{$cfg->{-section_order}});
}
+sub _complete_add_external { # for bash, this relies on "compopt -o nospace"
+ my ($self, @argv) = @_;
+ my $cfg = $self->_lei_cfg(0);
+ my ($cur, $re) = _complete_url_common(\@argv);
+ require URI;
+ map {
+ my $u = URI->new(substr($_, length('external.')));
+ my ($base) = ($u->path =~ m!((?:/?.*)?/)[^/]+/?\z!);
+ $u->path($base);
+ $u = $u->as_string;
+ $u =~ /\A$re(\Q$cur\E.*)/ ? ($cur eq $1 ? () : $1) : ();
+ } grep(m!\Aexternal\.https?://!, @{$cfg->{-section_order}});
+}
+
1;
diff --git a/t/lei.t b/t/lei.t
index 461669a8..03bbb078 100644
--- a/t/lei.t
+++ b/t/lei.t
@@ -233,6 +233,9 @@ my $test_external = sub {
"completed partial URL $u on q $qo");
}
}
+ ok($lei->(qw(_complete lei add-external), 'https://'),
+ 'add-external hostname completion');
+ is($out, "https://example.com/\n", 'completed up to hostname');
$lei->('ls-external');
like($out, qr!https://example\.com/ibx/!s, 'added canonical URL');
^ permalink raw reply related [relevance 5%]
* [PATCH 00/11] lei q --stdin, shortcut names, etc
@ 2021-02-03 8:11 7% Eric Wong
2021-02-03 8:11 5% ` [PATCH 09/11] lei add-external: completion for existing URL basenames Eric Wong
0 siblings, 1 reply; 2+ results
From: Eric Wong @ 2021-02-03 8:11 UTC (permalink / raw)
To: meta
Since externals tend to have common URL or pathname prefixes,
it's now possible to use -I/--only/--exclude with just the
basename of a URL or directory if that's unambiguous.
Wildcard matches are also supported with -I/--only/--exclude.
forget-external still requires the full path, but that's
rarely-used.
add-external bash completion now supports URL hostnames
and common base names.
"lei q" also supports reading queries from stdin.
FD use is slightly reduced, but still far from ideal
(it's bad when I have to bump "ulimit -n" to reattach
screen(1) while I'm running stress tests).
Eric Wong (11):
lei: reduce FD pressure from lei2mail worker
lei: further reduce lei2mail FD pressure
pkt_op: rely on DS::in_loop global
lei: err: avoid uninitialized variable warnings
lei: propagate curl errors, improve internal consistency
lei q: -I/--exclude/--only support globs and basenames
lei: complete basenames for include|exclude|only
lei: help starts pager
lei add-external: completion for existing URL basenames
lei: use sleep(1) loop for infinite sleep
lei q: support reading queries from stdin
MANIFEST | 1 +
contrib/completion/lei-completion.bash | 6 ++
lib/PublicInbox/InputPipe.pm | 37 ++++++++++++
lib/PublicInbox/LEI.pm | 37 +++++++-----
lib/PublicInbox/LeiExternal.pm | 82 +++++++++++++++++++++-----
lib/PublicInbox/LeiOverview.pm | 9 ++-
lib/PublicInbox/LeiQuery.pm | 59 ++++++++++++++----
lib/PublicInbox/LeiToMail.pm | 2 +-
lib/PublicInbox/LeiXSearch.pm | 20 +++----
lib/PublicInbox/PktOp.pm | 25 +++++---
script/lei | 2 +-
t/lei.t | 51 ++++++++++++----
12 files changed, 248 insertions(+), 83 deletions(-)
create mode 100644 lib/PublicInbox/InputPipe.pm
^ permalink raw reply [relevance 7%]
Results 1-2 of 2 | reverse | options above
-- pct% links below jump to the message on this page, permalinks otherwise --
2021-02-03 8:11 7% [PATCH 00/11] lei q --stdin, shortcut names, etc Eric Wong
2021-02-03 8:11 5% ` [PATCH 09/11] lei add-external: completion for existing URL basenames Eric Wong
Code repositories for project(s) associated with this public 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).