Skip to content

Commit 50845cb

Browse files
authored
Fix bash completion failure when being sourced/used together with nim's one (#1352)
1 parent b9430b5 commit 50845cb

File tree

1 file changed

+55
-60
lines changed

1 file changed

+55
-60
lines changed

nimble.bash-completion

+55-60
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,9 @@
11
# bash completion for nim -*- shell-script -*-
22

3+
# (NOTE) The following functions SHOULD be identical to those
4+
# in nim source tree: __is_short_or_long, __ask_for_subcmd_or_subopts,
5+
# __ask_for_subcmd, __ask_for_subcmd_opts
6+
37
__is_short_or_long()
48
{
59
local actual short long
@@ -11,82 +15,48 @@ __is_short_or_long()
1115
return 1
1216
}
1317

14-
__ask_for_subcmd()
18+
__ask_for_subcmd_or_subopts()
1519
{
16-
local args cmd words len ilast ilastlast word_first word_last sub_words sub_len
17-
local i ele
18-
args=("$@")
19-
cmd="${args[0]}"
20-
ilast="${args[1]}"
21-
words=("${args[@]:2}")
22-
len=${#words[@]}
23-
ilastlast=$((ilast - 1))
24-
word_first=${words[0]}
25-
word_last=${words[ilast]}
26-
sub_words=("${words[@]:1:ilastlast}") # start at 1 hence lastlast
27-
sub_len=${#words[@]}
28-
29-
# printf "\n[DBUG] word_first:${word_first}|word_last:${word_last}|sub_words(${#sub_words[*]}):${sub_words[*]}\n"
30-
31-
if [[ $word_first != $cmd || $word_last =~ ^- ]]
32-
then
33-
return 1
34-
fi
20+
local args cmd subcmd words sub_words word_first word_last word_lastlast
21+
local len ilast ilastlast i ele sub_len n_nopts
3522

36-
i=0
37-
while [[ $i -lt $sub_len ]]
38-
do
39-
ele=${sub_words[i]}
40-
if [[ ! -z $ele && ! $ele =~ ^[-:] ]]
41-
then
42-
return 1
43-
fi
44-
if [[ $ele =~ ^: ]]
45-
then
46-
((i+=1))
47-
fi
48-
((i+=1))
49-
done
50-
51-
if [[ $word_last == : ]]
52-
then
53-
return 1
54-
fi
55-
56-
return 0
57-
}
58-
59-
__ask_for_subcmd_opts()
60-
{
61-
local args cmd subcmd words len ilast sub_words sub_len word_first n_nopts
62-
local i ele
6323
args=("$@")
64-
cmd="${args[0]}"
65-
subcmd="${args[1]}"
66-
ilast="${args[2]}"
67-
words=("${args[@]:3}")
24+
ask_for_what="${args[0]}"
25+
cmd="${args[1]}"
26+
subcmd="${args[2]}"
27+
ilast="${args[3]}"
28+
words=("${args[@]:4}")
6829
len=${#words[@]}
30+
ilastlast=$((ilast - 1))
6931
sub_words=("${words[@]:0:ilast}")
7032
sub_len=${#sub_words[@]}
7133
word_first=${words[0]}
34+
word_last=${words[ilast]}
35+
word_lastlast=${words[ilastlast]}
7236
n_nopts=0
7337

74-
# printf "\n[DBUG] word_first:${word_first}|words(${len}):${words[*]}|sub_words(${sub_len}):${sub_words[*]}\n"
38+
# printf "\n[DBUG] word_first:${word_first}|ilast:${ilast}|words(${len}):${words[*]}|sub_words(${sub_len}):${sub_words[*]}\n"
7539

7640
if [[ $word_first != $cmd ]]
7741
then
7842
return 1
7943
fi
8044

8145
i=0
82-
while [[ $i -lt $sub_len ]]
46+
while [[ $i -lt $len ]]
8347
do
84-
ele=${sub_words[i]}
48+
ele=${words[i]}
8549
if [[ ! $ele =~ ^- ]]
8650
then
8751
if [[ $ele == $cmd || $ele == $subcmd ]]
8852
then
8953
((n_nopts+=1))
54+
elif [[ $i -eq $ilast && $ele =~ ^[a-zA-Z] ]]
55+
then
56+
((i=i))
57+
elif [[ -z $ele ]]
58+
then
59+
((i=i))
9060
elif [[ $ele =~ ^: ]]
9161
then
9262
((i+=1))
@@ -97,12 +67,37 @@ __ask_for_subcmd_opts()
9767
((i+=1))
9868
done
9969

100-
if [[ n_nopts -ne 2 ]]
101-
then
102-
return 1
103-
fi
70+
case $ask_for_what in
71+
1)
72+
if [[ n_nopts -eq 1 ]]
73+
then
74+
if [[ -z $word_last || $word_last =~ ^[a-zA-Z] ]] && [[ $word_lastlast != : ]]
75+
then
76+
return 0
77+
fi
78+
fi
79+
;;
80+
2)
81+
if [[ n_nopts -eq 2 ]]
82+
then
83+
if [[ -z $word_last ]] || [[ $word_last =~ ^[-:] ]]
84+
then
85+
return 0
86+
fi
87+
fi
88+
esac
10489

105-
return 0
90+
return 1
91+
}
92+
93+
__ask_for_subcmd()
94+
{
95+
__ask_for_subcmd_or_subopts 1 "$@"
96+
}
97+
98+
__ask_for_subcmd_opts()
99+
{
100+
__ask_for_subcmd_or_subopts 2 "$@"
106101
}
107102

108103
_nimble()
@@ -122,7 +117,7 @@ _nimble()
122117
# printf "\n[DBUG] i_curr:$i_curr|curr:$curr|prev:$prev|words(${#words[*]}):${words[*]}\n"
123118

124119
# Asking for a subcommand
125-
if __ask_for_subcmd nimble $i_curr "${words[@]}"
120+
if __ask_for_subcmd nimble nimble $i_curr "${words[@]}"
126121
then
127122
subcmds=""
128123
subcmds="${subcmds} install"

0 commit comments

Comments
 (0)