mirror of
https://gitlab.com/pnx/gosh
synced 2026-06-15 23:03:09 +02:00
26 lines
530 B
Go
26 lines
530 B
Go
package paths
|
|
|
|
import (
|
|
"os"
|
|
"strings"
|
|
)
|
|
|
|
// HomeDir returns the current user's home directory.
|
|
// If it cannot be determined, returns an empty string.
|
|
func HomeDir() string {
|
|
home, _ := os.UserHomeDir()
|
|
return home
|
|
}
|
|
|
|
// AbbreviateHome replaces the user's home directory prefix with '~'
|
|
// to produce a shorter, more readable path for display.
|
|
func AbbreviateHome(p string) string {
|
|
home := HomeDir()
|
|
if home == "" {
|
|
return p
|
|
}
|
|
if after, found := strings.CutPrefix(p, home); found {
|
|
return "~" + after
|
|
}
|
|
return p
|
|
}
|