Skip to content

Commit bfb1017

Browse files
authored
Merge pull request #35 from DannyBen/change/hide-flags
Hide flag completion unless input ends with a hyphen
2 parents 38d644d + 6491106 commit bfb1017

File tree

16 files changed

+244
-44
lines changed

16 files changed

+244
-44
lines changed

README.md

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -97,6 +97,9 @@ Each pattern in this configuration file will be checked against the user's
9797
input, and if the input **starts with** a matching pattern, the list that
9898
follows it will be suggested as completions.
9999
100+
Note that the suggested completions will not show flags (string that start with
101+
a hyphen `-`) unless the input ends with a hyphen.
102+
100103
To generate the bash script, simply run:
101104

102105
```bash

lib/completely/completions.rb

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -54,7 +54,7 @@ def tester
5454

5555
def patterns!
5656
config.map do |text, completions|
57-
Pattern.new text, completions
57+
Pattern.new text, completions, pattern_function_name
5858
end.sort_by { |pattern| -pattern.length }
5959
end
6060

@@ -74,6 +74,10 @@ def function_name
7474
@function_name ||= "_#{command}_completions"
7575
end
7676

77+
def pattern_function_name
78+
@pattern_function_name ||= "#{function_name}_filter"
79+
end
80+
7781
def pattern_prefixes
7882
patterns.map &:prefix
7983
end

lib/completely/pattern.rb

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,11 @@
11
module Completely
22
class Pattern
3-
attr_reader :text, :completions
3+
attr_reader :text, :completions, :function_name
44

5-
def initialize(text, completions)
5+
def initialize(text, completions, function_name)
66
@text = text
77
@completions = completions || []
8+
@function_name = function_name
89
end
910

1011
def length
@@ -53,7 +54,7 @@ def compgen
5354
def compgen!
5455
result = []
5556
result << %Q[#{actions.join ' '}] if actions.any?
56-
result << %Q[-W "#{words.join ' '}"] if words.any?
57+
result << %Q[-W "$(#{function_name} "#{words.join ' '}")"] if words.any?
5758
result.any? ? result.join(' ') : nil
5859
end
5960
end

lib/completely/templates/template.erb

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,24 @@
44
# completely (https://github.com/dannyben/completely)
55
# Modifying it manually is not recommended
66

7+
<%= function_name %>_filter() {
8+
local words="$1"
9+
local cur=${COMP_WORDS[COMP_CWORD]}
10+
local result=()
11+
12+
if [[ "${cur:0:1}" == "-" ]]; then
13+
echo "$words"
14+
15+
else
16+
for word in $words; do
17+
[[ "${word:0:1}" != "-" ]] && result+=("$word")
18+
done
19+
20+
echo "${result[*]}"
21+
22+
fi
23+
}
24+
725
<%= function_name %>() {
826
local cur=${COMP_WORDS[COMP_CWORD]}
927
local compwords=("${COMP_WORDS[@]:1:$COMP_CWORD-1}")

spec/approvals/cli/generated-script

Lines changed: 22 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -4,26 +4,44 @@
44
# completely (https://github.com/dannyben/completely)
55
# Modifying it manually is not recommended
66

7+
_mygit_completions_filter() {
8+
local words="$1"
9+
local cur=${COMP_WORDS[COMP_CWORD]}
10+
local result=()
11+
12+
if [[ "${cur:0:1}" == "-" ]]; then
13+
echo "$words"
14+
15+
else
16+
for word in $words; do
17+
[[ "${word:0:1}" != "-" ]] && result+=("$word")
18+
done
19+
20+
echo "${result[*]}"
21+
22+
fi
23+
}
24+
725
_mygit_completions() {
826
local cur=${COMP_WORDS[COMP_CWORD]}
927
local compwords=("${COMP_WORDS[@]:1:$COMP_CWORD-1}")
1028
local compline="${compwords[*]}"
1129

1230
case "$compline" in
1331
'status'*)
14-
while read -r; do COMPREPLY+=( "$REPLY" ); done < <( compgen -W "--help --verbose --branch $(git branch 2> /dev/null)" -- "$cur" )
32+
while read -r; do COMPREPLY+=( "$REPLY" ); done < <( compgen -W "$(_mygit_completions_filter "--help --verbose --branch $(git branch 2> /dev/null)")" -- "$cur" )
1533
;;
1634

1735
'commit'*)
18-
while read -r; do COMPREPLY+=( "$REPLY" ); done < <( compgen -A file -W "--help --message --all -a --quiet -q" -- "$cur" )
36+
while read -r; do COMPREPLY+=( "$REPLY" ); done < <( compgen -A file -W "$(_mygit_completions_filter "--help --message --all -a --quiet -q")" -- "$cur" )
1937
;;
2038

2139
'init'*)
22-
while read -r; do COMPREPLY+=( "$REPLY" ); done < <( compgen -A directory -W "--bare" -- "$cur" )
40+
while read -r; do COMPREPLY+=( "$REPLY" ); done < <( compgen -A directory -W "$(_mygit_completions_filter "--bare")" -- "$cur" )
2341
;;
2442

2543
*)
26-
while read -r; do COMPREPLY+=( "$REPLY" ); done < <( compgen -W "--help --version status init commit" -- "$cur" )
44+
while read -r; do COMPREPLY+=( "$REPLY" ); done < <( compgen -W "$(_mygit_completions_filter "--help --version status init commit")" -- "$cur" )
2745
;;
2846

2947
esac

spec/approvals/cli/generated-script-alt

Lines changed: 22 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -4,26 +4,44 @@
44
# completely (https://github.com/dannyben/completely)
55
# Modifying it manually is not recommended
66

7+
_mycomps_filter() {
8+
local words="$1"
9+
local cur=${COMP_WORDS[COMP_CWORD]}
10+
local result=()
11+
12+
if [[ "${cur:0:1}" == "-" ]]; then
13+
echo "$words"
14+
15+
else
16+
for word in $words; do
17+
[[ "${word:0:1}" != "-" ]] && result+=("$word")
18+
done
19+
20+
echo "${result[*]}"
21+
22+
fi
23+
}
24+
725
_mycomps() {
826
local cur=${COMP_WORDS[COMP_CWORD]}
927
local compwords=("${COMP_WORDS[@]:1:$COMP_CWORD-1}")
1028
local compline="${compwords[*]}"
1129

1230
case "$compline" in
1331
'status'*)
14-
while read -r; do COMPREPLY+=( "$REPLY" ); done < <( compgen -W "--help --verbose --branch $(git branch 2> /dev/null)" -- "$cur" )
32+
while read -r; do COMPREPLY+=( "$REPLY" ); done < <( compgen -W "$(_mycomps_filter "--help --verbose --branch $(git branch 2> /dev/null)")" -- "$cur" )
1533
;;
1634

1735
'commit'*)
18-
while read -r; do COMPREPLY+=( "$REPLY" ); done < <( compgen -A file -W "--help --message --all -a --quiet -q" -- "$cur" )
36+
while read -r; do COMPREPLY+=( "$REPLY" ); done < <( compgen -A file -W "$(_mycomps_filter "--help --message --all -a --quiet -q")" -- "$cur" )
1937
;;
2038

2139
'init'*)
22-
while read -r; do COMPREPLY+=( "$REPLY" ); done < <( compgen -A directory -W "--bare" -- "$cur" )
40+
while read -r; do COMPREPLY+=( "$REPLY" ); done < <( compgen -A directory -W "$(_mycomps_filter "--bare")" -- "$cur" )
2341
;;
2442

2543
*)
26-
while read -r; do COMPREPLY+=( "$REPLY" ); done < <( compgen -W "--help --version status init commit" -- "$cur" )
44+
while read -r; do COMPREPLY+=( "$REPLY" ); done < <( compgen -W "$(_mycomps_filter "--help --version status init commit")" -- "$cur" )
2745
;;
2846

2947
esac

spec/approvals/cli/generated-wrapped-script

Lines changed: 22 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -5,26 +5,44 @@ give_comps() {
55
echo $'# completely (https://github.com/dannyben/completely)'
66
echo $'# Modifying it manually is not recommended'
77
echo $''
8+
echo $'_mygit_completions_filter() {'
9+
echo $' local words="$1"'
10+
echo $' local cur=${COMP_WORDS[COMP_CWORD]}'
11+
echo $' local result=()'
12+
echo $''
13+
echo $' if [[ "${cur:0:1}" == "-" ]]; then'
14+
echo $' echo "$words"'
15+
echo $' '
16+
echo $' else'
17+
echo $' for word in $words; do'
18+
echo $' [[ "${word:0:1}" != "-" ]] && result+=("$word")'
19+
echo $' done'
20+
echo $''
21+
echo $' echo "${result[*]}"'
22+
echo $''
23+
echo $' fi'
24+
echo $'}'
25+
echo $''
826
echo $'_mygit_completions() {'
927
echo $' local cur=${COMP_WORDS[COMP_CWORD]}'
1028
echo $' local compwords=("${COMP_WORDS[@]:1:$COMP_CWORD-1}")'
1129
echo $' local compline="${compwords[*]}"'
1230
echo $''
1331
echo $' case "$compline" in'
1432
echo $' \'status\'*)'
15-
echo $' while read -r; do COMPREPLY+=( "$REPLY" ); done < <( compgen -W "--help --verbose --branch $(git branch 2> /dev/null)" -- "$cur" )'
33+
echo $' while read -r; do COMPREPLY+=( "$REPLY" ); done < <( compgen -W "$(_mygit_completions_filter "--help --verbose --branch $(git branch 2> /dev/null)")" -- "$cur" )'
1634
echo $' ;;'
1735
echo $''
1836
echo $' \'commit\'*)'
19-
echo $' while read -r; do COMPREPLY+=( "$REPLY" ); done < <( compgen -A file -W "--help --message --all -a --quiet -q" -- "$cur" )'
37+
echo $' while read -r; do COMPREPLY+=( "$REPLY" ); done < <( compgen -A file -W "$(_mygit_completions_filter "--help --message --all -a --quiet -q")" -- "$cur" )'
2038
echo $' ;;'
2139
echo $''
2240
echo $' \'init\'*)'
23-
echo $' while read -r; do COMPREPLY+=( "$REPLY" ); done < <( compgen -A directory -W "--bare" -- "$cur" )'
41+
echo $' while read -r; do COMPREPLY+=( "$REPLY" ); done < <( compgen -A directory -W "$(_mygit_completions_filter "--bare")" -- "$cur" )'
2442
echo $' ;;'
2543
echo $''
2644
echo $' *)'
27-
echo $' while read -r; do COMPREPLY+=( "$REPLY" ); done < <( compgen -W "--help --version status init commit" -- "$cur" )'
45+
echo $' while read -r; do COMPREPLY+=( "$REPLY" ); done < <( compgen -W "$(_mygit_completions_filter "--help --version status init commit")" -- "$cur" )'
2846
echo $' ;;'
2947
echo $''
3048
echo $' esac'

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

Lines changed: 22 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -12,26 +12,44 @@ fi
1212
# completely (https://github.com/dannyben/completely)
1313
# Modifying it manually is not recommended
1414

15+
_mygit_completions_filter() {
16+
local words="$1"
17+
local cur=${COMP_WORDS[COMP_CWORD]}
18+
local result=()
19+
20+
if [[ "${cur:0:1}" == "-" ]]; then
21+
echo "$words"
22+
23+
else
24+
for word in $words; do
25+
[[ "${word:0:1}" != "-" ]] && result+=("$word")
26+
done
27+
28+
echo "${result[*]}"
29+
30+
fi
31+
}
32+
1533
_mygit_completions() {
1634
local cur=${COMP_WORDS[COMP_CWORD]}
1735
local compwords=("${COMP_WORDS[@]:1:$COMP_CWORD-1}")
1836
local compline="${compwords[*]}"
1937

2038
case "$compline" in
2139
'status'*)
22-
while read -r; do COMPREPLY+=( "$REPLY" ); done < <( compgen -W "--help --verbose --branch $(git branch 2> /dev/null)" -- "$cur" )
40+
while read -r; do COMPREPLY+=( "$REPLY" ); done < <( compgen -W "$(_mygit_completions_filter "--help --verbose --branch $(git branch 2> /dev/null)")" -- "$cur" )
2341
;;
2442

2543
'commit'*)
26-
while read -r; do COMPREPLY+=( "$REPLY" ); done < <( compgen -A file -W "--help --message --all -a --quiet -q" -- "$cur" )
44+
while read -r; do COMPREPLY+=( "$REPLY" ); done < <( compgen -A file -W "$(_mygit_completions_filter "--help --message --all -a --quiet -q")" -- "$cur" )
2745
;;
2846

2947
'init'*)
30-
while read -r; do COMPREPLY+=( "$REPLY" ); done < <( compgen -A directory -W "--bare" -- "$cur" )
48+
while read -r; do COMPREPLY+=( "$REPLY" ); done < <( compgen -A directory -W "$(_mygit_completions_filter "--bare")" -- "$cur" )
3149
;;
3250

3351
*)
34-
while read -r; do COMPREPLY+=( "$REPLY" ); done < <( compgen -W "--help --version status init commit" -- "$cur" )
52+
while read -r; do COMPREPLY+=( "$REPLY" ); done < <( compgen -W "$(_mygit_completions_filter "--help --version status init commit")" -- "$cur" )
3553
;;
3654

3755
esac

spec/approvals/completions/function

Lines changed: 21 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -5,22 +5,40 @@ send_completions() {
55
echo $'# completely (https://github.com/dannyben/completely)'
66
echo $'# Modifying it manually is not recommended'
77
echo $''
8+
echo $'_completely_completions_filter() {'
9+
echo $' local words="$1"'
10+
echo $' local cur=${COMP_WORDS[COMP_CWORD]}'
11+
echo $' local result=()'
12+
echo $''
13+
echo $' if [[ "${cur:0:1}" == "-" ]]; then'
14+
echo $' echo "$words"'
15+
echo $' '
16+
echo $' else'
17+
echo $' for word in $words; do'
18+
echo $' [[ "${word:0:1}" != "-" ]] && result+=("$word")'
19+
echo $' done'
20+
echo $''
21+
echo $' echo "${result[*]}"'
22+
echo $''
23+
echo $' fi'
24+
echo $'}'
25+
echo $''
826
echo $'_completely_completions() {'
927
echo $' local cur=${COMP_WORDS[COMP_CWORD]}'
1028
echo $' local compwords=("${COMP_WORDS[@]:1:$COMP_CWORD-1}")'
1129
echo $' local compline="${compwords[*]}"'
1230
echo $''
1331
echo $' case "$compline" in'
1432
echo $' \'generate\'*)'
15-
echo $' while read -r; do COMPREPLY+=( "$REPLY" ); done < <( compgen -A directory -W "--help --force" -- "$cur" )'
33+
echo $' while read -r; do COMPREPLY+=( "$REPLY" ); done < <( compgen -A directory -W "$(_completely_completions_filter "--help --force")" -- "$cur" )'
1634
echo $' ;;'
1735
echo $''
1836
echo $' \'init\'*)'
19-
echo $' while read -r; do COMPREPLY+=( "$REPLY" ); done < <( compgen -W "--help" -- "$cur" )'
37+
echo $' while read -r; do COMPREPLY+=( "$REPLY" ); done < <( compgen -W "$(_completely_completions_filter "--help")" -- "$cur" )'
2038
echo $' ;;'
2139
echo $''
2240
echo $' *)'
23-
echo $' while read -r; do COMPREPLY+=( "$REPLY" ); done < <( compgen -W "--help --version init generate" -- "$cur" )'
41+
echo $' while read -r; do COMPREPLY+=( "$REPLY" ); done < <( compgen -W "$(_completely_completions_filter "--help --version init generate")" -- "$cur" )'
2442
echo $' ;;'
2543
echo $''
2644
echo $' esac'

spec/approvals/completions/script

Lines changed: 21 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -4,22 +4,40 @@
44
# completely (https://github.com/dannyben/completely)
55
# Modifying it manually is not recommended
66

7+
_completely_completions_filter() {
8+
local words="$1"
9+
local cur=${COMP_WORDS[COMP_CWORD]}
10+
local result=()
11+
12+
if [[ "${cur:0:1}" == "-" ]]; then
13+
echo "$words"
14+
15+
else
16+
for word in $words; do
17+
[[ "${word:0:1}" != "-" ]] && result+=("$word")
18+
done
19+
20+
echo "${result[*]}"
21+
22+
fi
23+
}
24+
725
_completely_completions() {
826
local cur=${COMP_WORDS[COMP_CWORD]}
927
local compwords=("${COMP_WORDS[@]:1:$COMP_CWORD-1}")
1028
local compline="${compwords[*]}"
1129

1230
case "$compline" in
1331
'generate'*)
14-
while read -r; do COMPREPLY+=( "$REPLY" ); done < <( compgen -A directory -W "--help --force" -- "$cur" )
32+
while read -r; do COMPREPLY+=( "$REPLY" ); done < <( compgen -A directory -W "$(_completely_completions_filter "--help --force")" -- "$cur" )
1533
;;
1634

1735
'init'*)
18-
while read -r; do COMPREPLY+=( "$REPLY" ); done < <( compgen -W "--help" -- "$cur" )
36+
while read -r; do COMPREPLY+=( "$REPLY" ); done < <( compgen -W "$(_completely_completions_filter "--help")" -- "$cur" )
1937
;;
2038

2139
*)
22-
while read -r; do COMPREPLY+=( "$REPLY" ); done < <( compgen -W "--help --version init generate" -- "$cur" )
40+
while read -r; do COMPREPLY+=( "$REPLY" ); done < <( compgen -W "$(_completely_completions_filter "--help --version init generate")" -- "$cur" )
2341
;;
2442

2543
esac

0 commit comments

Comments
 (0)