gitlab.com/apertussolutions/u-root@v7.0.0+incompatible/cmds/core/elvish/util/getwd.go (about)

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