-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathcompletion_fish.go
More file actions
55 lines (43 loc) · 1.23 KB
/
completion_fish.go
File metadata and controls
55 lines (43 loc) · 1.23 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
package cli
import (
"context"
"fmt"
)
// FishCompletion implements fish shell completion
type FishCompletion struct{}
func (f *FishCompletion) GetCompletions(cmd *Command, args []string) []string {
return getCompletionWords(cmd)
}
func (f *FishCompletion) Register(cmd *Command) {
fishCmd := Cmd("__fishcomplete").
Description("Fish completion helper").
Hidden().
Action(func(ctx context.Context, fishCommand *Command) error {
targetCmd := fishCommand.GetParent()
// For completion, we don't need args since we complete the parent
words := f.GetCompletions(targetCmd, nil)
for _, word := range words {
fmt.Println(word)
}
return nil
})
cmd.AddCommand(fishCmd)
// Recursively register for all subcommands
for _, subcmd := range cmd.GetCommands() {
if !subcmd.IsHidden() {
f.Register(subcmd)
}
}
}
func (f *FishCompletion) GenerateScript(cmd *Command) string {
cmdName := cmd.GetName()
script := fmt.Sprintf(`# Fish completion script for %s
# Save this to ~/.config/fish/completions/%s.fish
function __%s_complete
set -l cmd_path (commandline -opc)
$cmd_path __fishcomplete 2>/dev/null
end
complete -c %s -f -a "(__%s_complete)"
`, cmdName, cmdName, cmdName, cmdName, cmdName)
return script
}