From 8b6506b36c3c5541b390e41f63a161b23a64fda0 Mon Sep 17 00:00:00 2001 From: TorchedSammy <38820196+TorchedSammy@users.noreply.github.com> Date: Tue, 17 May 2022 21:37:42 -0400 Subject: [PATCH] refactor: move out home abbreviating code to a util function --- main.go | 4 +--- util/util.go | 9 +++++++++ 2 files changed, 10 insertions(+), 3 deletions(-) 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 +}