aboutsummaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorobscuren <geffobscura@gmail.com>2014-05-19 22:32:45 +0800
committerobscuren <geffobscura@gmail.com>2014-05-19 22:32:45 +0800
commit017bbbb582b09a3264b4ff996f35275d381f284f (patch)
treeb11fba658b414029b78958ddb6137634e7ccf966
parent16421106d47efb65331ed9f0499f12038158cbf1 (diff)
downloadgo-tangerine-017bbbb582b09a3264b4ff996f35275d381f284f.tar.gz
go-tangerine-017bbbb582b09a3264b4ff996f35275d381f284f.tar.zst
go-tangerine-017bbbb582b09a3264b4ff996f35275d381f284f.zip
Improved REPL output
-rw-r--r--ethereum/ethereum.go24
-rw-r--r--ethereum/javascript_console.go10
-rw-r--r--ethereum/js_lib.go46
-rw-r--r--ethereum/repl_darwin.go17
-rw-r--r--ethereum/repl_windows.go4
5 files changed, 89 insertions, 12 deletions
diff --git a/ethereum/ethereum.go b/ethereum/ethereum.go
index 04933ef8e..1cbb61002 100644
--- a/ethereum/ethereum.go
+++ b/ethereum/ethereum.go
@@ -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)
diff --git a/ethereum/javascript_console.go b/ethereum/javascript_console.go
index 1e1ae0e48..07d69dafc 100644
--- a/ethereum/javascript_console.go
+++ b/ethereum/javascript_console.go
@@ -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
diff --git a/ethereum/js_lib.go b/ethereum/js_lib.go
new file mode 100644
index 000000000..8b59d75ca
--- /dev/null
+++ b/ethereum/js_lib.go
@@ -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))
+}
+`
diff --git a/ethereum/repl_darwin.go b/ethereum/repl_darwin.go
index 483d4cedf..87da3df1d 100644
--- a/ethereum/repl_darwin.go
+++ b/ethereum/repl_darwin.go
@@ -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)
+}
diff --git a/ethereum/repl_windows.go b/ethereum/repl_windows.go
index c42fd6e6a..9d4787772 100644
--- a/ethereum/repl_windows.go
+++ b/ethereum/repl_windows.go
@@ -18,3 +18,7 @@ func (self *JSRepl) read() {
}
}
}
+
+func (self *JSRepl) PrintValue(value otto.Value) {
+ fmt.Println(value)
+}