2024-06-19 18:55:01 -04:00
|
|
|
package dirs
|
|
|
|
|
|
|
|
import (
|
|
|
|
"os"
|
|
|
|
"path/filepath"
|
|
|
|
"strings"
|
|
|
|
)
|
|
|
|
|
|
|
|
// UnExpand unexpands some directory shortcuts
|
|
|
|
//
|
|
|
|
// $HOME -> ~
|
2024-06-20 01:21:35 -04:00
|
|
|
// $PWD -> .
|
|
|
|
// workdir -> .
|
|
|
|
func UnExpand(dir, workdir string) (outdir string) {
|
2024-06-19 18:55:01 -04:00
|
|
|
var (
|
|
|
|
home = os.Getenv("HOME")
|
2024-06-19 19:35:40 -04:00
|
|
|
pwd string
|
|
|
|
err error
|
2024-06-19 18:55:01 -04:00
|
|
|
)
|
|
|
|
|
|
|
|
outdir = filepath.Clean(dir)
|
2024-06-19 19:35:40 -04:00
|
|
|
|
2024-06-20 01:21:35 -04:00
|
|
|
if workdir != "" {
|
|
|
|
outdir = strings.Replace(outdir, workdir, ".", 1)
|
|
|
|
}
|
|
|
|
|
2024-06-19 19:35:40 -04:00
|
|
|
pwd, err = os.Getwd()
|
2024-06-19 20:13:46 -04:00
|
|
|
if 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)
|
2024-06-19 18:55:01 -04:00
|
|
|
|
|
|
|
return
|
|
|
|
}
|