-
Notifications
You must be signed in to change notification settings - Fork 2
/
dimensions.go
44 lines (37 loc) · 1.16 KB
/
dimensions.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
43
44
package chaakoo
import (
"errors"
"fmt"
"golang.org/x/term"
)
var errNotInTerminalError = errors.New("to get the width and height, the program must run in the terminal")
// Dimension represents the dimension of the terminal in which binary is executed in.
type Dimension struct {
Width int
Height int
}
// NewDimension constructs a dimension
func NewDimension(width int, height int) *Dimension {
return &Dimension{Width: width, Height: height}
}
// TerminalDimension can be implemented by the providers of terminal dimensions
type TerminalDimension interface {
Dimension() (*Dimension, error)
}
// DimensionUsingTerm uses golang/x/term to find the dimensions of the current shell.
// There can be other implementations using:
// - tput cols and tput size
// - stty size
type DimensionUsingTerm struct {
}
// Dimension fails if the binary is being executed in a terminal
func (d *DimensionUsingTerm) Dimension() (*Dimension, error) {
if !term.IsTerminal(0) {
return nil, errNotInTerminalError
}
width, height, err := term.GetSize(0)
if err != nil {
return nil, fmt.Errorf("cannot get the width and height: %w", err)
}
return NewDimension(width, height), nil
}