github.com/richardwilkes/toolbox@v1.121.0/xio/term/size_unix.go (about)

     1  // Copyright (c) 2016-2024 by Richard A. Wilkes. All rights reserved.
     2  //
     3  // This Source Code Form is subject to the terms of the Mozilla Public
     4  // License, version 2.0. If a copy of the MPL was not distributed with
     5  // this file, You can obtain one at http://mozilla.org/MPL/2.0/.
     6  //
     7  // This Source Code Form is "Incompatible With Secondary Licenses", as
     8  // defined by the Mozilla Public License, version 2.0.
     9  
    10  //go:build darwin || dragonfly || freebsd || linux || netbsd || openbsd || solaris
    11  
    12  package term
    13  
    14  import (
    15  	"syscall"
    16  	"unsafe"
    17  )
    18  
    19  // Size returns the number of columns and rows comprising the terminal.
    20  func Size() (columns, rows int) {
    21  	var ws [4]uint16
    22  	if _, _, errno := syscall.Syscall(syscall.SYS_IOCTL, uintptr(0), syscall.TIOCGWINSZ, uintptr(unsafe.Pointer(&ws[0]))); errno == 0 {
    23  		return int(ws[1]), int(ws[0])
    24  	}
    25  	return defColumns, defRows
    26  }