-
Notifications
You must be signed in to change notification settings - Fork 12
/
clear_windows.go
98 lines (81 loc) · 1.82 KB
/
clear_windows.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
// +build windows
package screen
import (
"os"
"syscall"
"unsafe"
)
// Clear clears the screen
func Clear() {
var (
cursor coord
w dword
h = getScreen()
)
total := dword(h.size.x * h.size.y)
xFillConsoleOutputCharacter.Call(
uintptr(h.handle),
uintptr(' '),
uintptr(total),
*(*uintptr)(unsafe.Pointer(&cursor)),
uintptr(unsafe.Pointer(&w)),
)
xFillConsoleOutputAttribute.Call(
uintptr(h.handle),
uintptr(h.attributes),
uintptr(total), *(*uintptr)(unsafe.Pointer(&cursor)),
uintptr(unsafe.Pointer(&w)),
)
}
// MoveTopLeft moves the cursor to the top left position of the screen
func MoveTopLeft() {
h := getScreen()
xSetConsoleCursorPosition.Call(
uintptr(h.handle),
*(*uintptr)(unsafe.Pointer(&coord{})),
)
}
func getScreen() consoleScreenBufferInfoHandle {
h := consoleScreenBufferInfoHandle{
handle: syscall.Handle(os.Stdout.Fd()),
}
xGetConsoleScreenBufferInfo.Call(
uintptr(h.handle),
uintptr(unsafe.Pointer(&h.consoleScreenBufferInfo)),
)
return h
}
var (
kernel32 = syscall.NewLazyDLL("kernel32.dll")
xGetConsoleScreenBufferInfo = kernel32.NewProc("GetConsoleScreenBufferInfo")
xSetConsoleCursorPosition = kernel32.NewProc("SetConsoleCursorPosition")
xFillConsoleOutputCharacter = kernel32.NewProc("FillConsoleOutputCharacterW")
xFillConsoleOutputAttribute = kernel32.NewProc("FillConsoleOutputAttribute")
)
type (
wchar uint16
short int16
dword uint32
word uint16
)
type coord struct {
x short
y short
}
type smallRect struct {
left short
top short
right short
bottom short
}
type consoleScreenBufferInfo struct {
size coord
cursorPosition coord
attributes word
window smallRect
maximumWindowSize coord
}
type consoleScreenBufferInfoHandle struct {
handle syscall.Handle
consoleScreenBufferInfo
}