-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathsetup_zsh.go
83 lines (69 loc) · 1.34 KB
/
setup_zsh.go
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
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
package main
import (
"fmt"
"os"
)
const ZSH = `#compdef $PROG
_cli_zsh_autocomplete() {
local -a opts
local cur
cur=${words[-1]}
if [[ "$cur" == "-"* ]]; then
opts=("${(@f)$(_CLI_ZSH_AUTOCOMPLETE_HACK=1 ${words[@]:0:#words[@]-1} ${cur} --generate-bash-completion)}")
else
opts=("${(@f)$(_CLI_ZSH_AUTOCOMPLETE_HACK=1 ${words[@]:0:#words[@]-1} --generate-bash-completion)}")
fi
if [[ "${opts[1]}" != "" ]]; then
_describe 'values' opts
else
_files
fi
return
}
compdef _cli_zsh_autocomplete $PROG`
const ZSH_RC = `
PROG=narwhal
_CLI_ZSH_AUTOCOMPLETE_HACK=1
source ~/.narwhalrc
PROG=nw
source ~/.narwhalrc
`
func setupZSH() error {
err := write(".narwhalrc", BASH, true)
if err != nil {
return err
}
err = clearExistingZsh()
if err != nil {
return err
}
err = appendTo(".zshrc", ZSH_RC, true)
if err != nil {
return err
}
fmt.Println("Please either restart your shell or run:")
fmt.Println("\tsource ~/.zshrc")
return nil
}
func clearExistingZsh() error {
profile, err := homeFile(".zshrc")
if err != nil {
return err
}
return removeFromFile(profile, ZSH_RC)
}
func tearDownZsh() error {
file, err := homeFile(".narwhalrc")
if err != nil {
return err
}
err = os.Remove(file)
if err != nil {
fmt.Println(err)
}
err = clearExistingZsh()
if err != nil {
return err
}
return nil
}