From 6b625c0e8dedb8c38e251df7d11ce215b9cc1fdb Mon Sep 17 00:00:00 2001 From: Qi Xiao Date: Tue, 23 Feb 2016 13:29:10 +0100 Subject: [PATCH] New builtin "tilde-abbr". --- eval/builtin_func.go | 7 +++++++ util/getwd.go | 13 +++++++++---- 2 files changed, 16 insertions(+), 4 deletions(-) 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 }