elvish/main.go

68 lines
1.1 KiB
Go
Raw Normal View History

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)
if err != nil {
panic(err)
}
2013-09-22 18:49:37 +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
for {
cmd_no++
name := fmt.Sprintf("<tty %d>", cmd_no)
ed, err := edit.Init(os.Stdin, tr, ev)
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)
err = ed.Cleanup()
if err != nil {
panic(err)
}
if lr.Eof {
break
} else if lr.Err != nil {
panic(lr.Err)
}
2013-09-18 22:58:50 +08:00
n, pe := parse.Parse(name, lr.Line)
if pe != nil {
fmt.Print(pe.(*util.ContextualError).Pprint())
continue
}
ee := ev.Eval(name, lr.Line, n)
2013-10-17 21:24:44 +08:00
if ee != nil {
fmt.Println(ee.(*util.ContextualError).Pprint())
continue
2013-08-17 20:06:06 +08:00
}
}
2013-06-14 17:22:55 +08:00
}