diff --git a/eval/builtin_func.go b/eval/builtin_func.go index de530102..e1f10af1 100644 --- a/eval/builtin_func.go +++ b/eval/builtin_func.go @@ -96,6 +96,8 @@ func init() { &BuiltinFn{"fg", wrapFn(fg)}, + &BuiltinFn{"tilde-abbr", wrapFn(tildeAbbr)}, + &BuiltinFn{"-sleep", wrapFn(_sleep)}, &BuiltinFn{"-stack", wrapFn(_stack)}, &BuiltinFn{"-log", wrapFn(_log)}, @@ -639,6 +641,11 @@ func fg(ec *EvalCtx, pids ...int) { throwCompositeError(errors) } +func tildeAbbr(ec *EvalCtx, path string) { + out := ec.ports[1].Chan + out <- String(util.TildeAbbr(path)) +} + func _sleep(ec *EvalCtx, t float64) { d := time.Duration(float64(time.Second) * t) select { diff --git a/util/getwd.go b/util/getwd.go index f81377fd..ed673e03 100644 --- a/util/getwd.go +++ b/util/getwd.go @@ -12,13 +12,18 @@ func Getwd() string { if err != nil { return "?" } + return TildeAbbr(pwd) +} + +// TildeAbbr abbreviates the user's home directory to ~. +func TildeAbbr(path string) string { home, err := GetHome("") if err == nil { - if pwd == home { + if path == home { return "~" - } else if strings.HasPrefix(pwd, home+"/") { - return "~" + pwd[len(home):] + } else if strings.HasPrefix(path, home+"/") { + return "~" + path[len(home):] } } - return pwd + return path }