aboutsummaryrefslogtreecommitdiff
path: root/vendor/github.com/buger/goterm/terminal_sysioctl.go
blob: 8b48b405c5af1d4039f508b52d37dce3e88f7c50 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
//go:build !windows && !plan9 && !solaris
// +build !windows,!plan9,!solaris

package goterm

import (
	"errors"
	"math"
	"os"

	"golang.org/x/sys/unix"
)

func getWinsize() (*unix.Winsize, error) {

	ws, err := unix.IoctlGetWinsize(int(os.Stdout.Fd()), unix.TIOCGWINSZ)
	if err != nil {
		return nil, os.NewSyscallError("GetWinsize", err)
	}

	return ws, nil
}

// Height gets console height
func Height() int {
	ws, err := getWinsize()
	if err != nil {
		// returns math.MinInt32 if we could not retrieve the height of console window,
		// like VSCode debugging console
		if errors.Is(err, unix.EOPNOTSUPP) {
			return math.MinInt32
		}
		return -1
	}
	return int(ws.Row)
}