util.go 625 B

1234567891011121314151617181920212223242526272829303132333435
  1. package pty
  2. import (
  3. "os"
  4. "syscall"
  5. "unsafe"
  6. )
  7. // Getsize returns the number of rows (lines) and cols (positions
  8. // in each line) in terminal t.
  9. func Getsize(t *os.File) (rows, cols int, err error) {
  10. var ws winsize
  11. err = windowrect(&ws, t.Fd())
  12. return int(ws.ws_row), int(ws.ws_col), err
  13. }
  14. type winsize struct {
  15. ws_row uint16
  16. ws_col uint16
  17. ws_xpixel uint16
  18. ws_ypixel uint16
  19. }
  20. func windowrect(ws *winsize, fd uintptr) error {
  21. _, _, errno := syscall.Syscall(
  22. syscall.SYS_IOCTL,
  23. fd,
  24. syscall.TIOCGWINSZ,
  25. uintptr(unsafe.Pointer(ws)),
  26. )
  27. if errno != 0 {
  28. return syscall.Errno(errno)
  29. }
  30. return nil
  31. }