-
Notifications
You must be signed in to change notification settings - Fork 191
/
info.go
55 lines (45 loc) · 1016 Bytes
/
info.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
package cliutil
import (
"os"
"path/filepath"
"golang.org/x/term"
)
// Workdir get
func Workdir() string {
dir, _ := os.Getwd()
return dir
}
// BinDir get
func BinDir() string {
return filepath.Dir(os.Args[0])
}
// BinFile get
func BinFile() string {
return os.Args[0]
}
// BinName get
func BinName() string {
return filepath.Base(os.Args[0])
}
// exec: `stty -a 2>&1`
// const (
// mac: speed 9600 baud; 97 rows; 362 columns;
// macSttyMsgPattern = `(\d+)\s+rows;\s*(\d+)\s+columns;`
// linux: speed 38400 baud; rows 97; columns 362; line = 0;
// linuxSttyMsgPattern = `rows\s+(\d+);\s*columns\s+(\d+);`
// )
var terminalWidth, terminalHeight int
// GetTermSize for current console terminal.
func GetTermSize(refresh ...bool) (w int, h int) {
if terminalWidth > 0 && len(refresh) > 0 && !refresh[0] {
return terminalWidth, terminalHeight
}
var err error
w, h, err = term.GetSize(syscallStdinFd())
if err != nil {
return
}
// cache result
terminalWidth, terminalHeight = w, h
return
}