Feb 10, 2013 - ZSH Completion for frogblog

Comments

giving frogblog zsh completion

I always was annoyed, that working with the frogblog was a pain. One has to type the correct subject name into the command line. Therefore you must first get a list of all the subjects by frogblog list.

I wondered always, how to enable completion inside my console. Because i use zsh as my shell, i show here how to enable completion for frogblog in zsh.

Completion Folders

The folder where zsh looks for completions are listed in the $fpath variable. You can add the completion file in one of the existing folders. I created ~/.zsh/complete and added the path to the $fpath. Here are the additional lines for .zshrc:

   fpath=(~/.zsh/complete $fpath)

Also make sure that the compinit is autoloaded (what ever that means ;)) and that the right completion menu is known.

   autoload -U compinit
    compinit
    
    zstyle ':completion:*' menu select=2

Completion file for frogblog

Completion files are named with a starting '_'. Here is the content of ~/.zsh/complete/_frogblog

    #compdef frogblog
    
    _frogblog() {
        local curcontext="\$curcontext" state line
        typeset -A opt_args
    
        _arguments \
            '1: :->action'\
            '*: :->subject'
    
        case \$state in
        action)
            _arguments '1:actions:(list publish delete)'
        ;;
        *)
            case \$words[2] in
            list)
            ;;
            publish)
                compadd "\$@" `ls *.hlog`
            ;;
            delete)
                compadd "\$@" `frogblog list | xargs`
            ;;
            
            *)
                _files
            esac
        esac
    }
    
    _frogblog "$@"

The _arguments need to be first an action, which can be of the list (list publish delete). For every one of these actions there are different argument to be submitted.

  • list - is the one that has no more arguments
  • publish - offers all files in the current directory end with .hlog
  • delete - offers the list that frogblog list would return

If you want to know more how this mechanism actually works, have a look at the page i found the most useful information about zsh completion: ZSH – Writing own completion functions.