Skip to content

Commit

Permalink
✨ZshCompletionsに対応いたしましたわ~💯 #73 (#76)
Browse files Browse the repository at this point in the history
* add zsh completions

* fix

* fix

* readme

* fix

* fix
  • Loading branch information
jiro4989 authored Aug 8, 2022
1 parent 783e8fe commit a171791
Show file tree
Hide file tree
Showing 3 changed files with 52 additions and 6 deletions.
7 changes: 7 additions & 0 deletions README.adoc
Original file line number Diff line number Diff line change
Expand Up @@ -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
----

== 注意事項

=== 変換結果について
Expand Down
19 changes: 14 additions & 5 deletions cmd/ojosama/cli.go
Original file line number Diff line number Diff line change
Expand Up @@ -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()

Expand Down
32 changes: 31 additions & 1 deletion cmd/ojosama/completions.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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
}
Expand Down

0 comments on commit a171791

Please sign in to comment.