diff options
author | obscuren <geffobscura@gmail.com> | 2015-05-29 00:01:40 +0800 |
---|---|---|
committer | obscuren <geffobscura@gmail.com> | 2015-05-29 00:01:40 +0800 |
commit | f082c1b8959c1c729a4b62d6ff101d7569e8ba0f (patch) | |
tree | d19fdc2e8982a64d93612677dc2722dd41c8dbd3 /cmd/geth/js.go | |
parent | 70867904a0255bd044851585a9ad2dc34391ced2 (diff) | |
parent | d51d74eb55535db7670ad336d186ea64c6a2ff81 (diff) | |
download | dexon-f082c1b8959c1c729a4b62d6ff101d7569e8ba0f.tar dexon-f082c1b8959c1c729a4b62d6ff101d7569e8ba0f.tar.gz dexon-f082c1b8959c1c729a4b62d6ff101d7569e8ba0f.tar.bz2 dexon-f082c1b8959c1c729a4b62d6ff101d7569e8ba0f.tar.lz dexon-f082c1b8959c1c729a4b62d6ff101d7569e8ba0f.tar.xz dexon-f082c1b8959c1c729a4b62d6ff101d7569e8ba0f.tar.zst dexon-f082c1b8959c1c729a4b62d6ff101d7569e8ba0f.zip |
Merge branch 'release/0.9.26'
Diffstat (limited to 'cmd/geth/js.go')
-rw-r--r-- | cmd/geth/js.go | 64 |
1 files changed, 44 insertions, 20 deletions
diff --git a/cmd/geth/js.go b/cmd/geth/js.go index 0fb234d45..706bc6554 100644 --- a/cmd/geth/js.go +++ b/cmd/geth/js.go @@ -22,6 +22,7 @@ import ( "fmt" "math/big" "os" + "os/signal" "path/filepath" "strings" @@ -47,7 +48,8 @@ type dumbterm struct{ r *bufio.Reader } func (r dumbterm) Prompt(p string) (string, error) { fmt.Print(p) - return r.r.ReadString('\n') + line, err := r.r.ReadString('\n') + return strings.TrimSuffix(line, "\n"), err } func (r dumbterm) PasswordPrompt(p string) (string, error) { @@ -182,30 +184,52 @@ func (self *jsre) exec(filename string) error { } func (self *jsre) interactive() { - for { - input, err := self.Prompt(self.ps1) - if err != nil { - break + // Read input lines. + prompt := make(chan string) + inputln := make(chan string) + go func() { + defer close(inputln) + for { + line, err := self.Prompt(<-prompt) + if err != nil { + return + } + inputln <- line } - if input == "" { - continue + }() + // Wait for Ctrl-C, too. + sig := make(chan os.Signal, 1) + signal.Notify(sig, os.Interrupt) + + defer func() { + if self.atexit != nil { + self.atexit() } - str += input + "\n" - self.setIndent() - if indentCount <= 0 { - if input == "exit" { - break + self.re.Stop(false) + }() + for { + prompt <- self.ps1 + select { + case <-sig: + fmt.Println("caught interrupt, exiting") + return + case input, ok := <-inputln: + if !ok || indentCount <= 0 && input == "exit" { + return + } + if input == "" { + continue + } + str += input + "\n" + self.setIndent() + if indentCount <= 0 { + hist := str[:len(str)-1] + self.AppendHistory(hist) + self.parseInput(str) + str = "" } - hist := str[:len(str)-1] - self.AppendHistory(hist) - self.parseInput(str) - str = "" } } - if self.atexit != nil { - self.atexit() - } - self.re.Stop(false) } func (self *jsre) withHistory(op func(*os.File)) { |