forked from creack/pty
-
Notifications
You must be signed in to change notification settings - Fork 13
/
winsize_windows.go
86 lines (71 loc) · 1.95 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
//go:build windows
// +build windows
package pty
import (
"syscall"
"unsafe"
)
// Types from golang.org/x/sys/windows.
// Ref: https://pkg.go.dev/golang.org/x/sys/windows#Coord
type windowsCoord struct {
X int16
Y int16
}
// Ref: https://pkg.go.dev/golang.org/x/sys/windows#SmallRect
type windowsSmallRect struct {
Left int16
Top int16
Right int16
Bottom int16
}
// Ref: 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 {
if err := resizePseudoConsole.Find(); err != nil {
return err
}
// TODO: As we removed the use of `.Fd()` on Unix (https://github.com/creack/pty/pull/168), we need to check if we should do the same here.
// TODO: Check if it is expected to ignore `err` here.
r0, _, _ := 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) {
if err := getConsoleScreenBufferInfo.Find(); err != nil {
return nil, err
}
var info windowsConsoleScreenBufferInfo
// TODO: Check if it is expected to ignore `err` here.
r0, _, _ := 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
}