refactor: move out home abbreviating code to a util function

fg-job
TorchedSammy 9 months ago
parent ab8b9c8376
commit 8b6506b36c
Signed by: sammyette
GPG Key ID: 904FC49417B44DCD

@ -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" {

@ -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
}

Loading…
Cancel
Save