-
Notifications
You must be signed in to change notification settings - Fork 148
/
terminal_windows.go
69 lines (55 loc) · 1.6 KB
/
terminal_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
package gore
import (
"syscall"
"unsafe"
)
type (
short int16
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
}
var (
kernel32 = syscall.NewLazyDLL("kernel32.dll")
procGetStdHandle = kernel32.NewProc("GetStdHandle")
procGetConsoleScreenBufferInfo = kernel32.NewProc("GetConsoleScreenBufferInfo")
procSetConsoleCursorPosition = kernel32.NewProc("SetConsoleCursorPosition")
procFillConsoleOutputCharacter = kernel32.NewProc("FillConsoleOutputCharacterW")
stdoutHandle uintptr
)
func init() {
stdoutHandle = getStdHandle(syscall.STD_OUTPUT_HANDLE)
}
func getStdHandle(stdhandle int32) uintptr {
handle, _, _ := procGetStdHandle.Call(uintptr(stdhandle))
return handle
}
func cursorUp() {
var csbi consoleScreenBufferInfo
procGetConsoleScreenBufferInfo.Call(stdoutHandle, uintptr(unsafe.Pointer(&csbi)))
var cursor coord
cursor.x = csbi.cursorPosition.x
cursor.y = csbi.cursorPosition.y - 1
procSetConsoleCursorPosition.Call(stdoutHandle, uintptr(*(*int32)(unsafe.Pointer(&cursor))))
}
func eraseInLine() {
var csbi consoleScreenBufferInfo
procGetConsoleScreenBufferInfo.Call(stdoutHandle, uintptr(unsafe.Pointer(&csbi)))
var w uint32
procFillConsoleOutputCharacter.Call(stdoutHandle, uintptr(' '), uintptr(csbi.size.x), uintptr(*(*int32)(unsafe.Pointer(&csbi.cursorPosition))), uintptr(unsafe.Pointer(&w)))
}