@@ -6,38 +6,33 @@ import (
66 "os"
77 "path/filepath"
88 "strings"
9− "sync"
109)
1110
12−// Dir returns the users home directory, or if it fails, tries to create a new
13−// temporary directory and use that instead.
14−var Dir = sync.OnceValue(func() string {
15− home, err := os.UserHomeDir()
16− if err == nil {
17− slog.Debug("user home directory", "home", home)
18− return home
19− }
20− tmp, err := os.MkdirTemp("crush", "")
21− if err != nil {
22− slog.Error("could not find the user home directory")
23− return ""
11+var homedir, homedirErr = os.UserHomeDir()
12+
13+func init() {
14+ if homedirErr != nil {
15+ slog.Error("failed to get user home directory", "error", homedirErr)
2416 }
25− slog.Warn("could not find the user home directory, using a temporary one", "home", tmp)
26− return tmp
27−})
17+}
18+
19+// Dir returns the user home directory.
20+func Dir() string {
21+ return homedir
22+}
2823
2924// Short replaces the actual home path from [Dir] with `~`.
3025func Short(p string) string {
31− if !strings.HasPrefix(p, Dir()) || Dir() == "" {
26+ if homedir == "" || !strings.HasPrefix(p, homedir) {
3227 return p
3328 }
34− return filepath.Join("~", strings.TrimPrefix(p, Dir()))
29+ return filepath.Join("~", strings.TrimPrefix(p, homedir))
3530}
3631
3732// Long replaces the `~` with actual home path from [Dir].
3833func Long(p string) string {
39− if !strings.HasPrefix(p, "~") || Dir() == "" {
34+ if homedir == "" || !strings.HasPrefix(p, "~") {
4035 return p
4136 }
42− return strings.Replace(p, "~", Dir(), 1)
37+ return strings.Replace(p, "~", homedir, 1)
4338}