krupan / albion

A UNIX/Linux environment manager that allows on-the-fly configuration of your shell environment. Scroll down to read more.
GNU General Public License v3.0
2 stars 1 forks source link

Make it easier to write shell completion code #2

Open cmarqu opened 8 years ago

cmarqu commented 8 years ago

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's list-configs would either be modified to return that list (and print the current format with list-configs-verbose), or add a new command like list-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 :)

krupan commented 7 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?

krupan commented 7 years ago

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?

krupan commented 7 years ago

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.

cmarqu commented 7 years ago

Thanks, these sound good, I'll try it once I am back from vacation ☺️

cmarqu commented 7 years ago

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