/
winsize.go
101 lines (94 loc) · 1.55 KB
/
winsize.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
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
package main
import (
"fmt"
"strconv"
"9fans.net/go/draw"
)
func parsewinsize(s string, r *draw.Rectangle, havemin *bool) error {
os := s
isdigit := func(c byte) bool { return '0' <= c && c <= '9' }
strtol := func(s string, sp *string, base int) int {
i := 0
for i < len(s) && isdigit(s[i]) {
i++
}
*sp = s[i:]
n, _ := strconv.ParseInt(s[:i], base, 0)
return int(n)
}
*havemin = false
*r = draw.Rect(0, 0, 0, 0)
var i, j, k, l int
var c byte
if s == "" || !isdigit(s[0]) {
goto oops
}
i = strtol(s, &s, 0)
if s[0] == 'x' {
s = s[1:]
if s == "" || !isdigit(s[0]) {
goto oops
}
j = strtol(s, &s, 0)
r.Max.X = i
r.Max.Y = j
if len(s) == 0 {
return nil
}
if s[0] != '@' {
goto oops
}
s = s[1:]
if s == "" || !isdigit(s[0]) {
goto oops
}
i = strtol(s, &s, 0)
if s[0] != ',' && s[0] != ' ' {
goto oops
}
s = s[1:]
if s == "" || !isdigit(s[0]) {
goto oops
}
j = strtol(s, &s, 0)
if s[0] != 0 {
goto oops
}
*r = r.Add(draw.Pt(i, j))
*havemin = true
return nil
}
c = s[0]
if c != ' ' && c != ',' {
goto oops
}
s = s[1:]
if len(s) == 0 || !isdigit(s[0]) {
goto oops
}
j = strtol(s, &s, 0)
if s[0] != c {
goto oops
}
s = s[1:]
if len(s) == 0 || !isdigit(s[0]) {
goto oops
}
k = strtol(s, &s, 0)
if s[0] != c {
goto oops
}
s = s[1:]
if len(s) == 0 || !isdigit(s[0]) {
goto oops
}
l = strtol(s, &s, 0)
if s[0] != 0 {
goto oops
}
*r = draw.Rect(i, j, k, l)
*havemin = true
return nil
oops:
return fmt.Errorf("bad syntax in window size '%s'", os)
}