forked from pterm/pterm
-
Notifications
You must be signed in to change notification settings - Fork 0
/
terminal.go
42 lines (35 loc) · 1.05 KB
/
terminal.go
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
41
42
package pterm
import (
"os"
"golang.org/x/term"
)
// FallbackTerminalWidth is the value used for GetTerminalWidth, if the actual width can not be detected
// You can override that value if necessary.
var FallbackTerminalWidth = 80
// FallbackTerminalHeight is the value used for GetTerminalHeight, if the actual height can not be detected
// You can override that value if necessary.
var FallbackTerminalHeight = 10
// GetTerminalWidth returns the terminal width of the active terminal.
func GetTerminalWidth() int {
width, _, _ := GetTerminalSize()
return width
}
// GetTerminalHeight returns the terminal height of the active terminal.
func GetTerminalHeight() int {
_, height, _ := GetTerminalSize()
return height
}
// GetTerminalSize returns the width and the height of the active terminal.
func GetTerminalSize() (width, height int, err error) {
w, h, err := term.GetSize(int(os.Stdout.Fd()))
if w <= 0 {
w = FallbackTerminalWidth
}
if h <= 0 {
h = FallbackTerminalHeight
}
if err != nil {
err = ErrTerminalSizeNotDetectable
}
return w, h, err
}