gt/internal/dirs/dirs.go

30 lines
433 B
Go
Raw Normal View History

package dirs
import (
"os"
"path/filepath"
"strings"
)
// UnExpand unexpands some directory shortcuts
//
// $HOME -> ~
func UnExpand(dir string) (outdir string) {
var (
home = os.Getenv("HOME")
2024-06-19 19:35:40 -04:00
pwd string
err error
)
outdir = filepath.Clean(dir)
2024-06-19 19:35:40 -04:00
pwd, err = os.Getwd()
if err == nil && home != pwd {
outdir = strings.Replace(outdir, pwd, "$PWD", 1)
2024-06-19 19:35:40 -04:00
}
outdir = strings.Replace(outdir, home, "~", 1)
return
}