github.com/markusbkk/elvish@v0.0.0-20231204143114-91dc52438621/pkg/sys/winsize_unix.go (about)

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