Skip to content

Commit 834ed78

Browse files
committed
- Refactor template filter function
1 parent dce16c3 commit 834ed78

File tree

13 files changed

+143
-234
lines changed

13 files changed

+143
-234
lines changed

lib/completely/templates/template.erb

Lines changed: 11 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -8,41 +8,34 @@
88
local words=("$@")
99
local cur=${COMP_WORDS[COMP_CWORD]}
1010
local result=()
11+
local want_options=0
1112

1213
# words the user already typed (excluding the command itself)
1314
local used=()
1415
if ((COMP_CWORD > 1)); then
1516
used=("${COMP_WORDS[@]:1:$((COMP_CWORD - 1))}")
1617
fi
1718

18-
if [[ "${cur:0:1}" == "-" ]]; then
19-
# Completing an option: offer everything (including options)
20-
result=("${words[@]}")
21-
22-
else
23-
# Completing a non-option: offer only non-options,
24-
# and don't re-offer ones already used earlier in the line.
25-
for word in "${words[@]}"; do
19+
# Completing an option: offer everything.
20+
# Completing a non-option: drop options and already-used words.
21+
[[ "${cur:0:1}" == "-" ]] && want_options=1
22+
for word in "${words[@]}"; do
23+
if ((!want_options)); then
2624
[[ "${word:0:1}" == "-" ]] && continue
2725

28-
local seen=0
2926
for u in "${used[@]}"; do
3027
if [[ "$u" == "$word" ]]; then
31-
seen=1
32-
break
28+
continue 2
3329
fi
3430
done
35-
((!seen)) && result+=("$word")
36-
done
37-
fi
31+
fi
3832

39-
local escaped=()
40-
for word in "${result[@]}"; do
33+
# compgen -W expects shell-escaped words in one space-delimited string.
4134
printf -v word '%q' "$word"
42-
escaped+=("$word")
35+
result+=("$word")
4336
done
4437

45-
echo "${escaped[*]}"
38+
echo "${result[*]}"
4639
}
4740

