guarinogabriel / Mac-CLI

 macOS command line tool for developers – The ultimate tool to manage your Mac. It provides a huge set of command line commands that automatize the usage of your Mac.
MIT License
8.86k stars 334 forks source link

in zsh, i can't use auto complete of `mac` command #149

Closed ToBeDefined closed 6 years ago

guarinogabriel commented 6 years ago

Hi @ToBeDefined,

Thanks for your suggestion. ZSH is still not supported, but feel free to create a pull request to support the auto-complete feature in ZSH. It will be very much welcomed.

Regards. Gabriel

rprimus commented 6 years ago

Mon Apr 23 11:30:06 BST 2018

ZSH is still not supported

@guarinogabriel - might be worth mentioning this in the Readme.

mortenscheel commented 5 years ago

Here's an ugly little hack that enables autocompletion in ZSH. Add this code to .zshrc or put it in a separate file and source that from .zshrc

autoload -U +X compinit && compinit
autoload -U +X bashcompinit && bashcompinit
# use complete instead of compopt for zsh
if [ -n "$BASH_VERSION" ]; then
  COMP_OPT_CMD=compopt
elif [ -n "$ZSH_VERSION" ]; then
  COMP_OPT_CMD=complete
else
  COMP_OPT_CMD=compopt
fi

# The following functions are provided by bash_completion and are not available
# when using zsh bashcompinit/compinit which breaks autocompletion. The following
# ZSH-safe functions have been extracted from github.com/git/git-completion.bash
# and github.com/scop/bash-completion.
if ! type __reassemble_comp_words_by_ref >/dev/null 2>&1; then
  __reassemble_comp_words_by_ref() {
    local exclude i j first
    # Which word separators to exclude?
    exclude="${1//[^$COMP_WORDBREAKS]}"
    cword_=$COMP_CWORD
    if [ -z "$exclude" ]; then
      words_=("${COMP_WORDS[@]}")
      return
    fi
    # List of word completion separators has shrunk;
    # re-assemble words to complete.
    for ((i=0, j=0; i < ${#COMP_WORDS[@]}; i++, j++)); do
      # Append each nonempty word consisting of just
      # word separator characters to the current word.
      first=t
      while
        [ $i -gt 0 ] &&
        [ -n "${COMP_WORDS[$i]}" ] &&
        # word consists of excluded word separators
        [ "${COMP_WORDS[$i]//[^$exclude]}" = "${COMP_WORDS[$i]}" ]
      do
        # Attach to the previous token,
        # unless the previous token is the command name.
        if [ $j -ge 2 ] && [ -n "$first" ]; then
          ((j--))
        fi
        first=
        words_[$j]=${words_[j]}${COMP_WORDS[i]}
        if [ $i = $COMP_CWORD ]; then
          cword_=$j
        fi
        if (($i < ${#COMP_WORDS[@]} - 1)); then
          ((i++))
        else
          # Done.
          return
        fi
      done
      words_[$j]=${words_[j]}${COMP_WORDS[i]}
      if [ $i = $COMP_CWORD ]; then
        cword_=$j
      fi
    done
  }
fi

if ! type _get_comp_words_by_ref >/dev/null 2>&1; then
  _get_comp_words_by_ref () {
    local exclude cur_ words_ cword_
    if [ "$1" = "-n" ]; then
      exclude=$2
      shift 2
    fi
    __reassemble_comp_words_by_ref "$exclude"
    cur_=${words_[cword_]}
    while [ $# -gt 0 ]; do
      case "$1" in
      cur)
        cur=$cur_
        ;;
      prev)
        prev=${words_[$cword_-1]}
        ;;
      words)
        words=("${words_[@]}")
        ;;
      cword)
        cword=$cword_
        ;;
      esac
      shift
    done
  }
fi

if ! type __ltrim_colon_completions >/dev/null 2>&1; then
  __ltrim_colon_completions() {
    if [[ "$1" == *:* && "$COMP_WORDBREAKS" == *:* ]]; then
        # Remove colon-word prefix from COMPREPLY items
        local colon_word=${1%"${1##*:}"}
        local i=${#COMPREPLY[*]}
        while [[ $((--i)) -ge 0 ]]; do
            COMPREPLY[$i]=${COMPREPLY[$i]#"$colon_word"}
        done
    fi
  }
fi
_mac() {
    local MACWORDS cur

    COMPREPLY=()
    _get_comp_words_by_ref -n : cur

    MACWORDS=$(awk "/COMMANDS=/,/#/" < /usr/local/bin/mac  | tail -n +2 | sed '/)/d;/#/d')

    COMPREPLY=( $( compgen -W "$MACWORDS" -- "$cur" ) )

    __ltrim_colon_completions "$cur"
}
complete -F _mac mac