Only replace '~' with home if at start of path

Ref #757
This commit is contained in:
Zachary Yedidia
2017-09-23 20:56:08 -04:00
parent 5a7ddb8330
commit 12a4dd58f3
5 changed files with 23 additions and 22 deletions

View File

@@ -12,6 +12,7 @@ import (
"unicode/utf8"
"github.com/mattn/go-runewidth"
homedir "github.com/mitchellh/go-homedir"
)
// Util.go is a collection of utility functions that are used throughout
@@ -363,3 +364,18 @@ func JoinCommandArgs(args ...string) string {
return buf.String()
}
// ReplaceHome takes a path as input and replaces ~ at the start of the path with the user's
// home directory. Does nothing if the path does not start with '~'.
func ReplaceHome(path string) string {
if !strings.HasPrefix(path, "~") {
return path
}
home, err := homedir.Dir()
if err != nil {
messenger.Error("Could not find home directory: ", err)
return path
}
return strings.Replace(path, "~", home, 1)
}