Skip to content

Commit 0209a79

Browse files
committed
fix(complete): Don't cause endless completions for bash/zsh
Reported on #5677
1 parent d811585 commit 0209a79

File tree

7 files changed

+92
-48
lines changed

7 files changed

+92
-48
lines changed

clap_complete/src/command/shells.rs

Lines changed: 20 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -187,15 +187,21 @@ impl CommandCompleter for Bash {
187187

188188
let script = r#"
189189
_clap_complete_NAME() {
190-
export IFS=$'\013'
191-
export _CLAP_COMPLETE_INDEX=${COMP_CWORD}
192-
export _CLAP_COMPLETE_COMP_TYPE=${COMP_TYPE}
190+
local IFS=$'\013'
191+
local _CLAP_COMPLETE_INDEX=${COMP_CWORD}
192+
local _CLAP_COMPLETE_COMP_TYPE=${COMP_TYPE}
193193
if compopt +o nospace 2> /dev/null; then
194-
export _CLAP_COMPLETE_SPACE=false
194+
local _CLAP_COMPLETE_SPACE=false
195195
else
196-
export _CLAP_COMPLETE_SPACE=true
196+
local _CLAP_COMPLETE_SPACE=true
197197
fi
198-
COMPREPLY=( $("COMPLETER" complete bash -- "${COMP_WORDS[@]}") )
198+
COMPREPLY=( $( \
199+
IFS="$IFS" \
200+
_CLAP_COMPLETE_INDEX="$_CLAP_COMPLETE_INDEX" \
201+
_CLAP_COMPLETE_COMP_TYPE="$_CLAP_COMPLETE_COMP_TYPE" \
202+
_CLAP_COMPLETE_SPACE="$_CLAP_COMPLETE_SPACE" \
203+
"COMPLETER" complete bash -- "${COMP_WORDS[@]}" \
204+
) )
199205
if [[ $? != 0 ]]; then
200206
unset COMPREPLY
201207
elif [[ $SUPPRESS_SPACE == 1 ]] && [[ "${COMPREPLY-}" =~ [=/:]$ ]]; then
@@ -471,10 +477,14 @@ impl CommandCompleter for Zsh {
471477

472478
let script = r#"#compdef BIN
473479
function _clap_dynamic_completer() {
474-
export _CLAP_COMPLETE_INDEX=$(expr $CURRENT - 1)
475-
export _CLAP_IFS=$'\n'
476-
477-
local completions=("${(@f)$(COMPLETER complete zsh -- ${words} 2>/dev/null)}")
480+
local _CLAP_COMPLETE_INDEX=$(expr $CURRENT - 1)
481+
local _CLAP_IFS=$'\n'
482+
483+
local completions=("${(@f)$( \
484+
_CLAP_IFS="$_CLAP_IFS" \
485+
_CLAP_COMPLETE_INDEX="$_CLAP_COMPLETE_INDEX" \
486+
COMPLETER complete zsh -- ${words} 2>/dev/null \
487+
)}")
478488
479489
if [[ -n $completions ]]; then
480490
compadd -a completions

clap_complete/src/env/shells.rs

Lines changed: 21 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -31,16 +31,21 @@ impl EnvCompleter for Bash {
3131

3232
let script = r#"
3333
_clap_complete_NAME() {
34-
export IFS=$'\013'
35-
export _CLAP_COMPLETE_INDEX=${COMP_CWORD}
36-
export _CLAP_COMPLETE_COMP_TYPE=${COMP_TYPE}
34+
local IFS=$'\013'
35+
local _CLAP_COMPLETE_INDEX=${COMP_CWORD}
36+
local _CLAP_COMPLETE_COMP_TYPE=${COMP_TYPE}
3737
if compopt +o nospace 2> /dev/null; then
38-
export _CLAP_COMPLETE_SPACE=false
38+
local _CLAP_COMPLETE_SPACE=false
3939
else
40-
export _CLAP_COMPLETE_SPACE=true
40+
local _CLAP_COMPLETE_SPACE=true
4141
fi
42-
export VAR="bash"
43-
COMPREPLY=( $("COMPLETER" -- "${COMP_WORDS[@]}") )
42+
COMPREPLY=( $( \
43+
IFS="$IFS" \
44+
_CLAP_COMPLETE_INDEX="$_CLAP_COMPLETE_INDEX" \
45+
_CLAP_COMPLETE_COMP_TYPE="$_CLAP_COMPLETE_COMP_TYPE" \
46+
VAR="bash" \
47+
"COMPLETER" -- "${COMP_WORDS[@]}" \
48+
) )
4449
if [[ $? != 0 ]]; then
4550
unset COMPREPLY
4651
elif [[ $SUPPRESS_SPACE == 1 ]] && [[ "${COMPREPLY-}" =~ [=/:]$ ]]; then
@@ -347,11 +352,15 @@ impl EnvCompleter for Zsh {
347352

348353
let script = r#"#compdef BIN
349354
function _clap_dynamic_completer() {
350-
export _CLAP_COMPLETE_INDEX=$(expr $CURRENT - 1)
351-
export _CLAP_IFS=$'\n'
352-
export VAR="zsh"
353-
354-
local completions=("${(@f)$(COMPLETER -- ${words} 2>/dev/null)}")
355+
local _CLAP_COMPLETE_INDEX=$(expr $CURRENT - 1)
356+
local _CLAP_IFS=$'\n'
357+
358+
local completions=("${(@f)$( \
359+
_CLAP_IFS="$_CLAP_IFS" \
360+
_CLAP_COMPLETE_INDEX="$_CLAP_COMPLETE_INDEX" \
361+
VAR="zsh" \
362+
COMPLETER -- ${words} 2>/dev/null \
363+
)}")
355364
356365
if [[ -n $completions ]]; then
357366
compadd -a completions

clap_complete/tests/snapshots/home/dynamic-command/exhaustive/bash/.bashrc

Lines changed: 12 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -2,15 +2,21 @@ PS1='% '
22
. /etc/bash_completion
33

44
_clap_complete_exhaustive() {
5-
export IFS=$'\013'
6-
export _CLAP_COMPLETE_INDEX=${COMP_CWORD}
7-
export _CLAP_COMPLETE_COMP_TYPE=${COMP_TYPE}
5+
local IFS=$'\013'
6+
local _CLAP_COMPLETE_INDEX=${COMP_CWORD}
7+
local _CLAP_COMPLETE_COMP_TYPE=${COMP_TYPE}
88
if compopt +o nospace 2> /dev/null; then
9-
export _CLAP_COMPLETE_SPACE=false
9+
local _CLAP_COMPLETE_SPACE=false
1010
else
11-
export _CLAP_COMPLETE_SPACE=true
11+
local _CLAP_COMPLETE_SPACE=true
1212
fi
13-
COMPREPLY=( $("exhaustive" complete bash -- "${COMP_WORDS[@]}") )
13+
COMPREPLY=( $( \
14+
IFS="$IFS" \
15+
_CLAP_COMPLETE_INDEX="$_CLAP_COMPLETE_INDEX" \
16+
_CLAP_COMPLETE_COMP_TYPE="$_CLAP_COMPLETE_COMP_TYPE" \
17+
_CLAP_COMPLETE_SPACE="$_CLAP_COMPLETE_SPACE" \
18+
"exhaustive" complete bash -- "${COMP_WORDS[@]}" \
19+
) )
1420
if [[ $? != 0 ]]; then
1521
unset COMPREPLY
1622
elif [[ $SUPPRESS_SPACE == 1 ]] && [[ "${COMPREPLY-}" =~ [=/:]$ ]]; then

clap_complete/tests/snapshots/home/dynamic-command/exhaustive/zsh/zsh/_exhaustive

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,13 @@
11
#compdef exhaustive
22
function _clap_dynamic_completer() {
3-
export _CLAP_COMPLETE_INDEX=$(expr $CURRENT - 1)
4-
export _CLAP_IFS=$'\n'
3+
local _CLAP_COMPLETE_INDEX=$(expr $CURRENT - 1)
4+
local _CLAP_IFS=$'\n'
55

6-
local completions=("${(@f)$(exhaustive complete zsh -- ${words} 2>/dev/null)}")
6+
local completions=("${(@f)$( \
7+
_CLAP_IFS="$_CLAP_IFS" \
8+
_CLAP_COMPLETE_INDEX="$_CLAP_COMPLETE_INDEX" \
9+
exhaustive complete zsh -- ${words} 2>/dev/null \
10+
)}")
711

812
if [[ -n $completions ]]; then
913
compadd -a completions

clap_complete/tests/snapshots/home/dynamic-env/exhaustive/bash/.bashrc

Lines changed: 12 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -2,16 +2,21 @@ PS1='% '
22
. /etc/bash_completion
33

44
_clap_complete_exhaustive() {
5-
export IFS=$'\013'
6-
export _CLAP_COMPLETE_INDEX=${COMP_CWORD}
7-
export _CLAP_COMPLETE_COMP_TYPE=${COMP_TYPE}
5+
local IFS=$'\013'
6+
local _CLAP_COMPLETE_INDEX=${COMP_CWORD}
7+
local _CLAP_COMPLETE_COMP_TYPE=${COMP_TYPE}
88
if compopt +o nospace 2> /dev/null; then
9-
export _CLAP_COMPLETE_SPACE=false
9+
local _CLAP_COMPLETE_SPACE=false
1010
else
11-
export _CLAP_COMPLETE_SPACE=true
11+
local _CLAP_COMPLETE_SPACE=true
1212
fi
13-
export COMPLETE="bash"
14-
COMPREPLY=( $("exhaustive" -- "${COMP_WORDS[@]}") )
13+
COMPREPLY=( $( \
14+
IFS="$IFS" \
15+
_CLAP_COMPLETE_INDEX="$_CLAP_COMPLETE_INDEX" \
16+
_CLAP_COMPLETE_COMP_TYPE="$_CLAP_COMPLETE_COMP_TYPE" \
17+
COMPLETE="bash" \
18+
"exhaustive" -- "${COMP_WORDS[@]}" \
19+
) )
1520
if [[ $? != 0 ]]; then
1621
unset COMPREPLY
1722
elif [[ $SUPPRESS_SPACE == 1 ]] && [[ "${COMPREPLY-}" =~ [=/:]$ ]]; then

clap_complete/tests/snapshots/home/dynamic-env/exhaustive/zsh/zsh/_exhaustive

Lines changed: 8 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,14 @@
11
#compdef exhaustive
22
function _clap_dynamic_completer() {
3-
export _CLAP_COMPLETE_INDEX=$(expr $CURRENT - 1)
4-
export _CLAP_IFS=$'\n'
5-
export COMPLETE="zsh"
3+
local _CLAP_COMPLETE_INDEX=$(expr $CURRENT - 1)
4+
local _CLAP_IFS=$'\n'
65

7-
local completions=("${(@f)$(exhaustive -- ${words} 2>/dev/null)}")
6+
local completions=("${(@f)$( \
7+
_CLAP_IFS="$_CLAP_IFS" \
8+
_CLAP_COMPLETE_INDEX="$_CLAP_COMPLETE_INDEX" \
9+
COMPLETE="zsh" \
10+
exhaustive -- ${words} 2>/dev/null \
11+
)}")
812

913
if [[ -n $completions ]]; then
1014
compadd -a completions

clap_complete/tests/snapshots/register_minimal.bash

Lines changed: 12 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,20 @@
11

22
_clap_complete_my_app() {
3-
export IFS=$'/013'
4-
export _CLAP_COMPLETE_INDEX=${COMP_CWORD}
5-
export _CLAP_COMPLETE_COMP_TYPE=${COMP_TYPE}
3+
local IFS=$'/013'
4+
local _CLAP_COMPLETE_INDEX=${COMP_CWORD}
5+
local _CLAP_COMPLETE_COMP_TYPE=${COMP_TYPE}
66
if compopt +o nospace 2> /dev/null; then
7-
export _CLAP_COMPLETE_SPACE=false
7+
local _CLAP_COMPLETE_SPACE=false
88
else
9-
export _CLAP_COMPLETE_SPACE=true
9+
local _CLAP_COMPLETE_SPACE=true
1010
fi
11-
COMPREPLY=( $("my-app" complete bash -- "${COMP_WORDS[@]}") )
11+
COMPREPLY=( $( /
12+
IFS="$IFS" /
13+
_CLAP_COMPLETE_INDEX="$_CLAP_COMPLETE_INDEX" /
14+
_CLAP_COMPLETE_COMP_TYPE="$_CLAP_COMPLETE_COMP_TYPE" /
15+
_CLAP_COMPLETE_SPACE="$_CLAP_COMPLETE_SPACE" /
16+
"my-app" complete bash -- "${COMP_WORDS[@]}" /
17+
) )
1218
if [[ $? != 0 ]]; then
1319
unset COMPREPLY
1420
elif [[ $SUPPRESS_SPACE == 1 ]] && [[ "${COMPREPLY-}" =~ [=/:]$ ]]; then

0 commit comments

Comments
 (0)
pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy