aboutsummaryrefslogtreecommitdiffstats
path: root/cmd/ethereum/repl/console_colors_windows.go
blob: 1f517bd8c81f538ac13407974cb1bc39f05e7ea8 (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
76
77
78
79
80
/* Inspired by https://github.com/xuyu/logging/blob/master/colorful_win.go */

package ethrepl

import (
    "syscall"
    "unsafe"
)

type color uint16

const (
    green  = color(0x0002)
    red    = color(0x0004)
    yellow = color(0x000E)
)

const (
    mask = uint16(yellow | green | red)
)

var (
    kernel32                       = syscall.NewLazyDLL("kernel32.dll")
    procGetStdHandle               = kernel32.NewProc("GetStdHandle")
    procSetConsoleTextAttribute    = kernel32.NewProc("SetConsoleTextAttribute")
    procGetConsoleScreenBufferInfo = kernel32.NewProc("GetConsoleScreenBufferInfo")
    hStdout                        uintptr
    initScreenInfo                 *consoleScreenBufferInfo
)

func setConsoleTextAttribute(hConsoleOutput uintptr, wAttributes uint16) bool {
    ret, _, _ := procSetConsoleTextAttribute.Call(hConsoleOutput, uintptr(wAttributes))
    return ret != 0
}

type coord struct {
    X, Y int16
}

type smallRect struct {
    Left, Top, Right, Bottom int16
}

type consoleScreenBufferInfo struct {
    DwSize              coord
    DwCursorPosition    coord
    WAttributes         uint16
    SrWindow            smallRect
    DwMaximumWindowSize coord
}

func getConsoleScreenBufferInfo(hConsoleOutput uintptr) *consoleScreenBufferInfo {
    var csbi consoleScreenBufferInfo
    ret, _, _ := procGetConsoleScreenBufferInfo.Call(hConsoleOutput, uintptr(unsafe.Pointer(&csbi)))
    if ret == 0 {
        return nil
    }
    return &csbi
}

const (
    stdOutputHandle = uint32(-11 & 0xFFFFFFFF)
)

func init() {
    hStdout, _, _ = procGetStdHandle.Call(uintptr(stdOutputHandle))
    initScreenInfo = getConsoleScreenBufferInfo(hStdout)
}

func resetColorful() {
    if initScreenInfo == nil {
        return
    }
    setConsoleTextAttribute(hStdout, initScreenInfo.WAttributes)
}

func changeColor(c color) {
    attr := uint16(0) & ^mask | uint16(c)
    setConsoleTextAttribute(hStdout, attr)
}