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

Tab completion #113

Closed
mcandre opened this issue Mar 31, 2018 · 11 comments
Closed

Tab completion #113

mcandre opened this issue Mar 31, 2018 · 11 comments

Comments

@mcandre
Copy link

mcandre commented Mar 31, 2018

Mage is so amazing, a breath of fresh air for build tools! Love the minimalism and potential for highly cross-platform builds! Hey, could we get some tab completion for target names? Perhaps a bash completion script?

@natefinch
Copy link
Member

natefinch commented Mar 31, 2018 via email

@natefinch
Copy link
Member

I saw some other tool (I forget which now) that would spit out the right scripts from a command (for either bash or zsh). That seems like a good way to do it.

@hanzei
Copy link
Contributor

hanzei commented Sep 16, 2018

https://github.com/posener/complete could help

@natefinch
Copy link
Member

So..... I think for now I'm going to close this, because I worry it's going to be a maintenance headache for little real gain. We can post completion scripts on the wiki or the website, and not have to worry about having our code do exactly the right thing.

@yohanyflores
Copy link

yohanyflores commented Nov 11, 2018

I am not a Bash developer, however this works for me:

mage-completion.sh:

#!/usr/bin/env bash

_mage_completions()
{
    local cur prev opts

    # namespaces with colon
    _get_comp_words_by_ref -n : cur

    # prev word.
    prev="${COMP_WORDS[COMP_CWORD-1]}"

    case "${prev}" in
        -compile)
            COMPREPLY+=( $(compgen -f -- "${cur}") )
            ;;
        -d)
            COMPREPLY+=( $(compgen -d -- "${cur}") )
            ;;
        -gocmd)
            COMPREPLY+=( $(compgen -f -- "${cur}") )
            ;;
        -t)
            opts="30s 1m 1m30s 2m 2m30s 3m 3m30s 4m 4m30s 5m 10m 20m 30m 1h"
            COMPREPLY+=( $(compgen -W "${opts}" -- ${cur}) )
            ;;
        *)
            if [[ ${cur} == -* ]]; then
                opts="$(mage -h | grep "^\\s*-" | awk '{print $1}')"
            else
                opts="$(mage -l | tail -n +2 | awk '{print tolower($1)}')"
            fi
            COMPREPLY+=( $(compgen -W "${opts}" -- "${cur}"))
            ;;
    esac

    __ltrim_colon_completions "$cur"
}

complete -F _mage_completions mage

I'm using it this way:

source mage-completion.sh

@mcandre
Copy link
Author

mcandre commented Nov 16, 2018

You sir win one free Internet!

Could we publish this to Homebrew to make it more accessible :)

@yohanyflores
Copy link

Sure :)

@iwittkau
Copy link

iwittkau commented Mar 6, 2019

I created a mage CLI frontend to circumvent bash completion. Maybe someone else finds this useful.

@natefinch
Copy link
Member

Dude, that is freakin' cool!

@hbt
Copy link

hbt commented Oct 8, 2020

zsh version reusing the bash version. If you have one with the custom bash functions implemented in pure zsh; feel free to share.

autoload -U compinit
autoload -U bashcompinit
compinit
bashcompinit
_get_comp_words_by_ref () {
}
__ltrim_colon_completions() {
}

# https://github.com/magefile/mage/issues/113#issuecomment-437710124
#source the_bash_version

Also, this (https://github.com/urfave/cli/blob/master/docs/v2/manual.md#bash-completion) is a good implementation of general bash/zsh autocompletion for cli tools.

The completion scripts (https://github.com/urfave/cli/tree/61026898ff6cbe7c1b4f19feafc6f9ffe098d7e2/autocomplete) are simple and all the logic is go and works across interpreters.

@sluedecke
Copy link

sluedecke commented Mar 19, 2021

Thanks a lot for #113 (comment) !

Since one can force colored output by setting MAGEFILE_ENABLE_COLOR=1 or setting it within the magefile like this:

func init() {
	// enable colored output from mage if not set from the outside
	if os.Getenv(mg.EnableColorEnv) == "" {
		os.Setenv(mg.EnableColorEnv, "1")
	}
}

the completion script can be adjusted accordingly with:

#!/usr/bin/env bash

_mage_completions()
{
    local cur prev opts

    # namespaces with colon
    _get_comp_words_by_ref -n : cur

    # prev word.
    prev="${COMP_WORDS[COMP_CWORD-1]}"

    case "${prev}" in
        -compile)
            COMPREPLY+=( $(compgen -f -- "${cur}") )
            ;;
        -d)
            COMPREPLY+=( $(compgen -d -- "${cur}") )
            ;;
        -gocmd)
            COMPREPLY+=( $(compgen -f -- "${cur}") )
            ;;
        -t)
            opts="30s 1m 1m30s 2m 2m30s 3m 3m30s 4m 4m30s 5m 10m 20m 30m 1h"
            COMPREPLY+=( $(compgen -W "${opts}" -- ${cur}) )
            ;;
        *)
            
            if [[ ${cur} == -* ]]; then
                opts="$(MAGEFILE_ENABLE_COLOR=0 mage -h | grep "^\\s*-" | awk '{print $1}')"
            else
                opts="$(MAGEFILE_ENABLE_COLOR=0 mage -l | tail -n +2 | awk '{print tolower($1)}')"
            fi
            COMPREPLY+=( $(compgen -W "${opts}" -- "${cur}"))
            ;;
    esac

    __ltrim_colon_completions "$cur"
}

complete -F _mage_completions mage

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

7 participants