github.com/hoop33/elvish@v0.0.0-20160801152013-6d25485beab4/util/getwd.go (about) 1 package util 2 3 import ( 4 "os" 5 "strings" 6 ) 7 8 // Getwd returns path of the working directory in a format suitable as the 9 // prompt. 10 func Getwd() string { 11 pwd, err := os.Getwd() 12 if err != nil { 13 return "?" 14 } 15 return TildeAbbr(pwd) 16 } 17 18 // TildeAbbr abbreviates the user's home directory to ~. 19 func TildeAbbr(path string) string { 20 home, err := GetHome("") 21 if err == nil { 22 if path == home { 23 return "~" 24 } else if strings.HasPrefix(path, home+"/") { 25 return "~" + path[len(home):] 26 } 27 } 28 return path 29 }