github.com/insomniacslk/u-root@v0.0.0-20200717035308-96b791510d76/cmds/core/elvish/sys/winsize_unix.go (about)

     1  // +build !windows,!plan9
     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  	"fmt"
    11  	"os"
    12  
    13  	"golang.org/x/sys/unix"
    14  )
    15  
    16  // SIGWINCH is the Window size change signal.
    17  const SIGWINCH = unix.SIGWINCH
    18  
    19  // GetWinsize queries the size of the terminal referenced by the given file.
    20  func GetWinsize(file *os.File) (row, col int) {
    21  	fd := int(file.Fd())
    22  	ws, err := unix.IoctlGetWinsize(fd, unix.TIOCGWINSZ)
    23  	if err != nil {
    24  		fmt.Printf("error in winSize: %v", err)
    25  		return -1, -1
    26  	}
    27  
    28  	// Pick up a reasonable value for row and col
    29  	// if they equal zero in special case,
    30  	// e.g. serial console
    31  	if ws.Col == 0 {
    32  		ws.Col = 80
    33  	}
    34  	if ws.Row == 0 {
    35  		ws.Row = 24
    36  	}
    37  
    38  	return int(ws.Row), int(ws.Col)
    39  }