4841
<%= function_name %>() {

spec/approvals/cli/generated-script

Lines changed: 11 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -8,41 +8,34 @@ _mygit_completions_filter() {
88
local words=("$@")
99
local cur=${COMP_WORDS[COMP_CWORD]}
1010
local result=()
11+
local want_options=0
1112

1213
# words the user already typed (excluding the command itself)
1314
local used=()
1415
if ((COMP_CWORD > 1)); then
1516
used=("${COMP_WORDS[@]:1:$((COMP_CWORD - 1))}")
1617
fi
1718

18-
if [[ "${cur:0:1}" == "-" ]]; then
19-
# Completing an option: offer everything (including options)
20-
result=("${words[@]}")
21-
22-
else
23-
# Completing a non-option: offer only non-options,
24-
# and don't re-offer ones already used earlier in the line.
25-
for word in "${words[@]}"; do
19+
# Completing an option: offer everything.
20+
# Completing a non-option: drop options and already-used words.
21+
[[ "${cur:0:1}" == "-" ]] && want_options=1
22+
for word in "${words[@]}"; do
23+
if ((!want_options)); then
2624
[[ "${word:0:1}" == "-" ]] && continue
2725

28-
local seen=0
2926
for u in "${used[@]}"; do
3027
if [[ "$u" == "$word" ]]; then
31-
seen=1
32-
break
28+
continue 2
3329
fi
3430
done
35-
((!seen)) && result+=("$word")
36-
done
37-
fi
31+
fi
3832

39-
local escaped=()
40-
for word in "${result[@]}"; do
33+
# compgen -W expects shell-escaped words in one space-delimited string.
4134
printf -v word '%q' "$word"
42-
escaped+=("$word")
35+
result+=("$word")
4336
done
4437

45-
echo "${escaped[*]}"
38+
echo "${result[*]}"
4639
}
4740

4841
_mygit_completions() {

spec/approvals/cli/generated-script-alt

Lines changed: 11 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -8,41 +8,34 @@ _mycomps_filter() {
88
local words=("$@")
99
local cur=${COMP_WORDS[COMP_CWORD]}
1010
local result=()
11+
local want_options=0
1112

1213
# words the user already typed (excluding the command itself)
1314
local used=()
1415
if ((COMP_CWORD > 1)); then
1516
used=("${COMP_WORDS[@]:1:$((COMP_CWORD - 1))}")
1617
fi
1718

18-
if [[ "${cur:0:1}" == "-" ]]; then
19-
# Completing an option: offer everything (including options)
20-
result=("${words[@]}")
21-
22-
else
23-
# Completing a non-option: offer only non-options,
24-
# and don't re-offer ones already used earlier in the line.
25-
for word in "${words[@]}"; do
19+
# Completing an option: offer everything.
20+
# Completing a non-option: drop options and already-used words.
21+
[[ "${cur:0:1}" == "-" ]] && want_options=1
22+
for word in "${words[@]}"; do
23+
if ((!want_options)); then
2624
[[ "${word:0:1}" == "-" ]] && continue
2725

28-
local seen=0
2926
for u in "${used[@]}"; do
3027
if [[ "$u" == "$word" ]]; then
31-
seen=1
32-
break
28+
continue 2
3329
fi
3430
done
35-
((!seen)) && result+=("$word")
36-
done
37-
fi
31+
fi
3832

39-
local escaped=()
40-
for word in "${result[@]}"; do
33+
# compgen -W expects shell-escaped words in one space-delimited string.
4134
printf -v word '%q' "$word"
42-
escaped+=("$word")
35+
result+=("$word")
4336
done
4437

45-
echo "${escaped[*]}"
38+
echo "${result[*]}"
4639
}
4740

4841
_mycomps() {

spec/approvals/cli/generated-wrapped-script

Lines changed: 11 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -9,41 +9,34 @@ give_comps() {
99
echo $' local words=("$@")'
1010
echo $' local cur=${COMP_WORDS[COMP_CWORD]}'
1111
echo $' local result=()'
12+
echo $' local want_options=0'
1213
echo $''
1314
echo $' # words the user already typed (excluding the command itself)'
1415
echo $' local used=()'
1516
echo $' if ((COMP_CWORD > 1)); then'
1617
echo $' used=("${COMP_WORDS[@]:1:$((COMP_CWORD - 1))}")'
1718
echo $' fi'
1819
echo $''
19-
echo $' if [[ "${cur:0:1}" == "-" ]]; then'
20-
echo $' # Completing an option: offer everything (including options)'
21-
echo $' result=("${words[@]}")'
22-
echo $''
23-
echo $' else'
24-
echo $' # Completing a non-option: offer only non-options,'
25-
echo $' # and don\'t re-offer ones already used earlier in the line.'
26-
echo $' for word in "${words[@]}"; do'
20+
echo $' # Completing an option: offer everything.'
21+
echo $' # Completing a non-option: drop options and already-used words.'
22+
echo $' [[ "${cur:0:1}" == "-" ]] && want_options=1'
23+
echo $' for word in "${words[@]}"; do'
24+
echo $' if ((!want_options)); then'
2725
echo $' [[ "${word:0:1}" == "-" ]] && continue'
2826
echo $''
29-
echo $' local seen=0'
3027
echo $' for u in "${used[@]}"; do'
3128
echo $' if [[ "$u" == "$word" ]]; then'
32-
echo $' seen=1'
33-
echo $' break'
29+
echo $' continue 2'
3430
echo $' fi'
3531
echo $' done'
36-
echo $' ((!seen)) && result+=("$word")'
37-
echo $' done'
38-
echo $' fi'
32+
echo $' fi'
3933
echo $''
40-
echo $' local escaped=()'
41-
echo $' for word in "${result[@]}"; do'
34+
echo $' # compgen -W expects shell-escaped words in one space-delimited string.'
4235
echo $' printf -v word \'%q\' "$word"'
43-
echo $' escaped+=("$word")'
36+
echo $' result+=("$word")'
4437
echo $' done'
4538
echo $''
46-
echo $' echo "${escaped[*]}"'
39+
echo $' echo "${result[*]}"'
4740
echo $'}'
4841
echo $''
4942
echo $'_mygit_completions() {'

spec/approvals/cli/test/completely-tester-1.sh

Lines changed: 11 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -16,41 +16,34 @@ _mygit_completions_filter() {
1616
local words=("$@")
1717
local cur=${COMP_WORDS[COMP_CWORD]}
1818
local result=()
19+
local want_options=0
1920

2021
# words the user already typed (excluding the command itself)
2122
local used=()
2223
if ((COMP_CWORD > 1)); then
2324
used=("${COMP_WORDS[@]:1:$((COMP_CWORD - 1))}")
2425
fi
2526

26-
if [[ "${cur:0:1}" == "-" ]]; then
27-
# Completing an option: offer everything (including options)
28-
result=("${words[@]}")
29-
30-
else
31-
# Completing a non-option: offer only non-options,
32-
# and don't re-offer ones already used earlier in the line.
33-
for word in "${words[@]}"; do
27+
# Completing an option: offer everything.
28+
# Completing a non-option: drop options and already-used words.
29+
[[ "${cur:0:1}" == "-" ]] && want_options=1
30+
for word in "${words[@]}"; do
31+
if ((!want_options)); then
3432
[[ "${word:0:1}" == "-" ]] && continue
3533

36-
local seen=0
3734
for u in "${used[@]}"; do
3835
if [[ "$u" == "$word" ]]; then
39-
seen=1
40-
break
36+
continue 2
4137
fi
4238
done
43-
((!seen)) && result+=("$word")
44-
done
45-
fi
39+
fi
4640

47-
local escaped=()
48-
for word in "${result[@]}"; do
41+
# compgen -W expects shell-escaped words in one space-delimited string.
4942
printf -v word '%q' "$word"
50-
escaped+=("$word")
43+
result+=("$word")
5144
done
5245

53-
echo "${escaped[*]}"
46+
echo "${result[*]}"
5447
}
5548

5649
_mygit_completions() {

spec/approvals/cli/test/completely-tester-2.sh

Lines changed: 11 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -16,41 +16,34 @@ _mygit_completions_filter() {
1616
local words=("$@")
1717
local cur=${COMP_WORDS[COMP_CWORD]}
1818
local result=()
19+
local want_options=0
1920

2021
# words the user already typed (excluding the command itself)
2122
local used=()
2223
if ((COMP_CWORD > 1)); then
2324
used=("${COMP_WORDS[@]:1:$((COMP_CWORD - 1))}")
2425
fi
2526

26-
if [[ "${cur:0:1}" == "-" ]]; then
27-
# Completing an option: offer everything (including options)
28-
result=("${words[@]}")
29-
30-
else
31-
# Completing a non-option: offer only non-options,
32-
# and don't re-offer ones already used earlier in the line.
33-
for word in "${words[@]}"; do
27+
# Completing an option: offer everything.
28+
# Completing a non-option: drop options and already-used words.
29+
[[ "${cur:0:1}" == "-" ]] && want_options=1
30+
for word in "${words[@]}"; do
31+
if ((!want_options)); then
3432
[[ "${word:0:1}" == "-" ]] && continue
3533

36-
local seen=0
3734
for u in "${used[@]}"; do
3835
if [[ "$u" == "$word" ]]; then
39-
seen=1
40-
break
36+
continue 2
4137
fi
4238
done
43-
((!seen)) && result+=("$word")
44-
done
45-
fi
39+
fi
4640

47-
local escaped=()
48-
for word in "${result[@]}"; do
41+
# compgen -W expects shell-escaped words in one space-delimited string.
4942
printf -v word '%q' "$word"
50-
escaped+=("$word")
43+
result+=("$word")
5144
done
5245

53-
echo "${escaped[*]}"
46+
echo "${result[*]}"
5447
}
5548

5649
_mygit_completions() {

spec/approvals/cli/test/completely-tester.sh

Lines changed: 11 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -16,41 +16,34 @@ _mygit_completions_filter() {
1616
local words=("$@")
1717
local cur=${COMP_WORDS[COMP_CWORD]}
1818
local result=()
19+
local want_options=0
1920

2021
# words the user already typed (excluding the command itself)
2122
local used=()
2223
if ((COMP_CWORD > 1)); then
2324
used=("${COMP_WORDS[@]:1:$((COMP_CWORD - 1))}")
2425
fi
2526

26-
if [[ "${cur:0:1}" == "-" ]]; then
27-
# Completing an option: offer everything (including options)
28-
result=("${words[@]}")
29-
30-
else
31-
# Completing a non-option: offer only non-options,
32-
# and don't re-offer ones already used earlier in the line.
33-
for word in "${words[@]}"; do
27+
# Completing an option: offer everything.
28+
# Completing a non-option: drop options and already-used words.
29+
[[ "${cur:0:1}" == "-" ]] && want_options=1
30+
for word in "${words[@]}"; do
31+
if ((!want_options)); then
3432
[[ "${word:0:1}" == "-" ]] && continue
3533

36-
local seen=0
3734
for u in "${used[@]}"; do
3835
if [[ "$u" == "$word" ]]; then
39-
seen=1
40-
break
36+
continue 2
4137
fi
4238
done
43-
((!seen)) && result+=("$word")
44-
done
45-
fi
39+
fi
4640

47-
local escaped=()
48-
for word in "${result[@]}"; do
41+
# compgen -W expects shell-escaped words in one space-delimited string.
4942
printf -v word '%q' "$word"
50-
escaped+=("$word")
43+
result+=("$word")
5144
done
5245

53-
echo "${escaped[*]}"
46+
echo "${result[*]}"
5447
}
5548

5649
_mygit_completions() {

0 commit comments

Comments
 (0)