2016-02-23 20:08:41 +08:00
|
|
|
package edit
|
|
|
|
|
|
|
|
import (
|
2016-02-23 20:24:28 +08:00
|
|
|
"errors"
|
2016-02-23 20:08:41 +08:00
|
|
|
"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
|
|
|
)
|
|
|
|
|
2016-09-08 06:16:39 +08:00
|
|
|
var ErrMustBeFn = errors.New("must be function")
|
2016-02-23 20:24:28 +08:00
|
|
|
|
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-02-23 20:24:28 +08:00
|
|
|
}
|
2016-09-08 06:16:39 +08:00
|
|
|
return nil
|
2016-02-23 20:24:28 +08:00
|
|
|
}
|
|
|
|
|
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-02-23 20:08:41 +08:00
|
|
|
|
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-02-23 20:08:41 +08:00
|
|
|
|
2016-09-08 06:16:39 +08:00
|
|
|
return &eval.BuiltinFn{"default prompt", prompt}, &eval.BuiltinFn{"default rprompt", rprompt}
|
2016-02-23 20:08:41 +08:00
|
|
|
}
|