-
Notifications
You must be signed in to change notification settings - Fork 0
/
term.go
66 lines (61 loc) · 1.23 KB
/
term.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
package term
import (
"encoding/binary"
"fmt"
"syscall"
"unsafe"
)
// ParseDims extracts terminal dimensions (width x height) from the provided buffer.
func ParseDims(b []byte) (uint32, uint32) {
w := binary.BigEndian.Uint32(b)
h := binary.BigEndian.Uint32(b[4:])
return w, h
}
func ParseValue(b []byte) (value string) {
for _, c := range b[4:] {
if c < 32 || c > 126 {
break
}
value += string(c)
}
return
}
func ParseKeyValue(b []byte) (key, value string) {
idx := 0
for i, c := range b[4:] {
if c < 32 || c > 126 {
idx = i + 4
break
}
key += string(c)
}
for _, c := range b[idx:] {
if c < 32 || c > 126 {
continue
}
value += string(c)
}
// fmt.Printf("key:%s, value:%s, bytes: %v\n", key, value, b)
return
}
// SetWinSz sets the width and height for the given tty fd
func SetWinSz(fd uintptr, w, h uint32) (err error) {
ws := &struct {
Height uint16
Width uint16
x uint16 // unused
y uint16 // unused
}{
Width: uint16(w),
Height: uint16(h),
}
_, _, errno := syscall.Syscall(
syscall.SYS_IOCTL,
fd, uintptr(syscall.TIOCSWINSZ),
uintptr(unsafe.Pointer(ws)),
)
if errno > 0 {
return fmt.Errorf("set tiocgwinsz error: [%v] %s", errno, errno.Error())
}
return nil
}