-
Notifications
You must be signed in to change notification settings - Fork 1.1k
/
jsonmessage.go
427 lines (357 loc) · 11.8 KB
/
jsonmessage.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
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
// Copyright 2016-2018, Pulumi Corporation.
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
package display
// forked from: https://github.com/moby/moby/blob/master/pkg/jsonmessage/jsonmessage.go
// so we can customize parts of the display of our progress messages
import (
"fmt"
"io"
"os"
"unicode/utf8"
gotty "github.com/ijc/Gotty"
"golang.org/x/crypto/ssh/terminal"
"github.com/pulumi/pulumi/pkg/v3/engine"
"github.com/pulumi/pulumi/sdk/v3/go/common/diag/colors"
"github.com/pulumi/pulumi/sdk/v3/go/common/util/cmdutil"
"github.com/pulumi/pulumi/sdk/v3/go/common/util/contract"
)
/* Satisfied by gotty.TermInfo as well as noTermInfo from below */
type termInfo interface {
Parse(attr string, params ...interface{}) (string, error)
}
type noTermInfo struct{} // canary used when no terminfo.
func (ti *noTermInfo) Parse(attr string, params ...interface{}) (string, error) {
return "", fmt.Errorf("noTermInfo")
}
func clearLine(out io.Writer, ti termInfo) {
// el2 (clear whole line) is not exposed by terminfo.
// First clear line from beginning to cursor
if attr, err := ti.Parse("el1"); err == nil {
fmt.Fprintf(out, "%s", attr)
} else {
fmt.Fprintf(out, "\x1b[1K")
}
// Then clear line from cursor to end
if attr, err := ti.Parse("el"); err == nil {
fmt.Fprintf(out, "%s", attr)
} else {
fmt.Fprintf(out, "\x1b[K")
}
}
func cursorUp(out io.Writer, ti termInfo, l int) {
if l == 0 { // Should never be the case, but be tolerant
return
}
if attr, err := ti.Parse("cuu", l); err == nil {
fmt.Fprintf(out, "%s", attr)
} else {
fmt.Fprintf(out, "\x1b[%dA", l)
}
}
func cursorDown(out io.Writer, ti termInfo, l int) {
if l == 0 { // Should never be the case, but be tolerant
return
}
if attr, err := ti.Parse("cud", l); err == nil {
fmt.Fprintf(out, "%s", attr)
} else {
fmt.Fprintf(out, "\x1b[%dB", l)
}
}
// Progress describes a message we want to show in the display. There are two types of messages,
// simple 'Messages' which just get printed out as a single uninterpreted line, and 'Actions' which
// are placed and updated in the progress-grid based on their ID. Messages do not need an ID, while
// Actions must have an ID.
type Progress struct {
ID string
Message string
Action string
}
func makeMessageProgress(message string) Progress {
return Progress{Message: message}
}
func makeActionProgress(id string, action string) Progress {
contract.Assertf(id != "", "id must be non empty for action %s", action)
contract.Assertf(action != "", "action must be non empty")
return Progress{ID: id, Action: action}
}
// Display displays the Progress to `out`. `termInfo` is non-nil if `out` is a terminal.
func (jm *Progress) Display(out io.Writer, termInfo termInfo) {
var endl string
if termInfo != nil && /*jm.Stream == "" &&*/ jm.Action != "" {
clearLine(out, termInfo)
endl = "\r"
fmt.Fprint(out, endl)
}
if jm.Action != "" && termInfo != nil {
fmt.Fprintf(out, "%s%s", jm.Action, endl)
} else {
var msg string
if jm.Action != "" {
msg = jm.Action
} else {
msg = jm.Message
}
fmt.Fprintf(out, "%s%s\n", msg, endl)
}
}
type messageRenderer struct {
opts Options
isTerminal bool
// The width and height of the terminal. Used so we can trim resource messages that are too long.
terminalWidth int
terminalHeight int
// A spinner to use to show that we're still doing work even when no output has been
// printed to the console in a while.
nonInteractiveSpinner cmdutil.Spinner
progressOutput chan<- Progress
closed <-chan bool
// Cache of lines we've already printed. We don't print a progress message again if it hasn't
// changed between the last time we printed and now.
printedProgressCache map[string]Progress
}
func newMessageRenderer(stdout io.Writer, op string, opts Options) progressRenderer {
progressOutput, closed := make(chan Progress), make(chan bool)
go func() {
ShowProgressOutput(progressOutput, stdout, false)
close(closed)
}()
spinner, ticker := cmdutil.NewSpinnerAndTicker(
fmt.Sprintf("%s%s...", cmdutil.EmojiOr("✨ ", "@ "), op),
nil, opts.Color, 1 /*timesPerSecond*/)
ticker.Stop()
return &messageRenderer{
opts: opts,
progressOutput: progressOutput,
closed: closed,
printedProgressCache: make(map[string]Progress),
nonInteractiveSpinner: spinner,
}
}
func (r *messageRenderer) Close() error {
close(r.progressOutput)
<-r.closed
return nil
}
// Converts the colorization tags in a progress message and then actually writes the progress
// message to the output stream. This should be the only place in this file where we actually
// process colorization tags.
func (r *messageRenderer) colorizeAndWriteProgress(progress Progress) {
if progress.Message != "" {
progress.Message = r.opts.Color.Colorize(progress.Message)
}
if progress.Action != "" {
progress.Action = r.opts.Color.Colorize(progress.Action)
}
if progress.ID != "" {
// don't repeat the same output if there is no difference between the last time we
// printed it and now.
lastProgress, has := r.printedProgressCache[progress.ID]
if has && lastProgress.Message == progress.Message && lastProgress.Action == progress.Action {
return
}
r.printedProgressCache[progress.ID] = progress
}
if !r.isTerminal {
// We're about to display something. Reset our spinner so that it will go on the next line.
r.nonInteractiveSpinner.Reset()
}
r.progressOutput <- progress
}
func (r *messageRenderer) writeSimpleMessage(msg string) {
r.colorizeAndWriteProgress(makeMessageProgress(msg))
}
func (r *messageRenderer) writeBlankLine() {
r.writeSimpleMessage(" ")
}
func (r *messageRenderer) println(display *ProgressDisplay, line string) {
r.writeSimpleMessage(line)
}
func (r *messageRenderer) tick(display *ProgressDisplay) {
if r.isTerminal {
r.render(display, false)
} else {
// Update the spinner to let the user know that that work is still happening.
r.nonInteractiveSpinner.Tick()
}
}
func (r *messageRenderer) renderRow(display *ProgressDisplay,
id string, colorizedColumns []string, maxColumnLengths []int) {
uncolorizedColumns := display.uncolorizeColumns(colorizedColumns)
row := renderRow(colorizedColumns, uncolorizedColumns, maxColumnLengths)
if r.isTerminal {
// Ensure we don't go past the end of the terminal. Note: this is made complex due to
// msgWithColors having the color code information embedded with it. So we need to get
// the right substring of it, assuming that embedded colors are just markup and do not
// actually contribute to the length
maxRowLength := r.terminalWidth - 1
if maxRowLength < 0 {
maxRowLength = 0
}
row = colors.TrimColorizedString(row, maxRowLength)
}
if row != "" {
if r.isTerminal {
r.colorizeAndWriteProgress(makeActionProgress(id, row))
} else {
r.writeSimpleMessage(row)
}
}
}
func (r *messageRenderer) rowUpdated(display *ProgressDisplay, row Row) {
if r.isTerminal {
// if we're in a terminal, then refresh everything so that all our columns line up
r.render(display, false)
} else {
// otherwise, just print out this single row.
colorizedColumns := row.ColorizedColumns()
colorizedColumns[display.suffixColumn] += row.ColorizedSuffix()
r.renderRow(display, "", colorizedColumns, nil)
}
}
func (r *messageRenderer) systemMessage(display *ProgressDisplay, payload engine.StdoutEventPayload) {
if r.isTerminal {
// if we're in a terminal, then refresh everything. The system events will come after
// all the normal rows
r.render(display, false)
} else {
// otherwise, in a non-terminal, just print out the actual event.
r.writeSimpleMessage(renderStdoutColorEvent(payload, display.opts))
}
}
func (r *messageRenderer) done(display *ProgressDisplay) {
if r.isTerminal {
r.render(display, false)
}
}
func (r *messageRenderer) render(display *ProgressDisplay, done bool) {
if !r.isTerminal || display.headerRow == nil {
return
}
// make sure our stored dimension info is up to date
r.updateTerminalDimensions()
rootNodes := display.generateTreeNodes()
rootNodes = display.filterOutUnnecessaryNodesAndSetDisplayTimes(rootNodes)
sortNodes(rootNodes)
display.addIndentations(rootNodes, true /*isRoot*/, "")
maxSuffixLength := 0
for _, v := range display.suffixesArray {
runeCount := utf8.RuneCountInString(v)
if runeCount > maxSuffixLength {
maxSuffixLength = runeCount
}
}
var rows [][]string
var maxColumnLengths []int
display.convertNodesToRows(rootNodes, maxSuffixLength, &rows, &maxColumnLengths)
removeInfoColumnIfUnneeded(rows)
for i, row := range rows {
r.renderRow(display, fmt.Sprintf("%v", i), row, maxColumnLengths)
}
systemID := len(rows)
printedHeader := false
for _, payload := range display.systemEventPayloads {
msg := payload.Color.Colorize(payload.Message)
lines := splitIntoDisplayableLines(msg)
if len(lines) == 0 {
continue
}
if !printedHeader {
printedHeader = true
r.colorizeAndWriteProgress(makeActionProgress(
fmt.Sprintf("%v", systemID), " "))
systemID++
r.colorizeAndWriteProgress(makeActionProgress(
fmt.Sprintf("%v", systemID),
colors.Yellow+"System Messages"+colors.Reset))
systemID++
}
for _, line := range lines {
r.colorizeAndWriteProgress(makeActionProgress(
fmt.Sprintf("%v", systemID), fmt.Sprintf(" %s", line)))
systemID++
}
}
if done {
r.println(display, "")
}
}
// Ensure our stored dimension info is up to date.
func (r *messageRenderer) updateTerminalDimensions() {
currentTerminalWidth, currentTerminalHeight, err := terminal.GetSize(int(os.Stdout.Fd()))
contract.IgnoreError(err)
if currentTerminalWidth != r.terminalWidth ||
currentTerminalHeight != r.terminalHeight {
r.terminalWidth = currentTerminalWidth
r.terminalHeight = currentTerminalHeight
// also clear our display cache as we want to reprint all lines.
r.printedProgressCache = make(map[string]Progress)
}
}
// ShowProgressOutput displays a progress stream from `in` to `out`, `isTerminal` describes if
// `out` is a terminal. If this is the case, it will print `\n` at the end of each line and move the
// cursor while displaying.
func ShowProgressOutput(in <-chan Progress, out io.Writer, isTerminal bool) {
var (
ids = make(map[string]int)
)
var info termInfo
if isTerminal {
term := os.Getenv("TERM")
if term == "" {
term = "vt102"
}
var err error
if info, err = gotty.OpenTermInfo(term); err != nil {
info = &noTermInfo{}
}
}
for jm := range in {
diff := 0
if jm.Action != "" {
if jm.ID == "" {
contract.Failf("Must have an ID if we have an action! %s", jm.Action)
}
line, ok := ids[jm.ID]
if !ok {
// NOTE: This approach of using len(id) to
// figure out the number of lines of history
// only works as long as we clear the history
// when we output something that's not
// accounted for in the map, such as a line
// with no ID.
line = len(ids)
ids[jm.ID] = line
if info != nil {
fmt.Fprintf(out, "\n")
}
}
diff = len(ids) - line
if info != nil {
cursorUp(out, info, diff)
}
} else {
// When outputting something that isn't progress
// output, clear the history of previous lines. We
// don't want progress entries from some previous
// operation to be updated (for example, pull -a
// with multiple tags).
ids = make(map[string]int)
}
jm.Display(out, info)
if jm.Action != "" && info != nil {
cursorDown(out, info, diff)
}
}
}