Save repl history to file and recall on next session

This commit is contained in:
obscuren 2014-06-24 10:09:02 +02:00
parent a13aa873c2
commit fd1ddbce68
2 changed files with 29 additions and 2 deletions

View File

@ -1,10 +1,15 @@
package main package main
import ( import (
"bufio"
"fmt" "fmt"
"github.com/ethereum/eth-go" "github.com/ethereum/eth-go"
"github.com/ethereum/eth-go/ethpub" "github.com/ethereum/eth-go/ethpub"
"github.com/ethereum/eth-go/ethutil"
"github.com/obscuren/otto" "github.com/obscuren/otto"
"io"
"os"
"path"
) )
type Repl interface { type Repl interface {
@ -16,18 +21,38 @@ type JSRepl struct {
re *JSRE re *JSRE
prompt string prompt string
history *os.File
} }
func NewJSRepl(ethereum *eth.Ethereum) *JSRepl { 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() { 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() self.read()
} }
func (self *JSRepl) Stop() { func (self *JSRepl) Stop() {
self.re.Stop() self.re.Stop()
self.history.Close()
} }
func (self *JSRepl) parseInput(code string) { func (self *JSRepl) parseInput(code string) {

View File

@ -102,7 +102,9 @@ L:
break 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) self.parseInput(str)