-
Notifications
You must be signed in to change notification settings - Fork 0
/
getwd.go
38 lines (34 loc) · 780 Bytes
/
getwd.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
package fsutil
import (
"os"
"path/filepath"
"strings"
)
var pathSep = string(filepath.Separator)
// Getwd returns path of the working directory in a format suitable as the
// prompt.
func Getwd() string {
pwd, err := os.Getwd()
if err != nil {
return "?"
}
return TildeAbbr(pwd)
}
// TildeAbbr abbreviates the user's home directory to ~.
func TildeAbbr(path string) string {
home, err := GetHome("")
if home == "" || home == "/" {
// If home is "" or "/", do not abbreviate because (1) it is likely a
// problem with the environment and (2) it will make the path actually
// longer.
return path
}
if err == nil {
if path == home {
return "~"
} else if strings.HasPrefix(path, home+pathSep) {
return "~" + path[len(home):]
}
}
return path
}