liuxiaolong
2022-06-28 37714b1093c04061e636e5b1d27179652e671c0a
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
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) Install(cmd, bin string) error {
    completeCmd := z.cmd(cmd, bin)
    if lineInFile(z.rc, completeCmd) {
        return fmt.Errorf("already installed in %s", z.rc)
    }
 
    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 {
    completeCmd := z.cmd(cmd, bin)
    if !lineInFile(z.rc, completeCmd) {
        return fmt.Errorf("does not installed in %s", z.rc)
    }
 
    return removeFromFile(z.rc, completeCmd)
}
 
func (zsh) cmd(cmd, bin string) string {
    return fmt.Sprintf("complete -o nospace -C %s %s", bin, cmd)
}