forked from direnv/direnv
-
Notifications
You must be signed in to change notification settings - Fork 0
/
shell_vim.go
51 lines (41 loc) · 1.05 KB
/
shell_vim.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
package main
import (
"errors"
"strings"
)
type vim struct{}
// Vim adds support for vim. Not really a shell but it's handly.
var Vim Shell = vim{}
func (sh vim) Hook() (string, error) {
return "", errors.New("this feature is not supported. Install the direnv.vim plugin instead")
}
func (sh vim) Export(e ShellExport) (out string) {
for key, value := range e {
if value == nil {
out += sh.unset(key)
} else {
out += sh.export(key, *value)
}
}
return out
}
func (sh vim) Dump(env Env) (out string) {
for key, value := range env {
out += sh.export(key, value)
}
return out
}
func (sh vim) export(key, value string) string {
return "let $" + sh.escapeKey(key) + " = " + sh.escapeValue(value) + "\n"
}
func (sh vim) unset(key string) string {
return "let $" + sh.escapeKey(key) + " = ''\n"
}
// TODO: support keys with special chars or fail
func (sh vim) escapeKey(str string) string {
return str
}
// TODO: Make sure this escaping is valid
func (sh vim) escapeValue(str string) string {
return "'" + strings.Replace(str, "'", "''", -1) + "'"
}