New builtin "tilde-abbr".

This commit is contained in:
Qi Xiao 2016-02-23 13:29:10 +01:00
parent 67e4b5df11
commit 6b625c0e8d
2 changed files with 16 additions and 4 deletions

View File

@ -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 {

View File

@ -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
}