zshの補完ってホントに便利ですよねー
堕落しきった俺には、もはやperlbrewのサブコマンドを入力することさえ困難なのです。
というわけで、perlbrewのzshでのコマンド補完(改) - LAPISLAZULI HILL#Hatena のルールを利用させてもらって、補完できるようにしました。
でも、このルールは若干古くなっていたので、ちょこっと修正しまった↓
具体的には、switchとuseの補完候補にinstalledサブコマンド(listサブコマンドの古いやつ)を使うようになっていたので、修正しています。
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#compdef perlbrew
typeset -A opt_args
local context state line
_arguments -C \
'(- 1 *)'{-h,--help}'[prints help]' \
'(-f --force)'{-f,--force}'[Force installation of a perl]' \
'(-q --quiet)'{-q,--quiet}'[Log output to a log file rather than STDOUT. This is the default.]' \
'(-v --verbose)'{-v,--verbose}'[Log output to STDOUT rather than a logfile]' \
'-D=-[pass through switches to the perl Configure script]' \
'(-as)'-as+'[Install a given perl under an alias.]:alias name' \
'1: :->cmds' \
'(1 *): :->args' && return 0
case $state in
cmds)
local -a cmds
cmds=( init install switch list use off help mirror version )
_describe -t commands 'perlbrew command' cmds && ret=0
;;
args)
case $line[1] in
switch | use)
local -a versions
versions=(system $(_call_program commands perlbrew list 2>/dev/null | perl -ne '!m{\*|^\s*/} && s/\n/ / && print'))
_wanted versions expl 'perl version' compadd $versions && ret=0
;;
install)
if ( [[ ${+_perlbrew_installs} -eq 0 ]] || _cache_invalid PERLBREW_installs ) && ! _retrieve_cache PERLBREW_installs; then
_perlbrew_installs=($(_call_program commands curl -L0 http://search.cpan.org/dist/perl/ 2>/dev/null |perl -ne 'm{^\s*<option value="[^"]+/(perl-[^"]+)/">} && print "$1\n"'))
_store_cache PERLBREW_installs _perlbrew_installs
fi
_wanted releases expl 'perl release' compadd -a _perlbrew_installs && ret=0
;;
*)
(( ret )) && _message 'no more arguments'
;;
esac
;;
esac
return ret
あとついでに、install候補も補完できるようにしときました。(※CPANからリストを取ってくるので、最初の補完には時間がかかりますが(>_<;))
これで、いちいちバージョンを調べなくても気軽にいろんなperlを試せますね!
zshの補完関数はじめて書いたけど、こんなんでいいのかな?
3/11追記 App-perlbrew-0.17にアップデートしたら、switch候補がちゃんと動かなくなったぽいので、修正しました。