blob: d54c88652c00d5b4140c6bcb396c4211843fb659 (
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
37
38
39
40
|
package common
import (
"os"
xterm "github.com/charmbracelet/x/term"
)
const (
defaultViewportWidth = 80
defaultViewportHeight = 24
)
var queryTerminalSize = func() (int, int, error) {
return xterm.GetSize(os.Stdout.Fd())
}
// EffectiveViewport returns a usable terminal viewport size. Missing or invalid
// dimensions fall back to defaults.
func EffectiveViewport(width, height int) (int, int) {
if width <= 0 || height <= 0 {
terminalWidth, terminalHeight, err := queryTerminalSize()
if err == nil {
if width <= 0 && terminalWidth > 0 {
width = terminalWidth
}
if height <= 0 && terminalHeight > 0 {
height = terminalHeight
}
}
}
if width <= 0 {
width = defaultViewportWidth
}
if height <= 0 {
height = defaultViewportHeight
}
return width, height
}
|