gt/internal/dirs/dirs.go

36 lines
574 B
Go
Raw Normal View History

package dirs
import (
"os"
"path/filepath"
"strings"
)
// UnExpand unexpands some directory shortcuts
//
// $HOME -> ~
2024-06-20 01:21:35 -04:00
// $PWD -> .
// workdir -> .
2024-07-15 18:58:57 -04:00
func UnExpand(dir string) (outdir string) {
outdir = filepath.Clean(dir)
home := os.Getenv("HOME")
2024-06-19 19:35:40 -04:00
if pwd, err := os.Getwd(); err == nil && home != pwd {
2024-06-20 01:21:35 -04:00
outdir = strings.Replace(outdir, pwd, ".", 1)
2024-06-19 19:35:40 -04:00
}
outdir = strings.Replace(outdir, home, "~", 1)
outdir = UnEscape(outdir)
if outdir == "" {
outdir = "/"
}
return
}
func UnEscape(input string) string {
return strings.ReplaceAll(input, "%20", " ")
}