mirror of
https://github.com/go-sylixos/elvish.git
synced 2024-12-04 02:37:50 +08:00
New builtin "tilde-abbr".
This commit is contained in:
parent
67e4b5df11
commit
6b625c0e8d
|
@ -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 {
|
||||
|
|
|
@ -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
|
||||
}
|
||||
|
|
Loading…
Reference in New Issue
Block a user