2013-06-14 17:22:55 +08:00
|
|
|
package main
|
|
|
|
|
2013-06-16 16:08:08 +08:00
|
|
|
import (
|
2014-01-27 18:55:45 +08:00
|
|
|
"fmt"
|
2014-01-29 18:37:07 +08:00
|
|
|
"github.com/xiaq/elvish/edit"
|
|
|
|
"github.com/xiaq/elvish/eval"
|
|
|
|
"github.com/xiaq/elvish/parse"
|
|
|
|
"github.com/xiaq/elvish/util"
|
2014-01-16 09:24:14 +08:00
|
|
|
"os"
|
|
|
|
"os/user"
|
2013-06-16 16:08:08 +08:00
|
|
|
)
|
|
|
|
|
2013-06-14 17:22:55 +08:00
|
|
|
func main() {
|
2013-10-18 09:46:06 +08:00
|
|
|
tr, err := util.NewTimedReader(os.Stdin)
|
2013-09-27 12:19:06 +08:00
|
|
|
if err != nil {
|
|
|
|
panic(err)
|
|
|
|
}
|
2013-09-22 18:49:37 +08:00
|
|
|
|
2013-10-17 16:45:26 +08:00
|
|
|
ev := eval.NewEvaluator(os.Environ())
|
|
|
|
cmd_no := 0
|
|
|
|
|
2014-01-04 22:45:35 +08:00
|
|
|
username := "???"
|
|
|
|
user, err := user.Current()
|
|
|
|
if err == nil {
|
|
|
|
username = user.Username
|
|
|
|
}
|
|
|
|
hostname, err := os.Hostname()
|
|
|
|
if err != nil {
|
|
|
|
hostname = "???"
|
|
|
|
}
|
|
|
|
rprompt := username + "@" + hostname
|
|
|
|
|
2013-07-29 14:39:21 +08:00
|
|
|
for {
|
2013-08-15 16:36:29 +08:00
|
|
|
cmd_no++
|
2013-10-17 10:21:35 +08:00
|
|
|
name := fmt.Sprintf("<tty %d>", cmd_no)
|
2013-08-15 16:36:29 +08:00
|
|
|
|
2013-11-19 20:28:45 +08:00
|
|
|
ed, err := edit.Init(os.Stdin, tr, ev)
|
2013-09-03 22:22:28 +08:00
|
|
|
if err != nil {
|
|
|
|
panic(err)
|
|
|
|
}
|
2013-09-18 22:58:50 +08:00
|
|
|
|
2013-12-25 14:12:01 +08:00
|
|
|
prompt := util.Getwd() + "> "
|
2014-01-04 22:45:35 +08:00
|
|
|
lr := ed.ReadLine(prompt, rprompt)
|
2013-09-05 16:59:57 +08:00
|
|
|
err = ed.Cleanup()
|
2013-07-29 14:39:21 +08:00
|
|
|
if err != nil {
|
2013-09-03 22:22:28 +08:00
|
|
|
panic(err)
|
|
|
|
}
|
|
|
|
|
|
|
|
if lr.Eof {
|
2013-07-29 14:39:21 +08:00
|
|
|
break
|
2013-09-03 22:22:28 +08:00
|
|
|
} else if lr.Err != nil {
|
|
|
|
panic(lr.Err)
|
2013-07-29 14:39:21 +08:00
|
|
|
}
|
2013-09-18 22:58:50 +08:00
|
|
|
|
2014-01-28 12:10:21 +08:00
|
|
|
n, pe := parse.Parse(name, lr.Line)
|
2013-10-17 10:21:35 +08:00
|
|
|
if pe != nil {
|
2013-11-16 19:34:13 +08:00
|
|
|
fmt.Print(pe.(*util.ContextualError).Pprint())
|
2013-08-15 16:36:29 +08:00
|
|
|
continue
|
|
|
|
}
|
2013-09-18 19:46:34 +08:00
|
|
|
|
2014-01-28 12:10:21 +08:00
|
|
|
ee := ev.Eval(name, lr.Line, n)
|
2013-10-17 21:24:44 +08:00
|
|
|
if ee != nil {
|
2013-11-16 19:34:13 +08:00
|
|
|
fmt.Println(ee.(*util.ContextualError).Pprint())
|
2013-10-02 19:25:25 +08:00
|
|
|
continue
|
2013-08-17 20:06:06 +08:00
|
|
|
}
|
2013-07-29 14:39:21 +08:00
|
|
|
}
|
2013-06-14 17:22:55 +08:00
|
|
|
}
|