mirror of
https://github.com/go-sylixos/elvish.git
synced 2024-12-04 10:57: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{"fg", wrapFn(fg)},
|
||||||
|
|
||||||
|
&BuiltinFn{"tilde-abbr", wrapFn(tildeAbbr)},
|
||||||
|
|
||||||
&BuiltinFn{"-sleep", wrapFn(_sleep)},
|
&BuiltinFn{"-sleep", wrapFn(_sleep)},
|
||||||
&BuiltinFn{"-stack", wrapFn(_stack)},
|
&BuiltinFn{"-stack", wrapFn(_stack)},
|
||||||
&BuiltinFn{"-log", wrapFn(_log)},
|
&BuiltinFn{"-log", wrapFn(_log)},
|
||||||
|
@ -639,6 +641,11 @@ func fg(ec *EvalCtx, pids ...int) {
|
||||||
throwCompositeError(errors)
|
throwCompositeError(errors)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func tildeAbbr(ec *EvalCtx, path string) {
|
||||||
|
out := ec.ports[1].Chan
|
||||||
|
out <- String(util.TildeAbbr(path))
|
||||||
|
}
|
||||||
|
|
||||||
func _sleep(ec *EvalCtx, t float64) {
|
func _sleep(ec *EvalCtx, t float64) {
|
||||||
d := time.Duration(float64(time.Second) * t)
|
d := time.Duration(float64(time.Second) * t)
|
||||||
select {
|
select {
|
||||||
|
|
|
@ -12,13 +12,18 @@ func Getwd() string {
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return "?"
|
return "?"
|
||||||
}
|
}
|
||||||
|
return TildeAbbr(pwd)
|
||||||
|
}
|
||||||
|
|
||||||
|
// TildeAbbr abbreviates the user's home directory to ~.
|
||||||
|
func TildeAbbr(path string) string {
|
||||||
home, err := GetHome("")
|
home, err := GetHome("")
|
||||||
if err == nil {
|
if err == nil {
|
||||||
if pwd == home {
|
if path == home {
|
||||||
return "~"
|
return "~"
|
||||||
} else if strings.HasPrefix(pwd, home+"/") {
|
} else if strings.HasPrefix(path, home+"/") {
|
||||||
return "~" + pwd[len(home):]
|
return "~" + path[len(home):]
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
return pwd
|
return path
|
||||||
}
|
}
|
||||||
|
|
Loading…
Reference in New Issue
Block a user