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
|
package cmd
import (
"errors"
"strings"
)
type vim struct{}
// Vim adds support for vim. Not really a shell but it's handy.
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) (string, error) {
var out string
for key, value := range e {
if value == nil {
out += sh.unset(key)
} else {
out += sh.export(key, *value)
}
}
return out, nil
}
func (sh vim) Dump(env Env) (string, error) {
var out string
for key, value := range env {
out += sh.export(key, value)
}
return out, nil
}
func (sh vim) export(key, value string) string {
return "call setenv(" + sh.escapeKey(key) + "," + sh.escapeValue(value) + ")\n"
}
func (sh vim) unset(key string) string {
return "call setenv(" + sh.escapeKey(key) + ",v:null)\n"
}
// TODO: support keys with special chars or fail
func (sh vim) escapeKey(str string) string {
return sh.escapeValue(str)
}
// TODO: Make sure this escaping is valid
func (sh vim) escapeValue(str string) string {
replacer := strings.NewReplacer(
"\n", "\\n",
"'", "''",
)
return "'" + replacer.Replace(str) + "'"
}
|