-
Notifications
You must be signed in to change notification settings - Fork 223
/
interactivelogger.go
262 lines (222 loc) · 6.11 KB
/
interactivelogger.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
package statuslogger
import (
"fmt"
"slices"
"strings"
"sync"
"time"
"github.com/morikuni/aec"
"github.com/superfly/flyctl/internal/cmdutil"
"github.com/superfly/flyctl/iostreams"
"golang.org/x/crypto/ssh/terminal"
)
type interactiveLogger struct {
lock sync.Mutex
io *iostreams.IOStreams
statusFrame int
showStatus bool
active bool
done bool
lines []*interactiveLine
prevLines int
// Should we include an item prefix, such as [01/10]?
logNumbers bool
}
func (il *interactiveLogger) Line(i int) StatusLine {
return il.lines[i]
}
const (
divider = "-------"
paddingBeforeJobs = 2
)
func (il *interactiveLogger) Destroy(clear bool) {
il.lock.Lock()
defer il.lock.Unlock()
if il.done {
return
}
il.active = false
il.done = false
if clear {
fmt.Fprintf(il.io.Out, il.clearStr())
} else {
fmt.Fprintf(il.io.Out, "%s%s\n", aec.Down(uint(il.height(il.prevLines))), divider)
}
}
func (il *interactiveLogger) consoleSize() (int, int) {
width, height, err := terminal.GetSize(int(il.io.StdoutFd()))
if err != nil {
height = 24
width = 80
}
return width, height
}
// The current sorting algorithm prioritizes failures, in-progress jobs, and then completed jobs.
// It will pick the most recently modified jobs, sequentially in these categories, then finally sort them all by job ID
func (il *interactiveLogger) currentLines(conHeight int) (finalLines []interactiveLine) {
maxHeight := conHeight - paddingBeforeJobs - 1
if maxHeight < 0 {
return nil
}
var errorLines []interactiveLine
var inProgressLines []interactiveLine
var doneLines []interactiveLine
// TODO: There's probably a more efficient way to insert these *and* have them sorted at the same time.
// Give tasks that are done a grace period before they're cleared.
now := time.Now()
twoSecondsAgo := now.Add(-time.Second * 2)
for _, line := range il.lines {
if line.status == StatusFailure {
errorLines = append(errorLines, *line)
} else if line.status == StatusSuccess {
if line.doneTime.Before(twoSecondsAgo) {
doneLines = append(doneLines, *line)
} else {
// Hack to ensure that this line is still visible
inProgressLines = append(inProgressLines, *line)
}
} else {
inProgressLines = append(inProgressLines, *line)
}
}
// Intentionally reversed, so that we sort in descending order
sortByTime := func(a, b interactiveLine) int {
return b.lastChanged.Compare(a.lastChanged)
}
sortById := func(a, b interactiveLine) int {
return a.lineNum - b.lineNum
}
slices.SortStableFunc(errorLines, sortByTime)
slices.SortStableFunc(inProgressLines, sortByTime)
slices.SortStableFunc(doneLines, sortByTime)
defer func() {
slices.SortFunc(finalLines, sortById)
}()
for _, line := range errorLines {
finalLines = append(finalLines, line)
if len(finalLines) >= maxHeight {
return finalLines
}
}
for _, line := range inProgressLines {
finalLines = append(finalLines, line)
if len(finalLines) >= maxHeight {
return finalLines
}
}
for _, line := range doneLines {
finalLines = append(finalLines, line)
if len(finalLines) >= maxHeight {
return finalLines
}
}
return finalLines
}
func (il *interactiveLogger) height(numEntries int) int {
// The +2 is to account for the divider before jobs
return paddingBeforeJobs + numEntries
}
func (il *interactiveLogger) clearStr() string {
total := il.height(il.prevLines)
return strings.Repeat(aec.EraseLine(aec.EraseModes.All).String()+"\n", total) + aec.Up(uint(total)).String()
}
func (il *interactiveLogger) animateThread() {
// Increment the animation frame every 2 iterations
// Each iteration is 50ms, so this is 100ms per frame
// We redraw so often in order to chase the beam, so to speak
// If three lines of text are drawn between frames, our status block will
// bleed into those new lines. Redrawing, along with the two lines of whitespace,
// are generally enough to prevent this.
incrementAnim := 0
for {
il.lock.Lock()
if il.done {
il.lock.Unlock()
return
}
if il.active {
if il.showStatus {
incrementAnim += 1
if incrementAnim == 2 {
il.statusFrame = (il.statusFrame + 1) % len(glyphsRunning)
incrementAnim = 0
}
}
il.lockedDraw()
}
il.lock.Unlock()
time.Sleep(50 * time.Millisecond)
}
}
func (il *interactiveLogger) lockedDraw() {
if !il.active || il.done {
return
}
conW, conH := il.consoleSize()
currentLines := il.currentLines(conH)
if len(currentLines) == 0 {
return
}
defer func() {
il.prevLines = len(currentLines)
}()
// Draw the entire status block, clearing each row to prevent overwriting
buf := fmt.Sprintf("%s\n%s\n", il.clearStr(), divider)
for _, line := range currentLines {
buf += " "
line_len := 0
if il.showStatus {
buf += line.status.charFor(il.statusFrame) + " "
line_len += 2
}
if il.logNumbers {
idx := formatIndex(line.lineNum, len(il.lines))
buf += idx + " "
line_len += len(idx) + 1
}
remainingSpace := conW - line_len - 3
switch {
case remainingSpace < 0:
case len(cmdutil.StripANSI(line.buf)) >= remainingSpace:
buf += substrIgnoreAnsi(line.buf, remainingSpace-1) + "…"
default:
buf += line.buf
}
buf += "\n"
}
// Erase last line, prevent weird display bug
buf += aec.EraseLine(aec.EraseModes.All).String()
// Send the cursor back up above the status block
buf += aec.Up(uint(il.height(len(currentLines)))).String()
fmt.Fprint(il.io.Out, buf)
}
func substrIgnoreAnsi(str string, length int) string {
for i := 0; i < length; i++ {
// Check if ansi escape
if str[i] == '\x1b' {
if len(str) > i+2 && str[i+1] == '[' {
// find next m
mIdx := strings.IndexByte(str[i+1:], 'm')
if mIdx != -1 {
// I don't understand how this could ever be >len(str), but it was
// causing crashes, so we'll just be safe :)
length = min(length+mIdx+2, len(str))
i += mIdx + 1
}
}
}
}
return str[:length] + aec.Reset
}
func (il *interactiveLogger) Pause() ResumeFn {
il.lock.Lock()
defer il.lock.Unlock()
fmt.Fprint(il.io.Out, il.clearStr())
il.active = false
return func() {
il.lock.Lock()
defer il.lock.Unlock()
il.active = true
il.lockedDraw()
}
}