refactor: move out home abbreviating code to a util function

fg-job
TorchedSammy 2022-05-17 21:37:42 -04:00
parent ab8b9c8376
commit 8b6506b36c
Signed by: sammyette
GPG Key ID: 904FC49417B44DCD
2 changed files with 10 additions and 3 deletions

View File

@ -240,9 +240,7 @@ func fmtPrompt(prompt string) string {
host, _ := os.Hostname() host, _ := os.Hostname()
cwd, _ := os.Getwd() cwd, _ := os.Getwd()
if strings.HasPrefix(cwd, curuser.HomeDir) { cwd = util.AbbrevHome(cwd)
cwd = "~" + strings.TrimPrefix(cwd, curuser.HomeDir)
}
username := curuser.Username username := curuser.Username
// this will be baked into binary since GOOS is a constant // this will be baked into binary since GOOS is a constant
if runtime.GOOS == "windows" { if runtime.GOOS == "windows" {

View File

@ -166,3 +166,12 @@ func ExpandHome(path string) string {
return path 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
}