forked from gosuri/uilive
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathterminal_size_windows.go
63 lines (52 loc) · 1.23 KB
/
terminal_size_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
// +build windows
package uilive
import (
"math"
"syscall"
"unsafe"
)
type consoleFontInfo struct {
font uint32
fontSize coord
}
const (
SmCxMin = 28
SmCyMin = 29
)
var (
tmpConsoleFontInfo consoleFontInfo
moduleUser32 = syscall.NewLazyDLL("user32.dll")
procGetCurrentConsoleFont = kernel32.NewProc("GetCurrentConsoleFont")
getSystemMetrics = moduleUser32.NewProc("GetSystemMetrics")
)
func getCurrentConsoleFont(h syscall.Handle, info *consoleFontInfo) (err error) {
r0, _, e1 := syscall.Syscall(
procGetCurrentConsoleFont.Addr(), 3, uintptr(h), 0, uintptr(unsafe.Pointer(info)),
)
if int(r0) == 0 {
if e1 != 0 {
err = error(e1)
} else {
err = syscall.EINVAL
}
}
return
}
func getTermSize() (int, int) {
out, err := syscall.Open("CONOUT$", syscall.O_RDWR, 0)
if err != nil {
return 0, 0
}
x, _, err := getSystemMetrics.Call(SmCxMin)
y, _, err := getSystemMetrics.Call(SmCyMin)
if x == 0 || y == 0 {
if err != nil {
panic(err)
}
}
err = getCurrentConsoleFont(out, &tmpConsoleFontInfo)
if err != nil {
panic(err)
}
return int(math.Ceil(float64(x) / float64(tmpConsoleFontInfo.fontSize.x))), int(math.Ceil(float64(y) / float64(tmpConsoleFontInfo.fontSize.y)))
}