good morning!!!!

Skip to content
Snippets Groups Projects
Commit 017bbbb5 authored by Jeffrey Wilcke's avatar Jeffrey Wilcke
Browse files

Improved REPL output

parent 16421106
No related branches found
No related tags found
No related merge requests found
...@@ -15,16 +15,15 @@ import ( ...@@ -15,16 +15,15 @@ import (
const Debug = true const Debug = true
// Register interrupt handlers so we can stop the ethereum func RegisterInterrupt(cb func(os.Signal)) {
func RegisterInterrupts(s *eth.Ethereum) { go func() {
// Buffered chan of one is enough // Buffered chan of one is enough
c := make(chan os.Signal, 1) c := make(chan os.Signal, 1)
// Notify about interrupts for now // Notify about interrupts for now
signal.Notify(c, os.Interrupt) signal.Notify(c, os.Interrupt)
go func() {
for sig := range c { for sig := range c {
fmt.Printf("Shutting down (%v) ... \n", sig) cb(sig)
s.Stop()
} }
}() }()
} }
...@@ -154,13 +153,20 @@ save these words so you can restore your account later: %s ...@@ -154,13 +153,20 @@ save these words so you can restore your account later: %s
repl := NewJSRepl(ethereum) repl := NewJSRepl(ethereum)
go repl.Start() go repl.Start()
RegisterInterrupt(func(os.Signal) {
repl.Stop()
})
} }
if StartRpc { if StartRpc {
utils.DoRpc(ethereum, RpcPort) utils.DoRpc(ethereum, RpcPort)
} }
RegisterInterrupts(ethereum) RegisterInterrupt(func(sig os.Signal) {
fmt.Printf("Shutting down (%v) ... \n", sig)
ethereum.Stop()
})
ethereum.Start(UseSeed) ethereum.Start(UseSeed)
......
...@@ -11,6 +11,7 @@ import ( ...@@ -11,6 +11,7 @@ import (
type Repl interface { type Repl interface {
Start() Start()
Stop()
} }
type JSRE struct { type JSRE struct {
...@@ -36,6 +37,9 @@ func NewJSRE(ethereum *eth.Ethereum) *JSRE { ...@@ -36,6 +37,9 @@ func NewJSRE(ethereum *eth.Ethereum) *JSRE {
make(map[string][]otto.Value), make(map[string][]otto.Value),
} }
// Init the JS lib
re.vm.Run(jsLib)
// We have to make sure that, whoever calls this, calls "Stop" // We have to make sure that, whoever calls this, calls "Stop"
go re.mainLoop() go re.mainLoop()
...@@ -113,6 +117,10 @@ func (self *JSRepl) Start() { ...@@ -113,6 +117,10 @@ func (self *JSRepl) Start() {
self.read() self.read()
} }
func (self *JSRepl) Stop() {
self.re.Stop()
}
func (self *JSRepl) parseInput(code string) { func (self *JSRepl) parseInput(code string) {
defer func() { defer func() {
if r := recover(); r != nil { if r := recover(); r != nil {
...@@ -126,7 +134,7 @@ func (self *JSRepl) parseInput(code string) { ...@@ -126,7 +134,7 @@ func (self *JSRepl) parseInput(code string) {
return return
} }
fmt.Println(value) self.PrintValue(value)
} }
// The JSEthereum object attempts to wrap the PEthereum object and returns // The JSEthereum object attempts to wrap the PEthereum object and returns
......
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))
}
`
...@@ -8,6 +8,7 @@ package main ...@@ -8,6 +8,7 @@ package main
import "C" import "C"
import ( import (
"github.com/robertkrimen/otto"
"strings" "strings"
"unsafe" "unsafe"
) )
...@@ -63,18 +64,30 @@ L: ...@@ -63,18 +64,30 @@ L:
for { for {
switch result := readLine(&self.prompt); true { switch result := readLine(&self.prompt); true {
case result == nil: case result == nil:
break L //exit loop break L
case *result != "": //ignore blank lines case *result != "":
str += *result + "\n" str += *result + "\n"
self.setIndent() self.setIndent()
if indentCount <= 0 { if indentCount <= 0 {
if *result == "exit" {
self.Stop()
break L
}
addHistory(str) //allow user to recall this line addHistory(str) //allow user to recall this line
self.parseInput(str) self.parseInput(str)
str = ""
} }
} }
} }
} }
func (self *JSRepl) PrintValue(value otto.Value) {
method, _ := self.re.vm.Get("prettyPrint")
method.Call(method, value)
}
...@@ -18,3 +18,7 @@ func (self *JSRepl) read() { ...@@ -18,3 +18,7 @@ func (self *JSRepl) read() {
} }
} }
} }
func (self *JSRepl) PrintValue(value otto.Value) {
fmt.Println(value)
}
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment