Skip to content

Commit ae34501

Browse files
authored
Merge pull request #87 from bashly-framework/fix/double-completion
Fix repeating final completion
2 parents 898962c + e460956 commit ae34501

File tree

16 files changed

+338
-44
lines changed

16 files changed

+338
-44
lines changed

Runfile

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -24,3 +24,9 @@ action :schema do
2424
end
2525
end
2626

27+
help 'Run preconfigured shfmt on any script'
28+
usage 'shfmt SCRIPT'
29+
action :shfmt do |args|
30+
system "shfmt -d -i 2 -ci #{args['SCRIPT']}"
31+
say $?.success? ? 'g`PASS`' : 'r`FAIL`'
32+
end

lib/completely/installer.rb

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -4,8 +4,8 @@ class << self
44
def from_io(program:, io: nil)
55
io ||= $stdin
66

7-
raise InstallError, "io must respond to #read" unless io.respond_to?(:read)
8-
raise InstallError, "io is closed" if io.respond_to?(:closed?) && io.closed?
7+
raise InstallError, 'io must respond to #read' unless io.respond_to?(:read)
8+
raise InstallError, 'io is closed' if io.respond_to?(:closed?) && io.closed?
99

1010
from_string program:, string: io.read
1111
end
@@ -23,7 +23,7 @@ def from_string(program:, string:)
2323
end
2424

2525
def create_tempfile
26-
tempfile = Tempfile.new ["completely-", '.bash']
26+
tempfile = Tempfile.new ['completely-', '.bash']
2727
tempfiles.push tempfile
2828
tempfile
2929
end

lib/completely/templates/template.erb

Lines changed: 25 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -9,24 +9,46 @@
99
local cur=${COMP_WORDS[COMP_CWORD]}
1010
local result=()
1111

12+
# words the user already typed (excluding the command itself)
13+
local used=()
14+
if ((COMP_CWORD > 1)); then
15+
used=("${COMP_WORDS[@]:1:$((COMP_CWORD - 1))}")
16+
fi
17+
1218
if [[ "${cur:0:1}" == "-" ]]; then
19+
# Completing an option: offer everything (including options)
1320
echo "$words"
1421

1522
else
23+
# Completing a non-option: offer only non-options,
24+
# and don't re-offer ones already used earlier in the line.
1625
for word in $words; do
17-
[[ "${word:0:1}" != "-" ]] && result+=("$word")
26+
[[ "${word:0:1}" == "-" ]] && continue
27+
28+
local seen=0
29+
for u in "${used[@]}"; do
30+
if [[ "$u" == "$word" ]]; then
31+
seen=1
32+
break
33+
fi
34+
done
35+
((!seen)) && result+=("$word")
1836
done
1937

2038
echo "${result[*]}"
21-
2239
fi
2340
}
2441

