11 // (un)install in fish
17 func (f fish) Install(cmd, bin string) error {
18 completionFile := filepath.Join(f.configDir, "completions", fmt.Sprintf("%s.fish", cmd))
19 completeCmd, err := f.cmd(cmd, bin)
23 if _, err := os.Stat(completionFile); err == nil {
24 return fmt.Errorf("already installed at %s", completionFile)
27 return createFile(completionFile, completeCmd)
30 func (f fish) Uninstall(cmd, bin string) error {
31 completionFile := filepath.Join(f.configDir, "completions", fmt.Sprintf("%s.fish", cmd))
32 if _, err := os.Stat(completionFile); err != nil {
33 return fmt.Errorf("does not installed in %s", f.configDir)
36 return os.Remove(completionFile)
39 func (f fish) cmd(cmd, bin string) (string, error) {
41 params := struct{ Cmd, Bin string }{cmd, bin}
42 tmpl := template.Must(template.New("cmd").Parse(`
43 function __complete_{{.Cmd}}
44 set -lx COMP_LINE (string join ' ' (commandline -o))
45 test (commandline -ct) = ""
46 and set COMP_LINE "$COMP_LINE "
49 complete -c {{.Cmd}} -a "(__complete_{{.Cmd}})"
51 err := tmpl.Execute(&buf, params)
55 return buf.String(), nil