replace workdir with /

and do it right this time

I hope...
This commit is contained in:
Lilian Jónsdóttir 2024-07-15 17:10:38 -07:00
parent f193d0f5b3
commit 30b605a230
2 changed files with 32 additions and 10 deletions

View file

@ -6,16 +6,27 @@ import (
"strings"
)
const sep = string(os.PathSeparator)
// UnExpand unexpands some directory shortcuts
//
// $HOME -> ~
// $PWD -> .
// workdir -> .
func UnExpand(dir string) (outdir string) {
outdir = filepath.Clean(dir)
home := os.Getenv("HOME")
// workdir -> /
func UnExpand(dir, workdir string) (outdir string) {
var (
home string = os.Getenv("HOME")
pwd, _ = os.Getwd()
)
if pwd, err := os.Getwd(); err == nil && home != pwd {
if dir != "" {
outdir = cleanDir(dir, pwd)
}
if workdir != "" {
workdir = cleanDir(workdir, pwd)
outdir = strings.Replace(outdir, workdir, "", 1)
} else if home != pwd && pwd != "" {
outdir = strings.Replace(outdir, pwd, ".", 1)
}
@ -30,6 +41,17 @@ func UnExpand(dir string) (outdir string) {
return
}
func cleanDir(dir, pwd string) (out string) {
if strings.HasPrefix(dir, ".") {
out = filepath.Clean(dir)
} else if !strings.HasPrefix(dir, sep) {
out = filepath.Join(pwd, dir)
} else {
out = dir
}
return
}
func UnEscape(input string) string {
return strings.ReplaceAll(input, "%20", " ")
}