Improved REPL output

This commit is contained in:
obscuren 2014-05-19 16:32:45 +02:00
parent 16421106d4
commit 017bbbb582
5 changed files with 89 additions and 12 deletions

View File

@ -15,16 +15,15 @@ import (
const Debug = true
// Register interrupt handlers so we can stop the ethereum
func RegisterInterrupts(s *eth.Ethereum) {
// Buffered chan of one is enough
c := make(chan os.Signal, 1)
// Notify about interrupts for now
signal.Notify(c, os.Interrupt)
func RegisterInterrupt(cb func(os.Signal)) {
go func() {
// Buffered chan of one is enough
c := make(chan os.Signal, 1)
// Notify about interrupts for now
signal.Notify(c, os.Interrupt)
for sig := range c {
fmt.Printf("Shutting down (%v) ... \n", sig)
s.Stop()
cb(sig)
}
}()
}
@ -154,13 +153,20 @@ save these words so you can restore your account later: %s
repl := NewJSRepl(ethereum)
go repl.Start()
RegisterInterrupt(func(os.Signal) {
repl.Stop()
})
}
if StartRpc {
utils.DoRpc(ethereum, RpcPort)
}
RegisterInterrupts(ethereum)
RegisterInterrupt(func(sig os.Signal) {
fmt.Printf("Shutting down (%v) ... \n", sig)
ethereum.Stop()
})
ethereum.Start(UseSeed)

View File

@ -11,6 +11,7 @@ import (
type Repl interface {
Start()
Stop()
}
type JSRE struct {
@ -36,6 +37,9 @@ func NewJSRE(ethereum *eth.Ethereum) *JSRE {
make(map[string][]otto.Value),
}
// Init the JS lib
re.vm.Run(jsLib)
// We have to make sure that, whoever calls this, calls "Stop"
go re.mainLoop()
@ -113,6 +117,10 @@ func (self *JSRepl) Start() {
self.read()
}
func (self *JSRepl) Stop() {
self.re.Stop()
}
func (self *JSRepl) parseInput(code string) {
defer func() {
if r := recover(); r != nil {
@ -126,7 +134,7 @@ func (self *JSRepl) parseInput(code string) {
return
}
fmt.Println(value)
self.PrintValue(value)
}
// The JSEthereum object attempts to wrap the PEthereum object and returns

46
ethereum/js_lib.go Normal file
View File

@ -0,0 +1,46 @@
package main
const jsLib = `
function pp(object) {
var str = "";
if(object instanceof Array) {
str += "[ ";
for(var i = 0, l = object.length; i < l; i++) {
str += pp(object[i]);
if(i < l-1) {
str += ", ";
}
}
str += " ]";
} else if(typeof(object) === "object") {
str += "{ ";
var last = Object.keys(object).sort().pop()
for(var k in object) {
str += k + ": " + pp(object[k]);
if(k !== last) {
str += ", ";
}
}
str += " }";
} else if(typeof(object) === "string") {
str += "\033[32m'" + object + "'";
} else if(typeof(object) === "undefined") {
str += "\033[1m\033[30m" + object;
} else if(typeof(object) === "number") {
str += "\033[31m" + object;
} else {
str += object;
}
str += "\033[0m";
return str;
}
function prettyPrint(object) {
console.log(pp(object))
}
`

View File

@ -8,6 +8,7 @@ package main
import "C"
import (
"github.com/robertkrimen/otto"
"strings"
"unsafe"
)
@ -63,18 +64,30 @@ L:
for {
switch result := readLine(&self.prompt); true {
case result == nil:
break L //exit loop
break L
case *result != "": //ignore blank lines
case *result != "":
str += *result + "\n"
self.setIndent()
if indentCount <= 0 {
if *result == "exit" {
self.Stop()
break L
}
addHistory(str) //allow user to recall this line
self.parseInput(str)
str = ""
}
}
}
}
func (self *JSRepl) PrintValue(value otto.Value) {
method, _ := self.re.vm.Get("prettyPrint")
method.Call(method, value)
}

View File

@ -18,3 +18,7 @@ func (self *JSRepl) read() {
}
}
}
func (self *JSRepl) PrintValue(value otto.Value) {
fmt.Println(value)
}