Skip to content

Commit

Permalink
add zsh support
Browse files Browse the repository at this point in the history
  • Loading branch information
AmirMohammadFakhimi committed Jul 28, 2023
1 parent afa891b commit 8b08765
Show file tree
Hide file tree
Showing 2 changed files with 53 additions and 2 deletions.
26 changes: 25 additions & 1 deletion core/commands/commands.go
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ import (
"sort"
"strings"

"github.com/ipfs/go-ipfs-cmds"
cmds "github.com/ipfs/go-ipfs-cmds"
)

type commandEncoder struct {
Expand Down Expand Up @@ -169,6 +169,30 @@ To install the completions permanently, they can be moved to
return res.Emit(&buf)
},
},
"zsh": {
Helptext: cmds.HelpText{
Tagline: "Generate zsh shell completions.",
ShortDescription: "Generates command completions for the zsh shell.",
LongDescription: `
Generates command completions for the zsh shell.
The simplest way to see it working is write the completions
to a file and then source it:
> ipfs commands completion zsh > ipfs-completion.zsh
> source ./ipfs-completion.zsh
To install the completions permanently, they can be moved to
/etc/zsh/completions or sourced from your ~/.zshrc file.
`,
},
NoRemote: true,
Run: func(req *cmds.Request, res cmds.ResponseEmitter, env cmds.Environment) error {
var buf bytes.Buffer
if err := writeZshCompletions(root, &buf); err != nil {
return err
}
res.SetLength(uint64(buf.Len()))
return res.Emit(&buf)
},
},
"fish": {
Helptext: cmds.HelpText{
Tagline: "Generate fish shell completions.",
Expand Down
29 changes: 28 additions & 1 deletion core/commands/completion.go
Original file line number Diff line number Diff line change
Expand Up @@ -83,7 +83,7 @@ func commandToCompletions(name string, fullName string, cmd *cmds.Command) *comp
return parsed
}

var bashCompletionTemplate, fishCompletionTemplate *template.Template
var bashCompletionTemplate, fishCompletionTemplate, zshCompletionTemplate *template.Template

func init() {
commandTemplate := template.Must(template.New("command").Parse(`
Expand Down Expand Up @@ -153,6 +153,28 @@ _ipfs() {
{{ template "command" . }}
}
complete -o nosort -o nospace -o default -F _ipfs ipfs
`))

zshCompletionTemplate = template.Must(commandTemplate.New("root").Parse(`#!bin/zsh
autoload bashcompinit
bashcompinit
_ipfs_compgen() {
local oldifs="$IFS"
IFS=$'\n'
while read -r line; do
COMPREPLY+=("$line")
done < <(compgen "$@")
IFS="$oldifs"
}
_ipfs() {
COMPREPLY=()
local index=1
local argidx=0
local word="${COMP_WORDS[COMP_CWORD]}"
{{ template "command" . }}
}
complete -o nosort -o nospace -o default -F _ipfs ipfs
`))

fishCommandTemplate := template.Must(template.New("command").Parse(`
Expand Down Expand Up @@ -222,3 +244,8 @@ func writeFishCompletions(cmd *cmds.Command, out io.Writer) error {
cmds := commandToCompletions("ipfs", "", cmd)
return fishCompletionTemplate.Execute(out, cmds)
}

func writeZshCompletions(cmd *cmds.Command, out io.Writer) error {
cmds := commandToCompletions("ipfs", "", cmd)
return zshCompletionTemplate.Execute(out, cmds)
}

0 comments on commit 8b08765

Please sign in to comment.