winsize.go 715 B

123456789101112131415161718192021222324252627282930
  1. // +build !solaris,!windows
  2. package term
  3. import (
  4. "unsafe"
  5. "golang.org/x/sys/unix"
  6. )
  7. // GetWinsize returns the window size based on the specified file descriptor.
  8. func GetWinsize(fd uintptr) (*Winsize, error) {
  9. ws := &Winsize{}
  10. _, _, err := unix.Syscall(unix.SYS_IOCTL, fd, uintptr(unix.TIOCGWINSZ), uintptr(unsafe.Pointer(ws)))
  11. // Skipp errno = 0
  12. if err == 0 {
  13. return ws, nil
  14. }
  15. return ws, err
  16. }
  17. // SetWinsize tries to set the specified window size for the specified file descriptor.
  18. func SetWinsize(fd uintptr, ws *Winsize) error {
  19. _, _, err := unix.Syscall(unix.SYS_IOCTL, fd, uintptr(unix.TIOCSWINSZ), uintptr(unsafe.Pointer(ws)))
  20. // Skipp errno = 0
  21. if err == 0 {
  22. return nil
  23. }
  24. return err
  25. }