2542
<%= function_name %>() {
2643
local cur=${COMP_WORDS[COMP_CWORD]}
27-
local compwords=("${COMP_WORDS[@]:1:$COMP_CWORD-1}")
44+
local compwords=()
45+
if ((COMP_CWORD > 0)); then
46+
compwords=("${COMP_WORDS[@]:1:$((COMP_CWORD - 1))}")
47+
fi
2848
local compline="${compwords[*]}"
2949

50+
COMPREPLY=()
51+
3052
% if ENV['COMPLETELY_DEBUG']
3153
if [[ -n "$COMPLETELY_DEBUG" ]]; then
3254
echo "compline: '$compline'" > 'completely-debug.txt'

spec/approvals/cli/generated-script

Lines changed: 25 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -9,24 +9,46 @@ _mygit_completions_filter() {
99
local cur=${COMP_WORDS[COMP_CWORD]}
1010
local result=()
1111

12+
# words the user already typed (excluding the command itself)
13+
local used=()
14+
if ((COMP_CWORD > 1)); then
15+
used=("${COMP_WORDS[@]:1:$((COMP_CWORD - 1))}")
16+
fi
17+
1218
if [[ "${cur:0:1}" == "-" ]]; then
19+
# Completing an option: offer everything (including options)
1320
echo "$words"
1421

1522
else
23+
# Completing a non-option: offer only non-options,
24+
# and don't re-offer ones already used earlier in the line.
1625
for word in $words; do
17-
[[ "${word:0:1}" != "-" ]] && result+=("$word")
26+
[[ "${word:0:1}" == "-" ]] && continue
27+
28+
local seen=0
29+
for u in "${used[@]}"; do
30+
if [[ "$u" == "$word" ]]; then
31+
seen=1
32+
break
33+
fi
34+
done
35+
((!seen)) && result+=("$word")
1836
done
1937

2038
echo "${result[*]}"
21-
2239
fi
2340
}
2441

2542
_mygit_completions() {
2643
local cur=${COMP_WORDS[COMP_CWORD]}
27-
local compwords=("${COMP_WORDS[@]:1:$COMP_CWORD-1}")
44+
local compwords=()
45+
if ((COMP_CWORD > 0)); then
46+
compwords=("${COMP_WORDS[@]:1:$((COMP_CWORD - 1))}")
47+
fi
2848
local compline="${compwords[*]}"
2949

50+
COMPREPLY=()
51+
3052
case "$compline" in
3153
'status'*'--branch')
3254
while read -r; do COMPREPLY+=("$REPLY"); done < <(compgen -W "$(_mygit_completions_filter "$(git branch --format='%(refname:short)' 2>/dev/null)")" -- "$cur")

spec/approvals/cli/generated-script-alt

Lines changed: 25 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -9,24 +9,46 @@ _mycomps_filter() {
99
local cur=${COMP_WORDS[COMP_CWORD]}
1010
local result=()
1111

12+
# words the user already typed (excluding the command itself)
13+
local used=()
14+
if ((COMP_CWORD > 1)); then
15+
used=("${COMP_WORDS[@]:1:$((COMP_CWORD - 1))}")
16+
fi
17+
1218
if [[ "${cur:0:1}" == "-" ]]; then
19+
# Completing an option: offer everything (including options)
1320
echo "$words"
1421

1522
else
23+
# Completing a non-option: offer only non-options,
24+
# and don't re-offer ones already used earlier in the line.
1625
for word in $words; do
17-
[[ "${word:0:1}" != "-" ]] && result+=("$word")
26+
[[ "${word:0:1}" == "-" ]] && continue
27+
28+
local seen=0
29+
for u in "${used[@]}"; do
30+
if [[ "$u" == "$word" ]]; then
31+
seen=1
32+
break
33+
fi
34+
done
35+
((!seen)) && result+=("$word")
1836
done
1937

2038
echo "${result[*]}"
21-
2239
fi
2340
}
2441

2542
_mycomps() {
2643
local cur=${COMP_WORDS[COMP_CWORD]}
27-
local compwords=("${COMP_WORDS[@]:1:$COMP_CWORD-1}")
44+
local compwords=()
45+
if ((COMP_CWORD > 0)); then
46+
compwords=("${COMP_WORDS[@]:1:$((COMP_CWORD - 1))}")
47+
fi
2848
local compline="${compwords[*]}"
2949

50+
COMPREPLY=()
51+
3052
case "$compline" in
3153
'status'*'--branch')
3254
while read -r; do COMPREPLY+=("$REPLY"); done < <(compgen -W "$(_mycomps_filter "$(git branch --format='%(refname:short)' 2>/dev/null)")" -- "$cur")

spec/approvals/cli/generated-wrapped-script

Lines changed: 25 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -10,24 +10,46 @@ give_comps() {
1010
echo $' local cur=${COMP_WORDS[COMP_CWORD]}'
1111
echo $' local result=()'
1212
echo $''
13+
echo $' # words the user already typed (excluding the command itself)'
14+
echo $' local used=()'
15+
echo $' if ((COMP_CWORD > 1)); then'
16+
echo $' used=("${COMP_WORDS[@]:1:$((COMP_CWORD - 1))}")'
17+
echo $' fi'
18+
echo $''
1319
echo $' if [[ "${cur:0:1}" == "-" ]]; then'
20+
echo $' # Completing an option: offer everything (including options)'
1421
echo $' echo "$words"'
1522
echo $''
1623
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.'
1726
echo $' for word in $words; do'
18-
echo $' [[ "${word:0:1}" != "-" ]] && result+=("$word")'
27+
echo $' [[ "${word:0:1}" == "-" ]] && continue'
28+
echo $''
29+
echo $' local seen=0'
30+
echo $' for u in "${used[@]}"; do'
31+
echo $' if [[ "$u" == "$word" ]]; then'
32+
echo $' seen=1'
33+
echo $' break'
34+
echo $' fi'
35+
echo $' done'
36+
echo $' ((!seen)) && result+=("$word")'
1937
echo $' done'
2038
echo $''
2139
echo $' echo "${result[*]}"'
22-
echo $''
2340
echo $' fi'
2441
echo $'}'
2542
echo $''
2643
echo $'_mygit_completions() {'
2744
echo $' local cur=${COMP_WORDS[COMP_CWORD]}'
28-
echo $' local compwords=("${COMP_WORDS[@]:1:$COMP_CWORD-1}")'
45+
echo $' local compwords=()'
46+
echo $' if ((COMP_CWORD > 0)); then'
47+
echo $' compwords=("${COMP_WORDS[@]:1:$((COMP_CWORD - 1))}")'
48+
echo $' fi'
2949
echo $' local compline="${compwords[*]}"'
3050
echo $''
51+
echo $' COMPREPLY=()'
52+
echo $''
3153
echo $' case "$compline" in'
3254
echo $' \'status\'*\'--branch\')'
3355
echo $' while read -r; do COMPREPLY+=("$REPLY"); done < <(compgen -W "$(_mygit_completions_filter "$(git branch --format=\'%(refname:short)\' 2>/dev/null)")" -- "$cur")'

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

Lines changed: 25 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -17,24 +17,46 @@ _mygit_completions_filter() {
1717
local cur=${COMP_WORDS[COMP_CWORD]}
1818
local result=()
1919

20+
# words the user already typed (excluding the command itself)
21+
local used=()
22+
if ((COMP_CWORD > 1)); then
23+
used=("${COMP_WORDS[@]:1:$((COMP_CWORD - 1))}")
24+
fi
25+
2026
if [[ "${cur:0:1}" == "-" ]]; then
27+
# Completing an option: offer everything (including options)
2128
echo "$words"
2229

2330
else
31+
# Completing a non-option: offer only non-options,
32+
# and don't re-offer ones already used earlier in the line.
2433
for word in $words; do
25-
[[ "${word:0:1}" != "-" ]] && result+=("$word")
34+
[[ "${word:0:1}" == "-" ]] && continue
35+
36+
local seen=0
37+
for u in "${used[@]}"; do
38+
if [[ "$u" == "$word" ]]; then
39+
seen=1
40+
break
41+
fi
42+
done
43+
((!seen)) && result+=("$word")
2644
done
2745

2846
echo "${result[*]}"
29-
3047
fi
3148
}
3249

3350
_mygit_completions() {
3451
local cur=${COMP_WORDS[COMP_CWORD]}
35-
local compwords=("${COMP_WORDS[@]:1:$COMP_CWORD-1}")
52+
local compwords=()
53+
if ((COMP_CWORD > 0)); then
54+
compwords=("${COMP_WORDS[@]:1:$((COMP_CWORD - 1))}")
55+
fi
3656
local compline="${compwords[*]}"
3757

58+
COMPREPLY=()
59+
3860
case "$compline" in
3961
'status'*'--branch')
4062
while read -r; do COMPREPLY+=("$REPLY"); done < <(compgen -W "$(_mygit_completions_filter "$(git branch --format='%(refname:short)' 2>/dev/null)")" -- "$cur")

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

Lines changed: 25 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -17,24 +17,46 @@ _mygit_completions_filter() {
1717
local cur=${COMP_WORDS[COMP_CWORD]}
1818
local result=()
1919

20+
# words the user already typed (excluding the command itself)
21+
local used=()
22+
if ((COMP_CWORD > 1)); then
23+
used=("${COMP_WORDS[@]:1:$((COMP_CWORD - 1))}")
24+
fi
25+
2026
if [[ "${cur:0:1}" == "-" ]]; then
27+
# Completing an option: offer everything (including options)
2128
echo "$words"
2229

2330
else
31+
# Completing a non-option: offer only non-options,
32+
# and don't re-offer ones already used earlier in the line.
2433
for word in $words; do
25-
[[ "${word:0:1}" != "-" ]] && result+=("$word")
34+
[[ "${word:0:1}" == "-" ]] && continue
35+
36+
local seen=0
37+
for u in "${used[@]}"; do
38+
if [[ "$u" == "$word" ]]; then
39+
seen=1
40+
break
41+
fi
42+
done
43+
((!seen)) && result+=("$word")
2644
done
2745

2846
echo "${result[*]}"
29-
3047
fi
3148
}
3249

3350
_mygit_completions() {
3451
local cur=${COMP_WORDS[COMP_CWORD]}
35-
local compwords=("${COMP_WORDS[@]:1:$COMP_CWORD-1}")
52+
local compwords=()
53+
if ((COMP_CWORD > 0)); then
54+
compwords=("${COMP_WORDS[@]:1:$((COMP_CWORD - 1))}")
55+
fi
3656
local compline="${compwords[*]}"
3757

58+
COMPREPLY=()
59+
3860
case "$compline" in
3961
'status'*'--branch')
4062
while read -r; do COMPREPLY+=("$REPLY"); done < <(compgen -W "$(_mygit_completions_filter "$(git branch --format='%(refname:short)' 2>/dev/null)")" -- "$cur")

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

Lines changed: 25 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -17,24 +17,46 @@ _mygit_completions_filter() {
1717
local cur=${COMP_WORDS[COMP_CWORD]}
1818
local result=()
1919

20+
# words the user already typed (excluding the command itself)
21+
local used=()
22+
if ((COMP_CWORD > 1)); then
23+
used=("${COMP_WORDS[@]:1:$((COMP_CWORD - 1))}")
24+
fi
25+
2026
if [[ "${cur:0:1}" == "-" ]]; then
27+
# Completing an option: offer everything (including options)
2128
echo "$words"
2229

2330
else
31+
# Completing a non-option: offer only non-options,
32+
# and don't re-offer ones already used earlier in the line.
2433
for word in $words; do
25-
[[ "${word:0:1}" != "-" ]] && result+=("$word")
34+
[[ "${word:0:1}" == "-" ]] && continue
35+
36+
local seen=0
37+
for u in "${used[@]}"; do
38+
if [[ "$u" == "$word" ]]; then
39+
seen=1
40+
break
41+
fi
42+
done
43+
((!seen)) && result+=("$word")
2644
done
2745

2846
echo "${result[*]}"
29-
3047
fi
3148
}
3249

3350
_mygit_completions() {
3451
local cur=${COMP_WORDS[COMP_CWORD]}
35-
local compwords=("${COMP_WORDS[@]:1:$COMP_CWORD-1}")
52+
local compwords=()
53+
if ((COMP_CWORD > 0)); then
54+
compwords=("${COMP_WORDS[@]:1:$((COMP_CWORD - 1))}")
55+
fi
3656
local compline="${compwords[*]}"
3757

58+
COMPREPLY=()
59+
3860
case "$compline" in
3961
'status'*'--branch')
4062
while read -r; do COMPREPLY+=("$REPLY"); done < <(compgen -W "$(_mygit_completions_filter "$(git branch --format='%(refname:short)' 2>/dev/null)")" -- "$cur")

0 commit comments

Comments
 (0)