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 From de1dfae7170a946d255a9b4932e08f887d48947c Mon Sep 17 00:00:00 2001 From: obscuren Date: Tue, 20 May 2014 17:49:12 +0200 Subject: Forked version of otto so we can support lowerCased methods --- ethereum/repl_darwin.go | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) (limited to 'ethereum/repl_darwin.go') diff --git a/ethereum/repl_darwin.go b/ethereum/repl_darwin.go index 87da3df1d..cf6e24e18 100644 --- a/ethereum/repl_darwin.go +++ b/ethereum/repl_darwin.go @@ -8,7 +8,7 @@ package main import "C" import ( - "github.com/robertkrimen/otto" + "github.com/obscuren/otto" "strings" "unsafe" ) -- cgit v1.2.3 From 563c035eb57a0507979a84f3dd22411be2a4cad1 Mon Sep 17 00:00:00 2001 From: obscuren Date: Tue, 20 May 2014 19:28:48 +0200 Subject: Refactored some of the functions --- ethereum/repl_darwin.go | 8 +++++--- 1 file changed, 5 insertions(+), 3 deletions(-) (limited to 'ethereum/repl_darwin.go') diff --git a/ethereum/repl_darwin.go b/ethereum/repl_darwin.go index cf6e24e18..1b98c2150 100644 --- a/ethereum/repl_darwin.go +++ b/ethereum/repl_darwin.go @@ -8,7 +8,6 @@ package main import "C" import ( - "github.com/obscuren/otto" "strings" "unsafe" ) @@ -87,7 +86,10 @@ L: } } -func (self *JSRepl) PrintValue(value otto.Value) { +func (self *JSRepl) PrintValue(v interface{}) { method, _ := self.re.vm.Get("prettyPrint") - method.Call(method, value) + v, err := self.re.vm.ToValue(v) + if err == nil { + method.Call(method, v) + } } -- cgit v1.2.3 From 3f5b348451a8acd4c22be1c320808dd4eadc38d3 Mon Sep 17 00:00:00 2001 From: obscuren Date: Wed, 21 May 2014 23:36:55 +0200 Subject: Fixes #50 --- ethereum/repl_darwin.go | 23 ++++++++++++++++++++++- 1 file changed, 22 insertions(+), 1 deletion(-) (limited to 'ethereum/repl_darwin.go') diff --git a/ethereum/repl_darwin.go b/ethereum/repl_darwin.go index 1b98c2150..cb11adfc7 100644 --- a/ethereum/repl_darwin.go +++ b/ethereum/repl_darwin.go @@ -1,17 +1,37 @@ package main +// #cgo darwin CFLAGS: -I/usr/local/opt/readline/include +// #cgo darwin LDFLAGS: -L/usr/local/opt/readline/lib // #cgo LDFLAGS: -lreadline // #include // #include // #include // #include import "C" - import ( + "os" + "os/signal" "strings" + "syscall" "unsafe" ) +func initReadLine() { + C.rl_catch_sigwinch = 0 + c := make(chan os.Signal, 1) + signal.Notify(c, syscall.SIGWINCH) + go func() { + for sig := range c { + switch sig { + case syscall.SIGWINCH: + C.rl_resize_terminal() + default: + + } + } + }() +} + func readLine(prompt *string) *string { var p *C.char @@ -59,6 +79,7 @@ func (self *JSRepl) setIndent() { } func (self *JSRepl) read() { + initReadLine() L: for { switch result := readLine(&self.prompt); true { -- cgit v1.2.3 From b902de20c7119ec521a28bba986a0cc9d14354c0 Mon Sep 17 00:00:00 2001 From: obscuren Date: Wed, 21 May 2014 23:46:16 +0200 Subject: Fixes #49 --- ethereum/repl_darwin.go | 5 +++++ 1 file changed, 5 insertions(+) (limited to 'ethereum/repl_darwin.go') diff --git a/ethereum/repl_darwin.go b/ethereum/repl_darwin.go index cb11adfc7..fa36b0d52 100644 --- a/ethereum/repl_darwin.go +++ b/ethereum/repl_darwin.go @@ -18,13 +18,18 @@ import ( func initReadLine() { C.rl_catch_sigwinch = 0 + C.rl_catch_signals = 0 c := make(chan os.Signal, 1) signal.Notify(c, syscall.SIGWINCH) + signal.Notify(c, os.Interrupt) go func() { for sig := range c { switch sig { case syscall.SIGWINCH: C.rl_resize_terminal() + + case os.Interrupt: + C.rl_cleanup_after_signal() default: } -- cgit v1.2.3 From d35380c19e5ce92b57158e7780f7105dc4136916 Mon Sep 17 00:00:00 2001 From: obscuren Date: Fri, 23 May 2014 14:37:03 +0200 Subject: New main script through init return value --- ethereum/repl_darwin.go | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) (limited to 'ethereum/repl_darwin.go') diff --git a/ethereum/repl_darwin.go b/ethereum/repl_darwin.go index fa36b0d52..b61d4edd7 100644 --- a/ethereum/repl_darwin.go +++ b/ethereum/repl_darwin.go @@ -102,7 +102,7 @@ L: break L } - addHistory(str) //allow user to recall this line + addHistory(str[:len(str)-1]) //allow user to recall this line self.parseInput(str) -- cgit v1.2.3 From fd1ddbce6892e3f0e09eec68687b6ef34b216888 Mon Sep 17 00:00:00 2001 From: obscuren Date: Tue, 24 Jun 2014 10:09:02 +0200 Subject: Save repl history to file and recall on next session --- ethereum/repl_darwin.go | 4 +++- 1 file changed, 3 insertions(+), 1 deletion(-) (limited to 'ethereum/repl_darwin.go') diff --git a/ethereum/repl_darwin.go b/ethereum/repl_darwin.go index b61d4edd7..62b40059a 100644 --- a/ethereum/repl_darwin.go +++ b/ethereum/repl_darwin.go @@ -102,7 +102,9 @@ L: break L } - addHistory(str[:len(str)-1]) //allow user to recall this line + hist := str[:len(str)-1] + addHistory(hist) //allow user to recall this line + self.history.WriteString(str) self.parseInput(str) -- cgit v1.2.3