diff --git a/main.go b/main.go index 669313a..6fcb7a7 100644 --- a/main.go +++ b/main.go @@ -240,9 +240,7 @@ func fmtPrompt(prompt string) string { host, _ := os.Hostname() cwd, _ := os.Getwd() - if strings.HasPrefix(cwd, curuser.HomeDir) { - cwd = "~" + strings.TrimPrefix(cwd, curuser.HomeDir) - } + cwd = util.AbbrevHome(cwd) username := curuser.Username // this will be baked into binary since GOOS is a constant if runtime.GOOS == "windows" { diff --git a/util/util.go b/util/util.go index 713ea6a..d27cfe1 100644 --- a/util/util.go +++ b/util/util.go @@ -166,3 +166,12 @@ func ExpandHome(path string) string { return path } +// AbbrevHome changes the user's home directory in the path string to ~ (tilde) +func AbbrevHome(path string) string { + curuser, _ := user.Current() + if strings.HasPrefix(path, curuser.HomeDir) { + return "~" + strings.TrimPrefix(path, curuser.HomeDir) + } + + return path +}