util.go 645 B

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