home.go

 1// Package home provides utilities for dealing with the user's home directory.
 2package home
 3
 4import (
 5	"os"
 6	"path/filepath"
 7	"strings"
 8)
 9
10// Dir returns the user home directory.
11func Dir() string {
12	home, _ := os.UserHomeDir()
13	return home
14}
15
16// Short replaces the actual home path from [Dir] with `~`.
17func Short(p string) string {
18	if !strings.HasPrefix(p, Dir()) || Dir() == "" {
19		return p
20	}
21	return filepath.Join("~", strings.TrimPrefix(p, Dir()))
22}
23
24// Long replaces the `~` with actual home path from [Dir].
25func Long(p string) string {
26	if !strings.HasPrefix(p, "~") || Dir() == "" {
27		return p
28	}
29	return strings.Replace(p, "~", Dir(), 1)
30}