go-ethereum/ethereum/repl_darwin.go

94 lines
1.5 KiB
Go
Raw Normal View History

2014-05-17 08:15:46 -05:00
package main
// #cgo LDFLAGS: -lreadline
// #include <stdio.h>
// #include <stdlib.h>
// #include <readline/readline.h>
// #include <readline/history.h>
import "C"
2014-05-19 06:04:31 -05:00
import (
2014-05-19 09:32:45 -05:00
"github.com/robertkrimen/otto"
2014-05-19 06:04:31 -05:00
"strings"
"unsafe"
)
2014-05-17 08:15:46 -05:00
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))
}
2014-05-19 06:04:31 -05:00
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 += " "
}
}
2014-05-17 08:15:46 -05:00
2014-05-19 06:04:31 -05:00
func (self *JSRepl) read() {
2014-05-17 08:15:46 -05:00
L:
for {
2014-05-19 06:04:31 -05:00
switch result := readLine(&self.prompt); true {
2014-05-17 08:15:46 -05:00
case result == nil:
2014-05-19 09:32:45 -05:00
break L
2014-05-17 08:15:46 -05:00
2014-05-19 09:32:45 -05:00
case *result != "":
2014-05-19 06:04:31 -05:00
str += *result + "\n"
self.setIndent()
if indentCount <= 0 {
2014-05-19 09:32:45 -05:00
if *result == "exit" {
self.Stop()
break L
}
2014-05-19 06:04:31 -05:00
addHistory(str) //allow user to recall this line
2014-05-17 08:15:46 -05:00
2014-05-19 06:04:31 -05:00
self.parseInput(str)
2014-05-19 09:32:45 -05:00
str = ""
2014-05-19 06:04:31 -05:00
}
2014-05-17 08:15:46 -05:00
}
}
}
2014-05-19 09:32:45 -05:00
func (self *JSRepl) PrintValue(value otto.Value) {
method, _ := self.re.vm.Get("prettyPrint")
method.Call(method, value)
}