Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Hide flag completion unless input ends with a hyphen #35

Merged
merged 3 commits into from
Sep 4, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 3 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -97,6 +97,9 @@ Each pattern in this configuration file will be checked against the user's
input, and if the input **starts with** a matching pattern, the list that
follows it will be suggested as completions.

Note that the suggested completions will not show flags (string that start with
a hyphen `-`) unless the input ends with a hyphen.

To generate the bash script, simply run:

```bash
Expand Down
6 changes: 5 additions & 1 deletion lib/completely/completions.rb
Original file line number Diff line number Diff line change
Expand Up @@ -54,7 +54,7 @@ def tester

def patterns!
config.map do |text, completions|
Pattern.new text, completions
Pattern.new text, completions, pattern_function_name
Copy link
Owner Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Pattern now receives a function name, which is the name of the helper filter function. It is used in Pattern#compgen to generate the compgen string which now calls a function rather than just using the string straight up.

end.sort_by { |pattern| -pattern.length }
end

Expand All @@ -74,6 +74,10 @@ def function_name
@function_name ||= "_#{command}_completions"
end

def pattern_function_name
@pattern_function_name ||= "#{function_name}_filter"
end

def pattern_prefixes
patterns.map &:prefix
end
Expand Down
7 changes: 4 additions & 3 deletions lib/completely/pattern.rb
Original file line number Diff line number Diff line change
@@ -1,10 +1,11 @@
module Completely
class Pattern
attr_reader :text, :completions
attr_reader :text, :completions, :function_name

def initialize(text, completions)
def initialize(text, completions, function_name)
@text = text
@completions = completions || []
@function_name = function_name
end

def length
Expand Down Expand Up @@ -53,7 +54,7 @@ def compgen
def compgen!
result = []
result << %Q[#{actions.join ' '}] if actions.any?
result << %Q[-W "#{words.join ' '}"] if words.any?
result << %Q[-W "$(#{function_name} "#{words.join ' '}")"] if words.any?
result.any? ? result.join(' ') : nil
end
end
Expand Down
18 changes: 18 additions & 0 deletions lib/completely/templates/template.erb
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,24 @@
# completely (https://github.com/dannyben/completely)
# Modifying it manually is not recommended

<%= function_name %>_filter() {
local words="$1"
local cur=${COMP_WORDS[COMP_CWORD]}
local result=()

if [[ "${cur:0:1}" == "-" ]]; then
echo "$words"

else
for word in $words; do
[[ "${word:0:1}" != "-" ]] && result+=("$word")
done

echo "${result[*]}"

fi
}

<%= function_name %>() {
Copy link
Owner Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The function at line 7 receives a space-delimited string of possible completions, and:

  1. returns the same string if the current word starts with a hyphen
  2. returns a new string without any --flags if the current word does not start with a hyphen

local cur=${COMP_WORDS[COMP_CWORD]}
local compwords=("${COMP_WORDS[@]:1:$COMP_CWORD-1}")
Expand Down
26 changes: 22 additions & 4 deletions spec/approvals/cli/generated-script
Original file line number Diff line number Diff line change
Expand Up @@ -4,26 +4,44 @@
# completely (https://github.com/dannyben/completely)
# Modifying it manually is not recommended

_mygit_completions_filter() {
local words="$1"
local cur=${COMP_WORDS[COMP_CWORD]}
local result=()

if [[ "${cur:0:1}" == "-" ]]; then
echo "$words"

else
for word in $words; do
[[ "${word:0:1}" != "-" ]] && result+=("$word")
done

echo "${result[*]}"

fi
}

_mygit_completions() {
local cur=${COMP_WORDS[COMP_CWORD]}
local compwords=("${COMP_WORDS[@]:1:$COMP_CWORD-1}")
local compline="${compwords[*]}"

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

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

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

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

esac
Expand Down
26 changes: 22 additions & 4 deletions spec/approvals/cli/generated-script-alt
Original file line number Diff line number Diff line change
Expand Up @@ -4,26 +4,44 @@
# completely (https://github.com/dannyben/completely)
# Modifying it manually is not recommended

_mycomps_filter() {
local words="$1"
local cur=${COMP_WORDS[COMP_CWORD]}
local result=()

if [[ "${cur:0:1}" == "-" ]]; then
echo "$words"

else
for word in $words; do
[[ "${word:0:1}" != "-" ]] && result+=("$word")
done

echo "${result[*]}"

fi
}

_mycomps() {
local cur=${COMP_WORDS[COMP_CWORD]}
local compwords=("${COMP_WORDS[@]:1:$COMP_CWORD-1}")
local compline="${compwords[*]}"

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

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

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

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

esac
Expand Down
26 changes: 22 additions & 4 deletions spec/approvals/cli/generated-wrapped-script
Original file line number Diff line number Diff line change
Expand Up @@ -5,26 +5,44 @@ give_comps() {
echo $'# completely (https://github.com/dannyben/completely)'
echo $'# Modifying it manually is not recommended'
echo $''
echo $'_mygit_completions_filter() {'
echo $' local words="$1"'
echo $' local cur=${COMP_WORDS[COMP_CWORD]}'
echo $' local result=()'
echo $''
echo $' if [[ "${cur:0:1}" == "-" ]]; then'
echo $' echo "$words"'
echo $' '
echo $' else'
echo $' for word in $words; do'
echo $' [[ "${word:0:1}" != "-" ]] && result+=("$word")'
echo $' done'
echo $''
echo $' echo "${result[*]}"'
echo $''
echo $' fi'
echo $'}'
echo $''
echo $'_mygit_completions() {'
echo $' local cur=${COMP_WORDS[COMP_CWORD]}'
echo $' local compwords=("${COMP_WORDS[@]:1:$COMP_CWORD-1}")'
echo $' local compline="${compwords[*]}"'
echo $''
echo $' case "$compline" in'
echo $' \'status\'*)'
echo $' while read -r; do COMPREPLY+=( "$REPLY" ); done < <( compgen -W "--help --verbose --branch $(git branch 2> /dev/null)" -- "$cur" )'
echo $' while read -r; do COMPREPLY+=( "$REPLY" ); done < <( compgen -W "$(_mygit_completions_filter "--help --verbose --branch $(git branch 2> /dev/null)")" -- "$cur" )'
echo $' ;;'
echo $''
echo $' \'commit\'*)'
echo $' while read -r; do COMPREPLY+=( "$REPLY" ); done < <( compgen -A file -W "--help --message --all -a --quiet -q" -- "$cur" )'
echo $' while read -r; do COMPREPLY+=( "$REPLY" ); done < <( compgen -A file -W "$(_mygit_completions_filter "--help --message --all -a --quiet -q")" -- "$cur" )'
echo $' ;;'
echo $''
echo $' \'init\'*)'
echo $' while read -r; do COMPREPLY+=( "$REPLY" ); done < <( compgen -A directory -W "--bare" -- "$cur" )'
echo $' while read -r; do COMPREPLY+=( "$REPLY" ); done < <( compgen -A directory -W "$(_mygit_completions_filter "--bare")" -- "$cur" )'
echo $' ;;'
echo $''
echo $' *)'
echo $' while read -r; do COMPREPLY+=( "$REPLY" ); done < <( compgen -W "--help --version status init commit" -- "$cur" )'
echo $' while read -r; do COMPREPLY+=( "$REPLY" ); done < <( compgen -W "$(_mygit_completions_filter "--help --version status init commit")" -- "$cur" )'
echo $' ;;'
echo $''
echo $' esac'
Expand Down
26 changes: 22 additions & 4 deletions spec/approvals/cli/test/completely-tester.sh
Original file line number Diff line number Diff line change
Expand Up @@ -12,26 +12,44 @@ fi
# completely (https://github.com/dannyben/completely)
# Modifying it manually is not recommended

_mygit_completions_filter() {
local words="$1"
local cur=${COMP_WORDS[COMP_CWORD]}
local result=()

if [[ "${cur:0:1}" == "-" ]]; then
echo "$words"

else
for word in $words; do
[[ "${word:0:1}" != "-" ]] && result+=("$word")
done

echo "${result[*]}"

fi
}

_mygit_completions() {
local cur=${COMP_WORDS[COMP_CWORD]}
local compwords=("${COMP_WORDS[@]:1:$COMP_CWORD-1}")
local compline="${compwords[*]}"

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

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

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

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

esac
Expand Down
24 changes: 21 additions & 3 deletions spec/approvals/completions/function
Original file line number Diff line number Diff line change
Expand Up @@ -5,22 +5,40 @@ send_completions() {
echo $'# completely (https://github.com/dannyben/completely)'
echo $'# Modifying it manually is not recommended'
echo $''
echo $'_completely_completions_filter() {'
echo $' local words="$1"'
echo $' local cur=${COMP_WORDS[COMP_CWORD]}'
echo $' local result=()'
echo $''
echo $' if [[ "${cur:0:1}" == "-" ]]; then'
echo $' echo "$words"'
echo $' '
echo $' else'
echo $' for word in $words; do'
echo $' [[ "${word:0:1}" != "-" ]] && result+=("$word")'
echo $' done'
echo $''
echo $' echo "${result[*]}"'
echo $''
echo $' fi'
echo $'}'
echo $''
echo $'_completely_completions() {'
echo $' local cur=${COMP_WORDS[COMP_CWORD]}'
echo $' local compwords=("${COMP_WORDS[@]:1:$COMP_CWORD-1}")'
echo $' local compline="${compwords[*]}"'
echo $''
echo $' case "$compline" in'
echo $' \'generate\'*)'
echo $' while read -r; do COMPREPLY+=( "$REPLY" ); done < <( compgen -A directory -W "--help --force" -- "$cur" )'
echo $' while read -r; do COMPREPLY+=( "$REPLY" ); done < <( compgen -A directory -W "$(_completely_completions_filter "--help --force")" -- "$cur" )'
echo $' ;;'
echo $''
echo $' \'init\'*)'
echo $' while read -r; do COMPREPLY+=( "$REPLY" ); done < <( compgen -W "--help" -- "$cur" )'
echo $' while read -r; do COMPREPLY+=( "$REPLY" ); done < <( compgen -W "$(_completely_completions_filter "--help")" -- "$cur" )'
echo $' ;;'
echo $''
echo $' *)'
echo $' while read -r; do COMPREPLY+=( "$REPLY" ); done < <( compgen -W "--help --version init generate" -- "$cur" )'
echo $' while read -r; do COMPREPLY+=( "$REPLY" ); done < <( compgen -W "$(_completely_completions_filter "--help --version init generate")" -- "$cur" )'
echo $' ;;'
echo $''
echo $' esac'
Expand Down
24 changes: 21 additions & 3 deletions spec/approvals/completions/script
Original file line number Diff line number Diff line change
Expand Up @@ -4,22 +4,40 @@
# completely (https://github.com/dannyben/completely)
# Modifying it manually is not recommended

_completely_completions_filter() {
local words="$1"
local cur=${COMP_WORDS[COMP_CWORD]}
local result=()

if [[ "${cur:0:1}" == "-" ]]; then
echo "$words"

else
for word in $words; do
[[ "${word:0:1}" != "-" ]] && result+=("$word")
done

echo "${result[*]}"

fi
}

_completely_completions() {
local cur=${COMP_WORDS[COMP_CWORD]}
local compwords=("${COMP_WORDS[@]:1:$COMP_CWORD-1}")
local compline="${compwords[*]}"

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

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

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

esac
Expand Down
Loading