From 770808ce0d44cadfedbe01694c836be2eaf0e82c Mon Sep 17 00:00:00 2001 From: obscuren Date: Sat, 17 May 2014 15:15:46 +0200 Subject: Readline repl for linux & osx --- ethereum/repl_darwin.go | 55 +++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 55 insertions(+) create mode 100644 ethereum/repl_darwin.go (limited to 'ethereum/repl_darwin.go') diff --git a/ethereum/repl_darwin.go b/ethereum/repl_darwin.go new file mode 100644 index 000000000..b6de190e4 --- /dev/null +++ b/ethereum/repl_darwin.go @@ -0,0 +1,55 @@ +package main + +// #cgo LDFLAGS: -lreadline +// #include +// #include +// #include +// #include +import "C" +import "unsafe" + +func readLine(prompt *string) *string { + var p *C.char + + //readline allows an empty prompt(NULL) + if prompt != nil { + p = C.CString(*prompt) + } + + ret := C.readline(p) + + if p != nil { + C.free(unsafe.Pointer(p)) + } + + if ret == nil { + return nil + } //EOF + + s := C.GoString(ret) + C.free(unsafe.Pointer(ret)) + return &s +} + +func addHistory(s string) { + p := C.CString(s) + C.add_history(p) + C.free(unsafe.Pointer(p)) +} + +func (self *JSRepl) read() { + prompt := "eth >>> " + +L: + for { + switch result := readLine(&prompt); true { + case result == nil: + break L //exit loop + + case *result != "": //ignore blank lines + addHistory(*result) //allow user to recall this line + + self.parseInput(*result) + } + } +} -- cgit v1.2.3 From 16421106d47efb65331ed9f0499f12038158cbf1 Mon Sep 17 00:00:00 2001 From: obscuren Date: Mon, 19 May 2014 13:04:31 +0200 Subject: Added multi-line support --- ethereum/repl_darwin.go | 37 +++++++++++++++++++++++++++++++------ 1 file changed, 31 insertions(+), 6 deletions(-) (limited to 'ethereum/repl_darwin.go') diff --git a/ethereum/repl_darwin.go b/ethereum/repl_darwin.go index b6de190e4..483d4cedf 100644 --- a/ethereum/repl_darwin.go +++ b/ethereum/repl_darwin.go @@ -6,7 +6,11 @@ package main // #include // #include import "C" -import "unsafe" + +import ( + "strings" + "unsafe" +) func readLine(prompt *string) *string { var p *C.char @@ -37,19 +41,40 @@ func addHistory(s string) { C.free(unsafe.Pointer(p)) } -func (self *JSRepl) read() { - prompt := "eth >>> " +var indentCount = 0 +var str = "" + +func (self *JSRepl) setIndent() { + open := strings.Count(str, "{") + open += strings.Count(str, "(") + closed := strings.Count(str, "}") + closed += strings.Count(str, ")") + indentCount = open - closed + if indentCount <= 0 { + self.prompt = "> " + } else { + self.prompt = strings.Join(make([]string, indentCount*2), "..") + self.prompt += " " + } +} +func (self *JSRepl) read() { L: for { - switch result := readLine(&prompt); true { + switch result := readLine(&self.prompt); true { case result == nil: break L //exit loop case *result != "": //ignore blank lines - addHistory(*result) //allow user to recall this line + str += *result + "\n" + + self.setIndent() + + if indentCount <= 0 { + addHistory(str) //allow user to recall this line - self.parseInput(*result) + self.parseInput(str) + } } } } -- cgit v1.2.3 From 017bbbb582b09a3264b4ff996f35275d381f284f Mon Sep 17 00:00:00 2001 From: obscuren Date: Mon, 19 May 2014 16:32:45 +0200 Subject: Improved REPL output --- ethereum/repl_darwin.go | 17 +++++++++++++++-- 1 file changed, 15 insertions(+), 2 deletions(-) (limited to 'ethereum/repl_darwin.go') 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) +} -- cgit v1.2.3