github.com/elves/Elvish@v0.12.0/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  	"unsafe"
    13  
    14  	"golang.org/x/sys/unix"
    15  )
    16  
    17  // SIGWINCH is the Window size change signal.
    18  const SIGWINCH = unix.SIGWINCH
    19  
    20  // winSize mirrors struct winsize in the C header.
    21  // The following declaration matches struct winsize in the headers of
    22  // Linux and FreeBSD.
    23  type winSize struct {
    24  	row    uint16
    25  	col    uint16
    26  	Xpixel uint16
    27  	Ypixel uint16
    28  }
    29  
    30  // GetWinsize queries the size of the terminal referenced by the given file.
    31  func GetWinsize(file *os.File) (row, col int) {
    32  	fd := int(file.Fd())
    33  	ws := winSize{}
    34  	if err := Ioctl(fd, unix.TIOCGWINSZ, uintptr(unsafe.Pointer(&ws))); err != nil {
    35  		fmt.Printf("error in winSize: %v", err)
    36  		return -1, -1
    37  	}
    38  
    39  	// Pick up a reasonable value for row and col
    40  	// if they equal zero in special case,
    41  	// e.g. serial console
    42  	if ws.col == 0 {
    43  		ws.col = 80
    44  	}
    45  	if ws.row == 0 {
    46  		ws.row = 24
    47  	}
    48  
    49  	return int(ws.row), int(ws.col)
    50  }