-
Notifications
You must be signed in to change notification settings - Fork 0
/
size.go
78 lines (62 loc) · 1.72 KB
/
size.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
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
// Package tsize gets the terminal size. Supports Linux and Windows.
package tsize
import (
"errors"
"os"
isatty "github.com/mattn/go-isatty"
)
// Size represents terminal size in columns and rows as Width and Height,
// respectively.
type Size struct {
Width int
Height int
}
var isTerminal = isatty.IsTerminal
// ErrNotATerminal is the error to return if the given file to FgetSize isn't a
// terminal.
var ErrNotATerminal = errors.New("Given file is not a terminal")
// GetSize gets the current terminal size. The terminal is expected to be
// os.Stdout. Returns the NotATerminal error, if it is not a terminal.
func GetSize() (s Size, err error) {
return FgetSize(os.Stdout)
}
// FgetSize gets the terminal size of a given os.File. Returns the NotATerminal error, if it is not a terminal.
func FgetSize(fp *os.File) (s Size, err error) {
if fp == nil || !isTerminal(fp.Fd()) {
err = ErrNotATerminal
return
}
s, err = getTerminalSize(fp)
return
}
// SizeListener listens to terminal size changes. The new size is returned
// through the Change channel when the change occurs.
type SizeListener struct {
Change <-chan Size
done chan struct{}
}
// Close implements the io.Closer interface that stops listening to terminal
// size changes.
func (sc *SizeListener) Close() (err error) {
if sc.done != nil {
close(sc.done)
sc.done = nil
sc.Change = nil
}
return
}
// NewSizeListener creates a new size change listener
func NewSizeListener() (sc *SizeListener, err error) {
sc = &SizeListener{}
sizechan := make(chan Size, 1)
sc.Change = sizechan
sc.done = make(chan struct{})
err = getTerminalSizeChanges(sizechan, sc.done)
if err != nil {
close(sizechan)
close(sc.done)
sc = &SizeListener{}
return
}
return
}