elvish/main.go

77 lines
1.3 KiB
Go
Raw Normal View History

2014-01-30 22:09:04 +08:00
// elvish is an experimental Unix shell. It tries to incorporate a powerful
2014-02-08 19:26:06 +08:00
// programming language with an extensible, friendly user interface.
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-02-10 11:33:53 +08:00
"os"
"os/user"
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"
2013-06-16 16:08:08 +08:00
)
const (
sigchSize = 32
)
// TODO(xiaq): Currently only the editor deals with signals.
2013-06-14 17:22:55 +08:00
func main() {
ev := eval.NewEvaluator()
2014-01-31 19:18:10 +08:00
cmdNum := 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 = "???"
}
rpromptStr := username + "@" + hostname
2014-01-04 22:45:35 +08:00
sigch := make(chan os.Signal, sigchSize)
// signal.Notify(sigch)
ed := edit.New(os.Stdin, ev, sigch)
2014-02-08 21:21:01 +08:00
for {
2014-01-31 19:18:10 +08:00
cmdNum++
name := fmt.Sprintf("<tty %d>", cmdNum)
if err != nil {
panic(err)
}
2013-09-18 22:58:50 +08:00
prompt := func() string {
return util.Getwd() + "> "
}
rprompt := func() string {
return rpromptStr
}
2014-01-04 22:45:35 +08:00
lr := ed.ReadLine(prompt, rprompt)
2014-01-31 19:18:10 +08:00
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.Print(ee.(*util.ContextualError).Pprint())
continue
2013-08-17 20:06:06 +08:00
}
}
2013-06-14 17:22:55 +08:00
}