-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathprinter-interface.go
309 lines (260 loc) · 7.46 KB
/
printer-interface.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
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
package toolprinter
import (
"fmt"
"math"
"os"
"strings"
"time"
"unicode/utf8"
"github.com/jimsnab/go-simpleutils"
"golang.org/x/term"
)
type terminalData interface {
IsTerminal(fd int) bool
GetSize(fd int) (width int, height int, err error)
}
type defaultTerminal struct {
}
func (t *defaultTerminal) IsTerminal(fd int) bool {
return term.IsTerminal(fd)
}
func (t *defaultTerminal) GetSize(fd int) (int, int, error) {
return term.GetSize(fd)
}
var xterm = terminalData(&defaultTerminal{})
type ToolPrinter interface {
Status(text ...interface{})
Statusf(format string, args ...interface{})
Clear()
ChattyStatus(text ...interface{})
ChattyStatusf(format string, args ...interface{})
SetCounterMax(max int, text ...interface{})
UpdateCountStatus(extraStatusText ...interface{})
Count()
PauseStatus()
ResumeStatus()
Println(text ...interface{})
Printlnf(format string, args ...interface{})
BeginPrint(text ...interface{})
ContinuePrint(text ...interface{})
ContinuePrintf(format string, args ...interface{})
EndPrint(text ...interface{})
EndPrintIfStarted()
DateRangeStatus(from time.Time, to time.Time, purpose ...interface{})
VerbosePrintln(text ...interface{})
VerbosePrintlnf(format string, args ...interface{})
EnableVerbose(enabled bool)
}
func NewToolPrinter() ToolPrinter {
return ToolPrinter(&defaultPrinter{stdoutPrinter: fmt.Print, statusPrinter: fmt.Print})
}
const simpleTimeFormat = "2006-01-02 15:04:05 MST"
type defaultPrinter struct {
stdoutPrinter func(a ...interface{}) (n int, err error)
statusPrinter func(a ...interface{}) (n int, err error)
pauseCount int
lastStatus time.Time
lastStatusText string
lastPrintedStatusText string
storedStatus string
counterText string
counter int
maxCounter int
nestedPrint bool
verboseEnabled bool
}
func (dp *defaultPrinter) Status(args ...interface{}) {
text := fmt.Sprint(args...)
dp.lastStatusText = text // lastStatusText is the true last status message, printed or not
if dp.pauseCount > 0 {
dp.storedStatus = text
return
}
if !xterm.IsTerminal(int(os.Stdout.Fd())) {
return
}
width, _, err := xterm.GetSize(int(os.Stdout.Fd()))
if err != nil {
return
}
textLength := utf8.RuneCountInString(text)
if textLength >= width {
textLength = width - 1
text = simpleutils.Substr(text, 0, textLength)
}
maxBase := utf8.RuneCountInString(dp.lastPrintedStatusText)
if textLength < maxBase {
maxBase = textLength
}
textRune := []rune(text)
lastRune := []rune(dp.lastPrintedStatusText)
// determine the common base between what was printed and what is to be printed
var baseLength int
for baseLength = 0; baseLength < maxBase; baseLength++ {
if textRune[baseLength] != lastRune[baseLength] {
break
}
}
// from the end of the last text, backspace until the new shorter base is reached
if baseLength < len(lastRune) {
dp.statusPrinter(strings.Repeat("\b", len(lastRune)-baseLength))
}
// print the part of the new text that is different from the last
dp.statusPrinter(simpleutils.Substr(text, baseLength, textLength-baseLength))
// if new text is shorter than the last text, erase extra right side characters
eraseLength := 0
if textLength < len(lastRune) {
eraseLength = len(lastRune) - textLength
dp.statusPrinter(strings.Repeat(" ", eraseLength))
dp.statusPrinter(strings.Repeat("\b", eraseLength))
}
dp.lastPrintedStatusText = text
dp.lastStatus = time.Now()
}
func (dp *defaultPrinter) Statusf(format string, args ...interface{}) {
dp.Status(fmt.Sprintf(format, args...))
}
func (dp *defaultPrinter) Clear() {
dp.SetCounterMax(0, "")
dp.Status("")
}
func (dp *defaultPrinter) ChattyStatus(args ...interface{}) {
text := fmt.Sprint(args...)
secondAgo := time.Now().Add(-1 * time.Second)
if dp.lastStatus.Before(secondAgo) {
dp.Status(text)
}
dp.lastStatusText = text // lastStatusText changes even if not printed
}
func (dp *defaultPrinter) ChattyStatusf(format string, args ...interface{}) {
dp.ChattyStatus(fmt.Sprintf(format, args...))
}
func (dp *defaultPrinter) SetCounterMax(max int, args ...interface{}) {
text := fmt.Sprint(args...)
dp.counterText = text
dp.counter = 0
dp.maxCounter = max
}
func (dp *defaultPrinter) count(args ...interface{}) {
extraStatusText := fmt.Sprint(args...)
if dp.maxCounter > 0 {
dp.counter++
c := dp.counter
if c > dp.maxCounter {
c = dp.maxCounter
}
f := (float64(c) * 100.0) / float64(dp.maxCounter)
percentage := int(math.Round(f))
text := fmt.Sprintf("%s %d of %d %d%%", dp.counterText, c, dp.maxCounter, percentage)
if percentage < 99 {
lastQuarterSecond := time.Now().Add(-250 * time.Millisecond)
if dp.lastStatus.After(lastQuarterSecond) {
dp.lastStatusText = text // lastStatusText changes even if not printed
return
}
}
if len(extraStatusText) == 0 {
dp.Status(text)
} else {
dp.Status(text + " " + extraStatusText)
}
}
}
func (dp *defaultPrinter) UpdateCountStatus(args ...interface{}) {
extraStatusText := fmt.Sprint(args...)
if dp.maxCounter > 0 {
dp.counter-- // decrement, then increment in dp.count(), for a net zero counter change
dp.count(extraStatusText)
}
}
func (dp *defaultPrinter) Count() {
dp.count("")
}
func (dp *defaultPrinter) PauseStatus() {
if dp.pauseCount == 0 {
dp.storedStatus = dp.lastStatusText
dp.Status("")
}
dp.pauseCount++
}
func (dp *defaultPrinter) ResumeStatus() {
if dp.pauseCount == 0 {
return
}
dp.pauseCount--
if dp.pauseCount == 0 {
dp.Status(dp.storedStatus)
}
}
func (dp *defaultPrinter) Println(args ...interface{}) {
text := fmt.Sprint(args...)
if dp.nestedPrint {
panic(fmt.Errorf("in a nested print"))
}
dp.PauseStatus()
dp.stdoutPrinter(text)
dp.stdoutPrinter("\n")
dp.ResumeStatus()
}
func (dp *defaultPrinter) Printlnf(format string, args ...interface{}) {
dp.Println(fmt.Sprintf(format, args...))
}
func (dp *defaultPrinter) BeginPrint(args ...interface{}) {
text := fmt.Sprint(args...)
if dp.nestedPrint {
panic(fmt.Errorf("in a nested print"))
}
dp.PauseStatus()
if len(text) > 0 {
dp.stdoutPrinter(text)
}
dp.nestedPrint = true
}
func (dp *defaultPrinter) ContinuePrint(args ...interface{}) {
text := fmt.Sprint(args...)
if !dp.nestedPrint {
panic(fmt.Errorf("segmented printing didn't begin yet"))
}
if len(text) > 0 {
dp.stdoutPrinter(text)
}
}
func (dp *defaultPrinter) ContinuePrintf(format string, args ...interface{}) {
dp.ContinuePrint(fmt.Sprintf(format, args...))
}
func (dp *defaultPrinter) EndPrint(args ...interface{}) {
text := fmt.Sprint(args...)
if !dp.nestedPrint {
panic(fmt.Errorf("segmented printing didn't begin yet"))
}
dp.stdoutPrinter(text)
dp.stdoutPrinter("\n")
dp.ResumeStatus()
dp.nestedPrint = false
}
func (dp *defaultPrinter) EndPrintIfStarted() {
if dp.nestedPrint {
dp.EndPrint("")
}
}
func (dp *defaultPrinter) DateRangeStatus(from time.Time, to time.Time, args ...interface{}) {
purpose := fmt.Sprint(args...)
if from.Equal(to) {
dp.Status(purpose + " for " + from.Format(simpleTimeFormat))
} else {
dp.Status(purpose + " between " + from.Format(simpleTimeFormat) + " and " + to.Format(simpleTimeFormat))
}
}
func (dp *defaultPrinter) EnableVerbose(enable bool) {
dp.verboseEnabled = enable
}
func (dp *defaultPrinter) VerbosePrintln(args ...interface{}) {
if dp.verboseEnabled {
dp.Println(args...)
}
}
func (dp *defaultPrinter) VerbosePrintlnf(format string, args ...interface{}) {
if dp.verboseEnabled {
dp.Printlnf(format, args...)
}
}