Changeset View
Standalone View
support/shell/templates/bash-template.sh
_arcanist_complete_{{{BIN}}} () | _arcanist_complete_{{{BIN}}} () | ||||
{ | { | ||||
COMPREPLY=() | COMPREPLY=() | ||||
CUR="${COMP_WORDS[COMP_CWORD]}" | RESULT=$(echo | {{{BIN}}} shell-complete \ | ||||
OPTS=$(echo | {{{BIN}}} shell-complete --current ${COMP_CWORD} -- ${COMP_WORDS[@]} 2>/dev/null) | --current ${COMP_CWORD} \ | ||||
-- \ | |||||
"${COMP_WORDS[@]}" \ | |||||
2>/dev/null) | |||||
epriestley: We need to quote `${COMP_WORDS[@]}` or `foo\ bar` gets passed to `shell-complete` as two… | |||||
if [ $? -ne 0 ]; then | if [ $? -ne 0 ]; then | ||||
return $? | return $? | ||||
fi | fi | ||||
if [ "$OPTS" = "FILE" ]; then | if [ "$RESULT" == "<compgen:file>" ]; then | ||||
COMPREPLY=( $(compgen -f -- ${CUR}) ) | RESULT=$( compgen -A file -- ${COMP_WORDS[COMP_CWORD]} ) | ||||
return 0 | |||||
fi | fi | ||||
Done Inline ActionsSee T9116 for more details on this. The new code is basically the same as the old code, except I've replaced -f with the more explicit -A file (both have the same meaning, -A file is just more explicit and easier to find in the documentation) and am replacing the result instead of turning it into an array here, so we can do the "turn it into an array" part later on in one place. At least on my machine, I can't get the correct behavior from this command unless we run compgen from the script instead of from PHP. I'd prefer to just run compgen ... from PHP over returning "<compgen:file>" as a magic string, but couldn't get it to work. epriestley: See T9116 for more details on this. The new code is basically the same as the old code, except… | |||||
if [ "$OPTS" = "ARGUMENT" ]; then | local IFS=$'\n' | ||||
return 0 | COMPREPLY=( $RESULT ) | ||||
fi | |||||
COMPREPLY=( $(compgen -W "${OPTS}" -- ${CUR}) ) | |||||
} | } | ||||
Done Inline ActionsThis is probably the most important part in getting the behavior correct, and means: $COMPREPLY = explode("\n", $RESULT); The previous behavior was to explode on any whitespace, without regard for quotes, so a\ b would become two tokens. IFS is "internal field separator", a piece of bash dark magic. epriestley: This is probably the most important part in getting the behavior correct, and means:
```… | |||||
complete -F _arcanist_complete_{{{BIN}}} -o filenames {{{BIN}}} | complete -F _arcanist_complete_{{{BIN}}} -o filenames {{{BIN}}} |
We need to quote ${COMP_WORDS[@]} or foo\ bar gets passed to shell-complete as two separate arguments. Otherwise, I just wrapped this so it looks a little nicer.