From fd1ddbce6892e3f0e09eec68687b6ef34b216888 Mon Sep 17 00:00:00 2001 From: obscuren Date: Tue, 24 Jun 2014 10:09:02 +0200 Subject: [PATCH] Save repl history to file and recall on next session --- ethereum/repl.go | 27 ++++++++++++++++++++++++++- ethereum/repl_darwin.go | 4 +++- 2 files changed, 29 insertions(+), 2 deletions(-) diff --git a/ethereum/repl.go b/ethereum/repl.go index 0208459ad..dd8d08179 100644 --- a/ethereum/repl.go +++ b/ethereum/repl.go @@ -1,10 +1,15 @@ package main import ( + "bufio" "fmt" "github.com/ethereum/eth-go" "github.com/ethereum/eth-go/ethpub" + "github.com/ethereum/eth-go/ethutil" "github.com/obscuren/otto" + "io" + "os" + "path" ) type Repl interface { @@ -16,18 +21,38 @@ type JSRepl struct { re *JSRE prompt string + + history *os.File } func NewJSRepl(ethereum *eth.Ethereum) *JSRepl { - return &JSRepl{re: NewJSRE(ethereum), prompt: "> "} + hist, err := os.OpenFile(path.Join(ethutil.Config.ExecPath, "history"), os.O_RDWR|os.O_CREATE, os.ModePerm) + if err != nil { + panic(err) + } + + return &JSRepl{re: NewJSRE(ethereum), prompt: "> ", history: hist} } func (self *JSRepl) Start() { + reader := bufio.NewReader(self.history) + for { + line, err := reader.ReadString('\n') + if err != nil && err == io.EOF { + break + } else if err != nil { + fmt.Println("error reading history", err) + break + } + + addHistory(line[:len(line)-1]) + } self.read() } func (self *JSRepl) Stop() { self.re.Stop() + self.history.Close() } func (self *JSRepl) parseInput(code string) { 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)