/
screen.go
67 lines (59 loc) · 1.36 KB
/
screen.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
package display
import (
"github.com/easysoft/zentaoatf/src/utils/common"
"github.com/easysoft/zentaoatf/src/utils/shell"
"os"
"os/exec"
"regexp"
"strconv"
"strings"
)
func GetScreenSize() (int, int) {
var cmd string
var width int
var height int
if commonUtils.IsWin() {
cmd = "mode" // tested for win7
out, _ := shellUtils.ExeSysCmd(cmd)
//out := `设备状态 CON:
// ---------
// 行: 300
// 列: 80
// 键盘速度: 31
// 键盘延迟: 1
// 代码页: 936
//`
myExp := regexp.MustCompile(`CON:\s+[^\s]+\s*(.*?)(\d+)\s\s*(.*?)(\d+)\s`)
arr := myExp.FindStringSubmatch(out)
if len(arr) > 4 {
height, _ = strconv.Atoi(strings.TrimSpace(arr[2]))
width, _ = strconv.Atoi(strings.TrimSpace(arr[4]))
}
} else {
width, height = noWindowsSize()
}
return width, height
}
func noWindowsSize() (int, int) {
cmd := exec.Command("stty", "size")
cmd.Stdin = os.Stdin
out, err := cmd.Output()
output := string(out)
if err != nil {
return 0, 0
}
width, height, err := parseSize(output)
return width, height
}
func parseSize(input string) (int, int, error) {
parts := strings.Split(input, " ")
h, err := strconv.Atoi(parts[0])
if err != nil {
return 0, 0, err
}
w, err := strconv.Atoi(strings.Replace(parts[1], "\n", "", 1))
if err != nil {
return 0, 0, err
}
return int(w), int(h), nil
}