github.com/ladydascalie/elvish@v0.0.0-20170703214355-2964dd3ece7f/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  }