aboutsummaryrefslogtreecommitdiffstats
path: root/ethereum/repl/repl_windows.go
blob: bfae57088819fe51a16065b3cc507d80186ef00d (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
package ethrepl

import (
    "bufio"
    "fmt"
    "os"
    "strings"
)

func (self *JSRepl) read() {
    reader := bufio.NewReader(os.Stdin)
    for {
        fmt.Printf(self.prompt)
        str, _, err := reader.ReadLine()
        if err != nil {
            fmt.Println("Error reading input", err)
        } else {
            if (string(str) == "exit") {
                self.Stop()
                break
            } else {
                self.parseInput(string(str))
            }
        }
    }
}

func addHistory(s string) {
}

func printColored(outputVal string) {
    for ; outputVal != "" ; {
        codePart := ""
        if (strings.HasPrefix(outputVal, "\033[32m")) {
            codePart = "\033[32m"
            changeColor(2)
        }
        if (strings.HasPrefix(outputVal, "\033[1m\033[30m")) {
            codePart = "\033[1m\033[30m"
            changeColor(8)
        }
        if (strings.HasPrefix(outputVal, "\033[31m")) {
            codePart = "\033[31m"
            changeColor(red)
        }
        if (strings.HasPrefix(outputVal, "\033[35m")) {
            codePart = "\033[35m"
            changeColor(5)
        }
        if (strings.HasPrefix(outputVal, "\033[0m")) {
            codePart = "\033[0m"
            resetColorful()
        }
        textPart := outputVal[len(codePart):len(outputVal)]
        index := strings.Index(textPart, "\033")
        if index == -1 {
            outputVal = ""
        } else {
            outputVal = textPart[index:len(textPart)]
            textPart = textPart[0:index]
        }
        fmt.Printf("%v", textPart)
    }
}

func (self *JSRepl) PrintValue(v interface{}) {
    method, _ := self.re.Vm.Get("prettyPrint")
    v, err := self.re.Vm.ToValue(v)
    if err == nil {
        val, err := method.Call(method, v)
        if err == nil {
            printColored(fmt.Sprintf("%v", val))
        }
    }
}