forked from reeflective/readline
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathterm.go
50 lines (40 loc) · 949 Bytes
/
term.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
package readline
import (
"fmt"
"os"
"regexp"
"unicode/utf8"
"golang.org/x/term"
)
// GetTermWidth returns the width of Stdout or 80 if the width cannot be established.
func GetTermWidth() (termWidth int) {
var err error
fd := int(os.Stdout.Fd())
termWidth, _, err = GetSize(fd)
if err != nil {
termWidth = 80 // The defacto standard on older terms
}
return
}
// GetTermLength returns the length of the terminal
// (Y length), or 80 if it cannot be established.
func GetTermLength() int {
width, _, err := term.GetSize(0)
if err != nil || width == 0 {
return 80
}
return width
}
func printf(format string, a ...interface{}) {
s := fmt.Sprintf(format, a...)
print(s)
}
func print(s string) {
os.Stdout.WriteString(s)
}
var rxAnsiSgr = regexp.MustCompile("\x1b\\[[:;0-9]+m")
// Gets the number of runes in a string.
func strLen(s string) int {
s = rxAnsiSgr.ReplaceAllString(s, "")
return utf8.RuneCountInString(s)
}