File: zsh.go

package info (click to toggle)
golang-github-posener-complete 1.2.3-2
  • links: PTS, VCS
  • area: main
  • in suites: bookworm, bullseye, forky, sid, trixie
  • size: 228 kB
  • sloc: makefile: 4
file content (44 lines) | stat: -rw-r--r-- 1,033 bytes parent folder | download
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
package install

import "fmt"

// (un)install in zsh
// basically adds/remove from .zshrc:
//
// autoload -U +X bashcompinit && bashcompinit"
// complete -C </path/to/completion/command> <command>
type zsh struct {
	rc string
}

func (z zsh) IsInstalled(cmd, bin string) bool {
	completeCmd := z.cmd(cmd, bin)
	return lineInFile(z.rc, completeCmd)
}

func (z zsh) Install(cmd, bin string) error {
	if z.IsInstalled(cmd, bin) {
		return fmt.Errorf("already installed in %s", z.rc)
	}

	completeCmd := z.cmd(cmd, bin)
	bashCompInit := "autoload -U +X bashcompinit && bashcompinit"
	if !lineInFile(z.rc, bashCompInit) {
		completeCmd = bashCompInit + "\n" + completeCmd
	}

	return appendToFile(z.rc, completeCmd)
}

func (z zsh) Uninstall(cmd, bin string) error {
	if !z.IsInstalled(cmd, bin) {
		return fmt.Errorf("does not installed in %s", z.rc)
	}

	completeCmd := z.cmd(cmd, bin)
	return removeFromFile(z.rc, completeCmd)
}

func (zsh) cmd(cmd, bin string) string {
	return fmt.Sprintf("complete -o nospace -C %s %s", bin, cmd)
}