forked from 99designs/smartling
-
Notifications
You must be signed in to change notification settings - Fork 9
/
Copy pathprogress_renderer_windows.go
77 lines (63 loc) · 1.26 KB
/
progress_renderer_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
//go:build windows
package main
import (
"fmt"
"os"
"syscall"
"unsafe"
)
var (
kernel32 = syscall.NewLazyDLL("kernel32.dll")
procGetConsoleScreenBufferInfo = kernel32.NewProc(
"GetConsoleScreenBufferInfo",
)
procSetConsoleCursorPosition = kernel32.NewProc(
"SetConsoleCursorPosition",
)
)
type (
consoleCoordinates struct {
X int16
Y int16
}
consoleScreenBufferInfo struct {
size consoleCoordinates
cursorPosition consoleCoordinates
maximumWindowSize consoleCoordinates
attributes int16
window struct {
left int16
top int16
right int16
bottom int16
}
}
)
type ProgressRenderer struct{}
func (renderer ProgressRenderer) Render(progress *Progress) error {
var info consoleScreenBufferInfo
_, _, code := syscall.SyscallN(
procGetConsoleScreenBufferInfo.Addr(),
2,
uintptr(syscall.Stderr),
uintptr(unsafe.Pointer(&info)),
0,
)
if code != 0 {
return error(code)
}
pos := info.cursorPosition
pos.X = 0
_, _, code = syscall.SyscallN(
procSetConsoleCursorPosition.Addr(),
2,
uintptr(syscall.Stderr),
uintptr(uint32(uint16(pos.Y))<<16|uint32(uint16(pos.X))),
0,
)
if code != 0 {
return error(code)
}
_, err := fmt.Fprint(os.Stderr, progress.String())
return err
}