Skip to content

Commit

Permalink
pkg/term: use IoctlGetWinsize/IoctlSetWinsize from golang.org/x/sys/unix
Browse files Browse the repository at this point in the history
Use unix.IoctlGetWinsize and unix.IoctlSetWinsize instead of manually
reimplementing them.

Signed-off-by: Tobias Klauser <[email protected]>
  • Loading branch information
tklauser committed Sep 1, 2017
1 parent 184cea5 commit 32dfc0c
Showing 1 changed file with 4 additions and 14 deletions.
18 changes: 4 additions & 14 deletions pkg/term/winsize.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,28 +3,18 @@
package term

import (
"unsafe"

"golang.org/x/sys/unix"
)

// GetWinsize returns the window size based on the specified file descriptor.
func GetWinsize(fd uintptr) (*Winsize, error) {
ws := &Winsize{}
_, _, err := unix.Syscall(unix.SYS_IOCTL, fd, uintptr(unix.TIOCGWINSZ), uintptr(unsafe.Pointer(ws)))
// Skipp errno = 0
if err == 0 {
return ws, nil
}
uws, err := unix.IoctlGetWinsize(int(fd), unix.TIOCGWINSZ)
ws := &Winsize{Height: uws.Row, Width: uws.Col, x: uws.Xpixel, y: uws.Ypixel}
return ws, err
}

// SetWinsize tries to set the specified window size for the specified file descriptor.
func SetWinsize(fd uintptr, ws *Winsize) error {
_, _, err := unix.Syscall(unix.SYS_IOCTL, fd, uintptr(unix.TIOCSWINSZ), uintptr(unsafe.Pointer(ws)))
// Skipp errno = 0
if err == 0 {
return nil
}
return err
uws := &unix.Winsize{Row: ws.Height, Col: ws.Width, Xpixel: ws.x, Ypixel: ws.y}
return unix.IoctlSetWinsize(int(fd), unix.TIOCSWINSZ, uws)
}

0 comments on commit 32dfc0c

Please sign in to comment.