-
Notifications
You must be signed in to change notification settings - Fork 309
/
termsize_windows.go
89 lines (74 loc) · 1.77 KB
/
termsize_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
//go:build windows
// +build windows
package flags
import (
"flag"
"syscall"
"unsafe"
)
type (
SHORT int16
WORD uint16
SMALL_RECT struct {
Left SHORT
Top SHORT
Right SHORT
Bottom SHORT
}
COORD struct {
X SHORT
Y SHORT
}
CONSOLE_SCREEN_BUFFER_INFO struct {
Size COORD
CursorPosition COORD
Attributes WORD
Window SMALL_RECT
MaximumWindowSize COORD
}
)
var kernel32DLL = syscall.NewLazyDLL("kernel32.dll")
var getConsoleScreenBufferInfoProc = kernel32DLL.NewProc("GetConsoleScreenBufferInfo")
func getError(r1, r2 uintptr, lastErr error) error {
// If the function fails, the return value is zero.
if r1 == 0 {
if lastErr != nil {
return lastErr
}
return syscall.EINVAL
}
return nil
}
func getStdHandle(stdhandle int) (uintptr, error) {
handle, err := syscall.GetStdHandle(stdhandle)
if err != nil {
return 0, err
}
return uintptr(handle), nil
}
// GetConsoleScreenBufferInfo retrieves information about the specified console screen buffer.
// http://msdn.microsoft.com/en-us/library/windows/desktop/ms683171(v=vs.85).aspx
func GetConsoleScreenBufferInfo(handle uintptr) (*CONSOLE_SCREEN_BUFFER_INFO, error) {
var info CONSOLE_SCREEN_BUFFER_INFO
if err := getError(getConsoleScreenBufferInfoProc.Call(handle, uintptr(unsafe.Pointer(&info)), 0)); err != nil {
return nil, err
}
return &info, nil
}
func getTerminalColumns() int {
if flag.Lookup("test.v") != nil {
return defaultTermSize
}
stdoutHandle, err := getStdHandle(syscall.STD_OUTPUT_HANDLE)
if err != nil {
return defaultTermSize
}
info, err := GetConsoleScreenBufferInfo(stdoutHandle)
if err != nil {
return defaultTermSize
}
if info.MaximumWindowSize.X > 0 {
return int(info.MaximumWindowSize.X)
}
return defaultTermSize
}