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

zsh completion hints #1156

Closed
thom4parisot opened this issue Jun 20, 2018 · 8 comments
Closed

zsh completion hints #1156

thom4parisot opened this issue Jun 20, 2018 · 8 comments

Comments

@thom4parisot
Copy link

thom4parisot commented Jun 20, 2018

Hello,

I was inspecting zsh completion and discovered it was not working.
Too bad!

By screening into https://github.com/mklabs/tabtab/blob/master/scripts/zsh.sh, I found out there was not much to change to port the existing template.

From this bash handlebar template

###-begin-{{app_name}}-completions-###
#
# yargs command completion script
#
# Installation: {{app_path}} {{completion_command}} >> ~/.bashrc
#    or {{app_path}} {{completion_command}} >> ~/.bash_profile on OSX.
#
_yargs_completions()
{
    local cur_word args type_list

    cur_word="${COMP_WORDS[COMP_CWORD]}"
    args=("${COMP_WORDS[@]}")

    # ask yargs to generate completions.
    type_list=$({{app_path}} --get-yargs-completions "${args[@]}")

    COMPREPLY=( $(compgen -W "${type_list}" -- ${cur_word}) )

    # if no match was found, fall back to filename completion
    if [ ${#COMPREPLY[@]} -eq 0 ]; then
      COMPREPLY=( $(compgen -f -- "${cur_word}" ) )
    fi

    return 0
}
complete -F _yargs_completions {{app_name}}
###-end-{{app_name}}-completions-###

To this zsh completion template

###-begin-{{app_name}}-completions-###
#
# yargs command completion script
#
# Installation: {{app_path}} {{completion_command}} >> ~/.bashrc
#    or {{app_path}} {{completion_command}} >> ~/.bash_profile on OSX.
#
_yargs_completions()
{
  local reply
  local si=$IFS

  # ask yargs to generate completions.
  IFS=$'\n' reply=($(COMP_CWORD="$((CURRENT-1))" COMP_LINE="$BUFFER" COMP_POINT="$CURSOR" {{app_path}} --get-yargs-completions "${words[@]}"))
  IFS=$si

  _describe 'values' reply
}
compdef _yargs_completions {{app_name}}
###-end-{{app_name}}-completions-###

I have no idea what's going on but it works — although it seems to recommend everything and not much what's only matching the typed words.

It's a start.

@thom4parisot
Copy link
Author

There are plenty of beautiful stuff happening in https://github.com/f/omelette/blob/master/coffee/omelette.coffee too.

@thom4parisot
Copy link
Author

The last element being bashcompinit, to enable bash completion for zsh.

autoload -U +X bashcompinit && bashcompinit
eval "$(yargs-enabled-program completion)"

@loilo
Copy link

loilo commented Jul 31, 2018

For anybody interested: Until there's official support for zsh completion, I made this little tool (with the aforementioned omelette).

@OliverJAsh
Copy link

I worked around this by enabling zsh's bash completion script compatibility mode: https://stackoverflow.com/a/8492043/5932012

@trevorlinton
Copy link
Contributor

Hello! I'd like to contribute back to yargs and have already written a zsh handler, before I started I wanted to make sure no one had already began work or if there were any requests around it.

@tylers-username
Copy link

@trevorlinton - would love to see what you came up with.

@bcoe bcoe closed this as completed in 16c5d25 Feb 14, 2019
@john-osullivan
Copy link

@loilo Hey, competarr seems to provide exactly the experience I was hoping yargs autocomplete would! I see how the scripts get generated, but having to update bash myself seemed like a waste. Thanks for writing this up! That simple setup through scripts is perfect.

@loilo
Copy link

loilo commented Sep 26, 2019

@john-osullivan Thanks! But to be honest, the point of completarr is pretty much void now that yargs has built-in auto completion for zsh (which was my major pain point before). Using it also is a performance hit as it executes and parses the --help output, which is quite some overhead.

The easy installation step however is a thing I consider very important — maybe I should provide an installation script for yargs-based CLIs as an extra package. 🙂

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