github.com/ActiveState/cli@v0.0.0-20240508170324-6801f60cd051/internal/osutils/termsize/termsize.go (about)

     1  //go:build !windows && !plan9 && !solaris && !appengine && !wasm
     2  // +build !windows,!plan9,!solaris,!appengine,!wasm
     3  
     4  package termsize
     5  
     6  import (
     7  	"syscall"
     8  	"unsafe"
     9  
    10  	"github.com/ActiveState/cli/internal/logging"
    11  )
    12  
    13  const (
    14  	termSizeFallback = 80
    15  )
    16  
    17  type winsize struct {
    18  	row, col       uint16
    19  	xpixel, ypixel uint16
    20  }
    21  
    22  // GetTerminalColumns returns the number of columns available in the current terminal
    23  func GetTerminalColumns() int {
    24  	ws := winsize{}
    25  
    26  	_, _, err := syscall.Syscall(syscall.SYS_IOCTL,
    27  		uintptr(0),
    28  		uintptr(syscall.TIOCGWINSZ),
    29  		uintptr(unsafe.Pointer(&ws)))
    30  	if err != 0 {
    31  		logging.Error("Error getting terminal size: %v", err)
    32  		return termSizeFallback
    33  	}
    34  
    35  	result := int(ws.col)
    36  	if result == 0 {
    37  		result = termSizeFallback
    38  	}
    39  	return result
    40  }