summaryrefslogtreecommitdiff
path: root/cmd/install/fish.go
diff options
context:
space:
mode:
authorYuki Ito <[email protected]>2018-01-08 17:51:45 +0900
committerYuki Ito <[email protected]>2018-01-08 17:51:45 +0900
commitfbb0b604547d5c4703f7e76fb41e65a090811557 (patch)
treeb630b54df28dbf85ac5ae694c9861a207402ee56 /cmd/install/fish.go
parent6bee943216c8cea4cc983c8596346d8945279a1f (diff)
Add support for fish
Diffstat (limited to 'cmd/install/fish.go')
-rw-r--r--cmd/install/fish.go50
1 files changed, 50 insertions, 0 deletions
diff --git a/cmd/install/fish.go b/cmd/install/fish.go
new file mode 100644
index 0000000..f04e7c3
--- /dev/null
+++ b/cmd/install/fish.go
@@ -0,0 +1,50 @@
+package install
+
+import (
+ "bytes"
+ "fmt"
+ "os"
+ "path/filepath"
+ "text/template"
+)
+
+// (un)install in fish
+
+type fish struct {
+ configDir string
+}
+
+func (f fish) Install(cmd, bin string) error {
+ completionFile := filepath.Join(f.configDir, "completions", fmt.Sprintf("%s.fish", cmd))
+ completeCmd := f.cmd(cmd, bin)
+ if _, err := os.Stat(completionFile); err == nil {
+ return fmt.Errorf("already installed at %s", completionFile)
+ }
+
+ return createFile(completionFile, completeCmd)
+}
+
+func (f fish) Uninstall(cmd, bin string) error {
+ completionFile := filepath.Join(f.configDir, "completions", fmt.Sprintf("%s.fish", cmd))
+ if _, err := os.Stat(completionFile); err != nil {
+ return fmt.Errorf("does not installed in %s", f.configDir)
+ }
+
+ return os.Remove(completionFile)
+}
+
+func (f fish) cmd(cmd, bin string) string {
+ var buf bytes.Buffer
+ params := struct{ Cmd, Bin string }{cmd, bin}
+ template.Must(template.New("cmd").Parse(`
+function __complete_{{.Cmd}}
+ set -lx COMP_LINE (string join ' ' (commandline -o))
+ test (commandline -ct) = ""
+ and set COMP_LINE "$COMP_LINE "
+ {{.Bin}}
+end
+complete -c {{.Cmd}} -a "(__complete_{{.Cmd}})"
+`)).Execute(&buf, params)
+
+ return string(buf.Bytes())
+}