Initial commit
This commit is contained in:
commit
9d9d8ce7d5
19 changed files with 399 additions and 0 deletions
20
internal/paths/expand.go
Normal file
20
internal/paths/expand.go
Normal file
|
|
@ -0,0 +1,20 @@
|
|||
package paths
|
||||
|
||||
import (
|
||||
"path/filepath"
|
||||
)
|
||||
|
||||
// Expand resolves shell-like shortcuts in a path string:
|
||||
// - empty string resolves to the user's home directory
|
||||
// - leading '~' expands to the user's home directory
|
||||
func Expand(dir string) string {
|
||||
// Empty string resolves to the users home dir.
|
||||
if len(dir) < 1 {
|
||||
return HomeDir()
|
||||
}
|
||||
// Expand ~ to users home dir.
|
||||
if dir[0] == '~' {
|
||||
return filepath.Join(HomeDir(), dir[1:])
|
||||
}
|
||||
return dir
|
||||
}
|
||||
26
internal/paths/home.go
Normal file
26
internal/paths/home.go
Normal file
|
|
@ -0,0 +1,26 @@
|
|||
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
|
||||
}
|
||||
Loading…
Add table
Add a link
Reference in a new issue