-
Notifications
You must be signed in to change notification settings - Fork 0
/
winterm.go
executable file
·113 lines (91 loc) · 2.33 KB
/
winterm.go
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
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
//+build windows
package colorterm
import (
"bufio"
"os"
)
const (
COLOR_BLACK = 0
COLOR_BLUE = 1
COLOR_GREEN = 2
COLOR_CYAN = 3
COLOR_RED = 4
COLOR_PURPLE = 5
COLOR_BROWN = 6
COLOR_LGREY = 7
COLOR_DGRAY = 8
COLOR_LBLUE = 9
COLOR_LGREEN = 10
COLOR_LCYAN = 11
COLOR_LRED = 12
COLOR_LPURPLE = 13
COLOR_YELLOW = 14
COLOR_WHITE = 15
COLOR_MAX = COLOR_WHITE + 1
)
type ColorTerm struct {
stdout _HANDLE
orgAttr uint16
}
func NewColorTerminal() *ColorTerm {
term := new(ColorTerm)
term.stdout = term._GetStdOutHandle()
term.orgAttr = term._GetTextAttr()
return term
}
func (term *ColorTerm) _GetStdOutHandle() _HANDLE {
return _GetStdHandle(_STD_OUTPUT_HANDLE)
}
func (term *ColorTerm) _FlushStdOut() {
stdout := bufio.NewWriter(os.Stdout)
stdout.Flush()
}
func (term *ColorTerm) _GetTextAttr() uint16 {
return _GetConsoleScreenBufferInfo(term.stdout).WAttributes
}
func (term *ColorTerm) _SetTextAttr(attr uint16) {
_SetConsoleTextAttribute(term.stdout, attr)
}
func (term *ColorTerm) Reset() {
term._SetTextAttr(term.orgAttr)
}
func (term *ColorTerm) ClearScreen() {
topleft := _COORD{0, 0}
csbi := _GetConsoleScreenBufferInfo(term.stdout)
var size uint32 = uint32(csbi.DwSize.X) * uint32(csbi.DwSize.Y)
_FillConsoleOutputCharacter(term.stdout,
0x20, size, topleft)
_FillConsoleOutputAttribute(term.stdout,
csbi.WAttributes, size, topleft)
_SetConsoleCursorPosition(term.stdout, topleft)
}
func (term *ColorTerm) Columns() uint16 {
csbi := _GetConsoleScreenBufferInfo(term.stdout)
return csbi.DwSize.X
}
func (term *ColorTerm) Lines() uint16 {
csbi := _GetConsoleScreenBufferInfo(term.stdout)
return csbi.DwSize.Y
}
func (term *ColorTerm) SetTitle(title string) {
_SetConsoleTitle(title)
}
func (term *ColorTerm) SetPosition(x, y uint16) {
_SetConsoleCursorPosition(term.stdout, _COORD{x, y})
}
func (term *ColorTerm) SetColor(fg, bg uint16) {
}
func (term *ColorTerm) SetBgColor(bg uint16) {
if bg < COLOR_MAX {
current := term._GetTextAttr()
fg := current & 0x000F
term._SetTextAttr(fg + (bg << 4))
}
}
func (term *ColorTerm) SetTextColor(fg uint16) {
if fg < COLOR_MAX {
current := term._GetTextAttr()
bg := current & 0x00F0
term._SetTextAttr(fg + bg)
}
}