Open cmarqu opened 8 years ago
That should be simple. I like list-configs and list-envs being more human readable. Is it a pain to have separate commands like list-configs-for-completion?
Also, for configs I think it's going to have to be mypath with 1.0 in a second step. I would assume that second step would run an albion command like this:
albion list-versions mypath
Where mypath is a config name. Does that work?
I added three new commands in commit 5dccac643eda271d2f5180f5b9e1ec3e738a1f4c that hopefully fit the bill:
list-envs-for-completion list-configs-for-completion list-versions
If those work please close this issue.
Thanks, these sound good, I'll try it once I am back from vacation ☺️
Here is something that is not perfect but is better than nothing - (save as bash_completion.albion
and source it).
# -*- mode: sh -*-
_albion_complete()
{
local cmds cur_word prev_word
cmds="help env load unload list-envs list-configs list-versions status which"
# COMP_WORDS is an array of words in the current command line.
# COMP_CWORD is the index of the current word (the one the cursor is
# in). So COMP_WORDS[COMP_CWORD] is the current word; we also record
# the previous word here.
cur_word="${COMP_WORDS[COMP_CWORD]}"
prev_word="${COMP_WORDS[COMP_CWORD-1]}"
# COMPREPLY is the array of possible completions, generated with the compgen builtin.
if [[ ${prev_word} == "albion" || ${prev_word} == "alb" ]] ; then
COMPREPLY=( $(compgen -W "${cmds}" -- ${cur_word}) )
elif [[ ${prev_word} == "load" ]] ; then
COMPREPLY=( $(compgen -W "`albion list-configs-for-completion`" -- ${cur_word}) )
elif [[ ${prev_word} == "unload" ]] ; then
COMPREPLY=( $(compgen -W "`albion load`" -- ${cur_word}) ) # FIXME: does not work well
elif [[ ${prev_word} == "list-versions" ]] ; then
COMPREPLY=( $(compgen -W "`albion list-configs-for-completion`" -- ${cur_word}) )
elif [[ ${prev_word} == "env" ]] ; then
COMPREPLY=( $(compgen -W "`albion list-envs-for-completion`" -- ${cur_word}) )
elif [[ ${prev_word} == "which" ]] ; then
COMPREPLY=( $(compgen -W "`albion list-envs-for-completion` `albion list-configs-for-completion`" -- ${cur_word}) )
else # no known command matched, so assume a config name was previously completed (FIXME: bad hack!)
COMPREPLY=( $(compgen -W "`albion list-versions ${prev_word}`" -- ${cur_word}) )
fi
# echo "COMPREPLY=${COMPREPLY}"
return 0
}
# Register _albion_complete to provide completion for the following commands
complete -F _albion_complete albion
complete -F _albion_complete alb
In order to write shell tab completion code for e.g.
compgen
, it is helpful to get a list of space-separated items from somewhere. It's best if "somewhere" is the tool to be completed itself. So if I wanted to complete the configs, it would be good if albion'slist-configs
would either be modified to return that list (and print the current format withlist-configs-verbose
), or add a new command likelist-configs-for-completion
.The question now is what to return there - taking the example shipped with albion, it could be "mypath/1.0", a format which albion should then be able to digest, or just "mypath", with the "1.0" to be completed in a second step, which would then need another command like
list-versions
.(Same for the envs of course.)
(And don't let this ticket spoil your vacation, have fun :)