2016-02-23 20:08:41 +08:00
|
|
|
package edit
|
|
|
|
|
|
|
|
import (
|
|
|
|
"os"
|
2016-02-23 20:24:28 +08:00
|
|
|
"os/user"
|
2016-02-23 20:08:41 +08:00
|
|
|
|
|
|
|
"github.com/elves/elvish/eval"
|
2016-02-23 20:24:28 +08:00
|
|
|
"github.com/elves/elvish/util"
|
2016-02-23 20:08:41 +08:00
|
|
|
)
|
|
|
|
|
2017-05-22 09:00:44 +08:00
|
|
|
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()
|
2017-01-11 12:14:19 +08:00
|
|
|
out <- &styled{util.Getwd() + "> ", styles{}}
|
2016-09-08 06:16:39 +08:00
|
|
|
}
|
2017-05-22 09:00:44 +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)
|
2016-02-23 20:08:41 +08:00
|
|
|
|
2017-05-22 09:00:44 +08:00
|
|
|
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
|
2017-05-22 09:00:44 +08:00
|
|
|
rprompt := func(ec *eval.EvalCtx,
|
|
|
|
args []eval.Value, opts map[string]eval.Value) {
|
|
|
|
|
2016-09-08 06:16:39 +08:00
|
|
|
out := ec.OutputChan()
|
2017-01-11 12:14:19 +08:00
|
|
|
out <- &styled{rpromptStr, styles{"7"}}
|
2016-09-08 06:16:39 +08:00
|
|
|
}
|
2016-02-23 20:08:41 +08:00
|
|
|
|
2017-05-22 09:00:44 +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())
|
2016-02-23 20:08:41 +08:00
|
|
|
}
|