Skip to content

Commit f4f01f5

Browse files
JonathanLogancreack
authored andcommitted
Added terminal/pty resize functionality and utility function to inherit size from master's pty to slave. Changes type winsize to make it accessible from outside the package. (#39)
1 parent 95d05c1 commit f4f01f5

1 file changed

Lines changed: 37 additions & 10 deletions

File tree

util.go

Lines changed: 37 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -8,26 +8,53 @@ import (
88
"unsafe"
99
)
1010

11+
// InheritSize applies the terminal size of master to slave. This should be run
12+
// in a signal handler for syscall.SIGWINCH to automatically resize the slave when
13+
// the master receives a window size change notification.
14+
func InheritSize(master, slave *os.File) error {
15+
size, err := GetsizeFull(master)
16+
if err != nil {
17+
return err
18+
}
19+
err = Setsize(slave, size)
20+
if err != nil {
21+
return err
22+
}
23+
return nil
24+
}
25+
26+
// Setsize resizes t to s.
27+
func Setsize(t *os.File, ws *Winsize) error {
28+
return windowRectCall(ws, t.Fd(), syscall.TIOCSWINSZ)
29+
}
30+
31+
// GetsizeFull returns the full terminal size description.
32+
func GetsizeFull(t *os.File) (size *Winsize, err error) {
33+
var ws Winsize
34+
err = windowRectCall(&ws, t.Fd(), syscall.TIOCGWINSZ)
35+
return &ws, err
36+
}
37+
1138
// Getsize returns the number of rows (lines) and cols (positions
1239
// in each line) in terminal t.
1340
func Getsize(t *os.File) (rows, cols int, err error) {
14-
var ws winsize
15-
err = windowrect(&ws, t.Fd())
16-
return int(ws.ws_row), int(ws.ws_col), err
41+
ws, err := GetsizeFull(t)
42+
return int(ws.Rows), int(ws.Cols), err
1743
}
1844

19-
type winsize struct {
20-
ws_row uint16
21-
ws_col uint16
22-
ws_xpixel uint16
23-
ws_ypixel uint16
45+
// Winsize describes the terminal size.
46+
type Winsize struct {
47+
Rows uint16 // ws_row: Number of rows (in cells)
48+
Cols uint16 // ws_col: Number of columns (in cells)
49+
X uint16 // ws_xpixel: Width in pixels
50+
Y uint16 // ws_ypixel: Height in pixels
2451
}
2552

26-
func windowrect(ws *winsize, fd uintptr) error {
53+
func windowRectCall(ws *Winsize, fd, a2 uintptr) error {
2754
_, _, errno := syscall.Syscall(
2855
syscall.SYS_IOCTL,
2956
fd,
30-
syscall.TIOCGWINSZ,
57+
a2,
3158
uintptr(unsafe.Pointer(ws)),
3259
)
3360
if errno != 0 {

0 commit comments

Comments
 (0)