elvish/edit/prompt.go

63 lines
1.5 KiB
Go
Raw Normal View History

package edit
import (
"os"
"os/user"
"github.com/elves/elvish/eval"
"github.com/elves/elvish/util"
)
var _ = registerVariable("prompt", promptVariable)
func promptVariable() eval.Variable {
prompt := func(ec *eval.EvalCtx,
args []eval.Value, opts map[string]eval.Value) {
2016-09-08 06:16:39 +08:00
out := ec.OutputChan()
out <- &styled{util.Getwd() + "> ", styles{}}
2016-09-08 06:16:39 +08:00
}
return eval.NewPtrVariableWithValidator(
&eval.BuiltinFn{"default prompt", prompt}, eval.ShouldBeFn)
}
func (ed *Editor) prompt() eval.Callable {
return ed.variables["prompt"].Get().(eval.Callable)
}
var _ = registerVariable("rprompt", rpromptVariable)
func rpromptVariable() eval.Variable {
2016-09-08 06:16:39 +08:00
username := "???"
user, err := user.Current()
if err == nil {
username = user.Username
}
hostname, err := os.Hostname()
if err != nil {
hostname = "???"
}
rpromptStr := username + "@" + hostname
rprompt := func(ec *eval.EvalCtx,
args []eval.Value, opts map[string]eval.Value) {
2016-09-08 06:16:39 +08:00
out := ec.OutputChan()
out <- &styled{rpromptStr, styles{"7"}}
2016-09-08 06:16:39 +08:00
}
return eval.NewPtrVariableWithValidator(
&eval.BuiltinFn{"default rprompt", rprompt}, eval.ShouldBeFn)
}
func (ed *Editor) rprompt() eval.Callable {
return ed.variables["rprompt"].Get().(eval.Callable)
}
var _ = registerVariable("rprompt-persistent", func() eval.Variable {
return eval.NewPtrVariableWithValidator(eval.Bool(false), eval.ShouldBeBool)
})
func (ed *Editor) rpromptPersistent() bool {
return bool(ed.variables["rprompt-persistent"].Get().(eval.Bool).Bool())
}