winsize_unix.go 688 B

12345678910111213141516171819202122232425262728293031323334353637
  1. //go:build !windows && !plan9
  2. // Copyright 2015 go-termios Author. All Rights Reserved.
  3. // https://github.com/go-termios/termios
  4. // Author: John Lenton <chipaca@github.com>
  5. package sys
  6. import (
  7. "fmt"
  8. "os"
  9. "golang.org/x/sys/unix"
  10. )
  11. const sigWINCH = unix.SIGWINCH
  12. func winSize(file *os.File) (row, col int) {
  13. fd := int(file.Fd())
  14. ws, err := unix.IoctlGetWinsize(fd, unix.TIOCGWINSZ)
  15. if err != nil {
  16. fmt.Printf("error in winSize: %v", err)
  17. return -1, -1
  18. }
  19. // Pick up a reasonable value for row and col
  20. // if they equal zero in special case,
  21. // e.g. serial console
  22. if ws.Col == 0 {
  23. ws.Col = 80
  24. }
  25. if ws.Row == 0 {
  26. ws.Row = 24
  27. }
  28. return int(ws.Row), int(ws.Col)
  29. }