diff --git a/README.adoc b/README.adoc index cedc4b9..8f6ba2a 100644 --- a/README.adoc +++ b/README.adoc @@ -150,6 +150,13 @@ $ go install github.com/jiro4989/ojosama/cmd/ojosama@latest $ ojosama -completions bash | sudo tee /usr/share/bash-completion/completions/ojosama ---- +==== Zsh + +[source,bash] +---- +$ ojosama -completions zsh | sudo tee /usr/share/zsh/functions/Completion/_ojosama +---- + == 注意事項 === 変換結果について diff --git a/cmd/ojosama/cli.go b/cmd/ojosama/cli.go index c87f974..27cfa15 100644 --- a/cmd/ojosama/cli.go +++ b/cmd/ojosama/cli.go @@ -16,15 +16,24 @@ type CmdArgs struct { Args []string } +const ( + helpMsgHelp = "print help" + helpMsgText = "input text" + helpMsgOutFile = "output file" + helpMsgCharCode = "input text file encoding. default is utf8. (utf8, sjis)" + helpMsgVersion = "print version" + helpMsgCompletions = "print completions file. (bash, zsh)" +) + func ParseArgs() (*CmdArgs, error) { opts := CmdArgs{} flag.Usage = flagHelpMessage - flag.StringVar(&opts.Text, "t", "", "input text") - flag.StringVar(&opts.OutFile, "o", "", "output file") - flag.StringVar(&opts.CharCode, "charcode", "utf8", "input text file encoding. (utf8 or sjis)") - flag.BoolVar(&opts.Version, "v", false, "print version") - flag.StringVar(&opts.Completions, "completions", "", "print completions file. (bash)") + flag.StringVar(&opts.Text, "t", "", helpMsgText) + flag.StringVar(&opts.OutFile, "o", "", helpMsgOutFile) + flag.StringVar(&opts.CharCode, "charcode", "utf8", helpMsgCharCode) + flag.BoolVar(&opts.Version, "v", false, helpMsgVersion) + flag.StringVar(&opts.Completions, "completions", "", helpMsgCompletions) flag.Parse() opts.Args = flag.Args() diff --git a/cmd/ojosama/completions.go b/cmd/ojosama/completions.go index e392295..a222370 100644 --- a/cmd/ojosama/completions.go +++ b/cmd/ojosama/completions.go @@ -27,7 +27,7 @@ _{{APPNAME}}_module() { COMPREPLY=($(compgen -W "${opts}" -- "${cur}")) ;; -completions) - local opts="bash" + local opts="bash zsh" COMPREPLY=($(compgen -W "${opts}" -- "${cur}")) ;; esac @@ -37,12 +37,42 @@ _{{APPNAME}}_module() { complete -F _{{APPNAME}}_module {{APPNAME}}`, "{{APPNAME}}", appName) + completionsZsh = strings.ReplaceAll(`#compdef {{APPNAME}} + +_{{APPNAME}}() { + _arguments \ + {-h,-help}'[`+helpMsgHelp+`]: :->etc' \ + -t'[`+helpMsgText+`]: :->etc' \ + -o'[`+helpMsgOutFile+`]:file:_files' \ + -charcode'[`+helpMsgCharCode+`]: :->charcode' \ + -v'[`+helpMsgVersion+`]: :->etc' \ + -completions'[`+helpMsgCompletions+`]: :->completions' + + case "$state" in + charcode) + _values 'charcode' utf8 sjis + ;; + completions) + _values 'completions' bash zsh + ;; + etc) + # nothing to do + ;; + esac +} + +compdef _{{APPNAME}} {{APPNAME}} + +# vim: ft=zsh`, "{{APPNAME}}", appName) + completionsMap = map[string]string{ "bash": completionsBash, + "zsh": completionsZsh, } ) func isSupportedCompletions(sh string) bool { + sh = strings.ToLower(sh) _, ok := completionsMap[sh] return ok }