mirror of
https://github.com/go-sylixos/elvish.git
synced 2024-12-12 17:27:50 +08:00
30 lines
508 B
Go
30 lines
508 B
Go
package util
|
|
|
|
import (
|
|
"os"
|
|
"strings"
|
|
)
|
|
|
|
// 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 err == nil {
|
|
if path == home {
|
|
return "~"
|
|
} else if strings.HasPrefix(path, home+"/") {
|
|
return "~" + path[len(home):]
|
|
}
|
|
}
|
|
return path
|
|
}
|