-
Notifications
You must be signed in to change notification settings - Fork 6
/
tokens.go
244 lines (198 loc) · 5.01 KB
/
tokens.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
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
package bar
import (
"bufio"
"bytes"
"fmt"
"io"
"math"
"os"
"strings"
)
type tokens []token
type token interface {
debug(*Bar) string
print(*Bar) string
}
type tokenFormat struct {
stream *bufio.Reader
}
type spaceToken struct{}
type barToken struct{}
type percentToken struct{}
type rateToken struct{}
type etaToken struct{}
type customVerbToken struct {
verb string
}
type literalToken struct {
content string
}
// tokenize takes a format string and a slice of custom verbs (if any)
// and returns a slice of tokens that represent the format string.
func tokenize(f string, customVerbs []string) tokens {
var t tokens
sr := strings.NewReader(f)
r := &tokenFormat{bufio.NewReader(sr)}
for {
tkn, err := r.nextToken(customVerbs)
if err != nil {
if err == io.EOF {
return t
}
panic(fmt.Sprintf("tokenize: %v", err))
}
t = append(t, tkn)
}
}
// nextToken consumes characters from the input until a complete token is
// found and returned. If an error is encountered, it is returned alongside
// a `nil` token.
func (f *tokenFormat) nextToken(customVerbs []string) (token, error) {
for {
r, _, err := f.stream.ReadRune()
if err != nil {
return nil, err
}
switch r {
case ' ':
return spaceToken{}, nil
case ':':
return f.readAction(customVerbs)
default:
return f.readLiteral(r)
}
}
}
// readAction will consume characters from the input until it finds a valid
// action verb, returning the corresponding verb token. If no valid verb is
// found when the input runs out, a literal token will be returned instead.
func (f *tokenFormat) readAction(customVerbs []string) (token, error) {
var verb bytes.Buffer
for {
r, _, err := f.stream.ReadRune()
if err != nil {
return nil, err
}
verb.Write([]byte(string([]rune{r})))
if t, ok := tokenFromString(verb.String(), customVerbs); ok {
return t, nil
}
if f.readSeparator() {
if t, ok := tokenFromString(verb.String(), customVerbs); ok {
return t, nil
}
return literalToken{":" + verb.String()}, nil
}
}
}
// readLiteral will consume characters from the input until it encounters
// a separator character (see `readSeparator`), returning a literal token
// containing the characters it consumed.
func (f *tokenFormat) readLiteral(prefix rune) (token, error) {
var value bytes.Buffer
value.Write([]byte(string([]rune{prefix})))
for {
if f.readSeparator() {
return literalToken{value.String()}, nil
}
r, _, err := f.stream.ReadRune()
if err != nil {
return nil, err
}
value.Write([]byte(string([]rune{r})))
}
}
// readSeparator looks for a separator character (one of ` `, `:`, or *EOF*), returning
// `true` if one is found and `false` otherwise. It does not consume any characters
// from the input.
func (f *tokenFormat) readSeparator() bool {
p, err := f.stream.Peek(1)
if err != nil || p[0] == byte(' ') || p[0] == byte(':') {
return true
}
return false
}
// tokenFromString will return the token parsed from s, as well as a
// bool determining whether a valid token was found.
func tokenFromString(s string, customVerbs []string) (token, bool) {
// check for standard verbs
switch s {
case "bar":
return barToken{}, true
case "percent":
return percentToken{}, true
case "rate":
return rateToken{}, true
case "eta":
return etaToken{}, true
}
// check for custom verbs
for _, verb := range customVerbs {
if s == verb {
return customVerbToken{verb}, true
}
}
return nil, false
}
//
// print implementations
//
func (t spaceToken) print(_ *Bar) string {
return " "
}
func (t barToken) print(b *Bar) string {
p := int(b.prog() * float64(b.width))
return fmt.Sprintf(
"%s%s%s%s%s",
b.start,
strings.Repeat(b.complete, int(math.Max(0, float64(p-1)))),
b.head,
strings.Repeat(b.incomplete, b.width-p),
b.end,
)
}
func (t percentToken) print(b *Bar) string {
return fmt.Sprintf("%.1f%%", b.prog()*100)
}
func (t rateToken) print(b *Bar) string {
return fmt.Sprintf("%.1f", b.rate)
}
func (t etaToken) print(b *Bar) string {
return b.eta.String()
}
func (t customVerbToken) print(b *Bar) string {
for _, def := range b.context {
if def.verb == t.verb {
return def.value.String()
}
}
fmt.Fprintf(os.Stderr, "tokenize: only use `:` for custom verbs")
return t.verb
}
func (t literalToken) print(_ *Bar) string {
return t.content
}
//
// debug implementations
//
func (t spaceToken) debug(b *Bar) string {
return " "
}
func (t barToken) debug(b *Bar) string {
return fmt.Sprintf("<barToken p={%d} t={%d}>", b.progress, b.total)
}
func (t percentToken) debug(b *Bar) string {
return fmt.Sprintf("<percentToken \"%s\">", t.print(b))
}
func (t rateToken) debug(b *Bar) string {
return fmt.Sprintf("<rateToken \"%s\">", t.print(b))
}
func (t etaToken) debug(b *Bar) string {
return fmt.Sprintf("<etaToken \"%s\">", t.print(b))
}
func (t customVerbToken) debug(b *Bar) string {
return fmt.Sprintf("<customVerbToken verb=\"%s\" value=\"%s\">", t.verb, t.print(b))
}
func (t literalToken) debug(b *Bar) string {
return fmt.Sprintf("<literalToken \"%s\">", t.content)
}