elvish/edit/prompt.go

46 lines
1007 B
Go
Raw Normal View History

package edit
import (
"errors"
"os"
"os/user"
"github.com/elves/elvish/eval"
"github.com/elves/elvish/util"
)
2016-09-08 06:16:39 +08:00
var ErrMustBeFn = errors.New("must be function")
2016-09-08 06:16:39 +08:00
// MustBeFn validates whether a Value is an Fn.
func MustBeFn(v eval.Value) error {
if _, ok := v.(eval.Fn); !ok {
return ErrMustBeFn
}
2016-09-08 06:16:39 +08:00
return nil
}
2016-09-08 06:16:39 +08:00
func defaultPrompts() (eval.FnValue, eval.FnValue) {
// Make default prompts.
2016-09-15 08:45:44 +08:00
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() + "> ", ""}
}
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
2016-09-15 08:45: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()
out <- &styled{rpromptStr, "7"}
}
2016-09-08 06:16:39 +08:00
return &eval.BuiltinFn{"default prompt", prompt}, &eval.BuiltinFn{"default rprompt", rprompt}
}