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

tool to generate bash/zsh completion? #24

Open
endel opened this issue Mar 21, 2014 · 3 comments
Open

tool to generate bash/zsh completion? #24

endel opened this issue Mar 21, 2014 · 3 comments

Comments

@endel
Copy link

endel commented Mar 21, 2014

is there any?

@nategood
Copy link
Owner

That falls outside the realm of php and more into the realm of the shell
language as far as I am aware. While you could write php to help with the
autocomplete, in the case of bash at least, it would still require sticking
something in your .bashrc. again afaik.
On Mar 21, 2014 12:02 PM, "Endel Dreyer" [email protected] wrote:

is there any?

Reply to this email directly or view it on GitHubhttps://github.com//issues/24
.

@endel
Copy link
Author

endel commented Mar 22, 2014

Yes, but I think it's totally possible to PHP generate the bash completion file, since the necessary definitions are already there.

Completions AFAIK have four main variables:

  • COMP_WORDS (array): list of words on the line
  • COMP_CWORD (integer): index of current typing word
  • COMP_LINE (string): the current commandline
  • COMPREPLY (array): reply for completion

The following template provides a very simple completion, just for commands/subcommands and options. Maybe commando could generate that, and leave to the user to create more advanced completions, if necessary.

#!/usr/bin/env bash

__commando()
{
    local cur=${COMP_WORDS[COMP_CWORD]}
    local prev=${COMP_WORDS[COMP_CWORD-1]}

    local first=$(echo $cur | cut -d ':' -f 1)
    local second=$(echo $cur | cut -d ':' -f 2)

    # Going deep on sub-commands.
    case "$first" in
      "subcommand")
        COMPREPLY=( $( compgen -W "sub1 sub2 sub3" -- $second ) )
        return 0
        ;;

      "subcommand2")
        COMPREPLY=( $( compgen -W "cmd1 cmd2 cmd3" -- $second ) )
        return 0
        ;;
    esac

    # All options here
    if [[ "$cur" == -* ]]; then
      COMPREPLY=( $( compgen -W "--all --options --here" -- $cur ) )
      return 0
    fi

    # List of all "top" commands here.
    # Subcommands should be listed on the "case" statement.
    # Sub-commands are separated by ":"
    COMPREPLY=( $(compgen -W "all commands here subcommand: subcommand2:" -- $cur) )
    return 0
}

# zsh compatibility
if [[ -n ${ZSH_VERSION-} ]]; then
  autoload -U +X bashcompinit && bashcompinit
fi

# install "complete" for session
complete -F __commando -o nospace commando-binary

@nategood
Copy link
Owner

Right. It's the "leave it up to the user" part that feels funky.

If it's kept isolated enough, I'm open to exploring it I suppose.
On Mar 22, 2014 12:54 PM, "Endel Dreyer" [email protected] wrote:

Yes, but I think it's totally possible to PHP generate the bash completion
file, since the necessary definitions are already there.

Completions AFAIK have four main variables:

  • COMP_WORDS (array): list of words on the line
  • COMP_CWORD (integer): index of current typing word
  • COMP_LINE (string): the current commandline
  • COMPREPLY (array): reply for completion

The following template provides a very simple completion, just for
commands/subcommands and options. Maybe commando could generate that, and
leave to the user to create more advanced completions, if necessary.

#!/usr/bin/env bash

__commando(){
local cur=${COMP_WORDS[COMP_CWORD]}
local prev=${COMP_WORDS[COMP_CWORD-1]}

local first=$(echo $cur | cut -d ':' -f 1)
local second=$(echo $cur | cut -d ':' -f 2)

# Going deep on sub-commands.
case "$first" in
  "subcommand")
    COMPREPLY=( $( compgen -W "sub1 sub2 sub3" -- $second ) )
    return 0
    ;;

  "subcommand2")
    COMPREPLY=( $( compgen -W "cmd1 cmd2 cmd3" -- $second ) )
    return 0
    ;;
esac

# All options here
if [[ "$cur" == -* ]]; then      COMPREPLY=( $( compgen -W "--all --options --here" -- $cur ) )
  return 0
fi

# List of all "top" commands here.
# Subcommands should be listed on the "case" statement.
# Sub-commands are separated by ":"
COMPREPLY=( $(compgen -W "all commands here subcommand: subcommand2:" -- $cur) )
return 0}

zsh compatibilityif [[ -n ${ZSH_VERSION-} ]]; then autoload -U +X bashcompinit && bashcompinitfi

install "complete" for sessioncomplete -F __commando -o nospace commando-binary

Reply to this email directly or view it on GitHubhttps://github.com//issues/24#issuecomment-38357137
.

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

2 participants