From 1558e2016ceb11a8bdb5daf07657629fbf798178 Mon Sep 17 00:00:00 2001 From: Francesco Camuffo Date: Wed, 7 Apr 2021 20:23:43 +0200 Subject: [PATCH] Add cwd.go --- cwd.go | 38 ++++++++++++++++++++++++++++++++++++++ misc.go | 31 ------------------------------- 2 files changed, 38 insertions(+), 31 deletions(-) create mode 100644 cwd.go diff --git a/cwd.go b/cwd.go new file mode 100644 index 0000000..89111d9 --- /dev/null +++ b/cwd.go @@ -0,0 +1,38 @@ +package main + +import ( + "os" + "os/user" + "strings" +) + +// CWD with contracted path +func segCwd() (string, error) { + wd, err := os.Getwd() + if err != nil { + return "", err + } + h := os.Getenv("HOME") + + if strings.HasPrefix(wd, h) { + wd = "~" + wd[len(h):] + } + ss := strings.Split(wd, "/") + wd = "" + for i, s := range ss { + if i == len(ss) - 1 { + wd += s + } else { + if len(s) == 0 { + wd += "/" + continue + } + if s[0] == '.' { + wd += s[:2] + "/" + } else { + wd += string(s[0]) + "/" + } + } + } + return wd, nil +} diff --git a/misc.go b/misc.go index e2c526f..b9f2450 100644 --- a/misc.go +++ b/misc.go @@ -10,37 +10,6 @@ func segHostname() (string, error) { return os.Hostname() } -// CWD with contracted path -func segCwd() (string, error) { - wd, err := os.Getwd() - if err != nil { - return "", err - } - h := os.Getenv("HOME") - - if strings.HasPrefix(wd, h) { - wd = "~" + wd[len(h):] - } - ss := strings.Split(wd, "/") - wd = "" - for i, s := range ss { - if i == len(ss) - 1 { - wd += s - } else { - if len(s) == 0 { - wd += "/" - continue - } - if s[0] == '.' { - wd += s[:2] + "/" - } else { - wd += string(s[0]) + "/" - } - } - } - return wd, nil -} - func segTail() (string, error) { u, err := user.Current() if err != nil { -- 2.45.2