github.com/jonasnick/go-ethereum@v0.7.12-0.20150216215225-22176f05d387/cmd/ethereum/repl/repl_windows.go (about)

     1  // Copyright (c) 2013-2014, Jeffrey Wilcke. All rights reserved.
     2  //
     3  // This library is free software; you can redistribute it and/or
     4  // modify it under the terms of the GNU General Public
     5  // License as published by the Free Software Foundation; either
     6  // version 2.1 of the License, or (at your option) any later version.
     7  //
     8  // This library is distributed in the hope that it will be useful,
     9  // but WITHOUT ANY WARRANTY; without even the implied warranty of
    10  // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
    11  // General Public License for more details.
    12  //
    13  // You should have received a copy of the GNU General Public License
    14  // along with this library; if not, write to the Free Software
    15  // Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston,
    16  // MA 02110-1301  USA
    17  
    18  package ethrepl
    19  
    20  import (
    21  	"bufio"
    22  	"fmt"
    23  	"os"
    24  	"strings"
    25  )
    26  
    27  func (self *JSRepl) read() {
    28  	reader := bufio.NewReader(os.Stdin)
    29  	for {
    30  		fmt.Printf(self.prompt)
    31  		str, _, err := reader.ReadLine()
    32  		if err != nil {
    33  			fmt.Println("Error reading input", err)
    34  		} else {
    35  			if string(str) == "exit" {
    36  				self.Stop()
    37  				break
    38  			} else {
    39  				self.parseInput(string(str))
    40  			}
    41  		}
    42  	}
    43  }
    44  
    45  func addHistory(s string) {
    46  }
    47  
    48  func printColored(outputVal string) {
    49  	for outputVal != "" {
    50  		codePart := ""
    51  		if strings.HasPrefix(outputVal, "\033[32m") {
    52  			codePart = "\033[32m"
    53  			changeColor(2)
    54  		}
    55  		if strings.HasPrefix(outputVal, "\033[1m\033[30m") {
    56  			codePart = "\033[1m\033[30m"
    57  			changeColor(8)
    58  		}
    59  		if strings.HasPrefix(outputVal, "\033[31m") {
    60  			codePart = "\033[31m"
    61  			changeColor(red)
    62  		}
    63  		if strings.HasPrefix(outputVal, "\033[35m") {
    64  			codePart = "\033[35m"
    65  			changeColor(5)
    66  		}
    67  		if strings.HasPrefix(outputVal, "\033[0m") {
    68  			codePart = "\033[0m"
    69  			resetColorful()
    70  		}
    71  		textPart := outputVal[len(codePart):len(outputVal)]
    72  		index := strings.Index(textPart, "\033")
    73  		if index == -1 {
    74  			outputVal = ""
    75  		} else {
    76  			outputVal = textPart[index:len(textPart)]
    77  			textPart = textPart[0:index]
    78  		}
    79  		fmt.Printf("%v", textPart)
    80  	}
    81  }
    82  
    83  func (self *JSRepl) PrintValue(v interface{}) {
    84  	method, _ := self.re.Vm.Get("prettyPrint")
    85  	v, err := self.re.Vm.ToValue(v)
    86  	if err == nil {
    87  		val, err := method.Call(method, v)
    88  		if err == nil {
    89  			printColored(fmt.Sprintf("%v", val))
    90  		}
    91  	}
    92  }