src.elv.sh@v0.21.0-dev.0.20240515223629-06979efb9a2a/pkg/sys/winsize_unix.go (about)

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