Skip to content

Commit

Permalink
fix: properly encapsulate expanded arrays in ""
Browse files Browse the repository at this point in the history
  • Loading branch information
RodEsp committed Dec 9, 2021
1 parent 2c6e060 commit 979be23
Showing 1 changed file with 8 additions and 10 deletions.
18 changes: 8 additions & 10 deletions src/commands/autocomplete/create.ts
Original file line number Diff line number Diff line change
Expand Up @@ -215,10 +215,10 @@ ${this.bashCommandsWithFlagsList}
# Remove colon-word prefix from $commands
commands=( "\${commands[@]/$colonPrefix}" )
for i in \${!commands[@]}; do
for i in "\${!commands[@]}"; do
if [[ "\${commands[$i]}" == "$normalizedCommand" ]]; then
# If the currently typed in command is a topic command we need to remove it to avoid suggesting it again
unset commands[$i]
unset "\${commands[$i]}"
else
# Trim subcommands from each command
commands[$i]="\${commands[$i]%%:*}"
Expand All @@ -231,10 +231,10 @@ ${this.bashCommandsWithFlagsList}
__COMP_WORDS=( "\${COMP_WORDS[@]:1}" )
# The command typed by the user but separated by colons (e.g. "mycli command subcom" -> "command:subcom")
normalizedCommand="$( printf "%s" "$(join_by ":" \${__COMP_WORDS[@]})" )"
normalizedCommand="$( printf "%s" "$(join_by ":" "\${__COMP_WORDS[@]}")" )"
# The command hirarchy, with colons, leading up to the last subcommand entered (e.g. "mycli com subcommand subsubcom" -> "com:subcommand:")
colonPrefix="\${normalizedCommand%\${normalizedCommand##*:}}"
colonPrefix="\${normalizedCommand%"\${normalizedCommand##*:}"}"
if [[ -z "$normalizedCommand" ]]; then
# If there is no normalizedCommand yet the user hasn't typed in a full command
Expand All @@ -243,7 +243,6 @@ ${this.bashCommandsWithFlagsList}
else
# Filter $commands to just the ones that match the $normalizedCommand and turn into an array
commands=( $(compgen -W "$commands" -- "\${normalizedCommand}") )
# Trim higher level and subcommands from the subcommands to suggest
__trim_colon_commands "$colonPrefix"
Expand All @@ -253,19 +252,18 @@ ${this.bashCommandsWithFlagsList}
# Flag
# The full CLI command separated by colons (e.g. "mycli command subcommand --fl" -> "command:subcommand")
# This needs to be defined with $COMP_CWORD-1 as opposed to above because the current character on the command line is "-" and we want everything before that
normalizedCommand="$( printf "%s" "$(join_by ":" \${COMP_WORDS[@]:1:($COMP_CWORD - 1)})" )"
# This needs to be defined with $COMP_CWORD-1 as opposed to above because the current "word" on the command line is a flag and the command is everything before the flag
normalizedCommand="$( printf "%s" "$(join_by ":" "\${COMP_WORDS[@]:1:($COMP_CWORD - 1)}")" )"
# The line below finds the command in $commands using grep
# Then, using sed, it removes everything from the found command before the --flags (e.g. "command:subcommand:subsubcom --flag1 --flag2" -> "--flag1 --flag2")
opts=$(printf "$commands" | grep "\${normalizedCommand}" | sed -n "s/^\${normalizedCommand} //p")
opts=$(echo "$commands" | grep "\${normalizedCommand}" | sed -n "s/^\${normalizedCommand} //p")
fi
COMPREPLY=($(compgen -W "$opts" -- \${cur}))
COMPREPLY=($(compgen -W "$opts" -- "\${cur}"))
}
complete -F _${cliBin} ${cliBin}
`
} else {
bashScript = `#!/usr/bin/env bash
Expand Down

0 comments on commit 979be23

Please sign in to comment.