forked from photostorm/pty
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathwinsize_windows.go
89 lines (73 loc) · 1.76 KB
/
winsize_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 pty
import (
"syscall"
"unsafe"
)
// types from golang.org/x/sys/windows
// copy of https://pkg.go.dev/golang.org/x/sys/windows#Coord
type windowsCoord struct {
X int16
Y int16
}
// copy of https://pkg.go.dev/golang.org/x/sys/windows#SmallRect
type windowsSmallRect struct {
Left int16
Top int16
Right int16
Bottom int16
}
// copy of https://pkg.go.dev/golang.org/x/sys/windows#ConsoleScreenBufferInfo
type windowsConsoleScreenBufferInfo struct {
Size windowsCoord
CursorPosition windowsCoord
Attributes uint16
Window windowsSmallRect
MaximumWindowSize windowsCoord
}
func (c windowsCoord) Pack() uintptr {
return uintptr((int32(c.Y) << 16) | int32(c.X))
}
// Setsize resizes t to ws.
func Setsize(t FdHolder, ws *Winsize) error {
var r0 uintptr
var err error
err = resizePseudoConsole.Find()
if err != nil {
return err
}
r0, _, err = resizePseudoConsole.Call(
t.Fd(),
(windowsCoord{X: int16(ws.Cols), Y: int16(ws.Rows)}).Pack(),
)
if int32(r0) < 0 {
if r0&0x1fff0000 == 0x00070000 {
r0 &= 0xffff
}
// S_OK: 0
return syscall.Errno(r0)
}
return nil
}
// GetsizeFull returns the full terminal size description.
func GetsizeFull(t FdHolder) (size *Winsize, err error) {
err = getConsoleScreenBufferInfo.Find()
if err != nil {
return nil, err
}
var info windowsConsoleScreenBufferInfo
var r0 uintptr
r0, _, err = getConsoleScreenBufferInfo.Call(t.Fd(), uintptr(unsafe.Pointer(&info)))
if int32(r0) < 0 {
if r0&0x1fff0000 == 0x00070000 {
r0 &= 0xffff
}
// S_OK: 0
return nil, syscall.Errno(r0)
}
return &Winsize{
Rows: uint16(info.Window.Bottom - info.Window.Top + 1),
Cols: uint16(info.Window.Right - info.Window.Left + 1),
}, nil
}