-
Notifications
You must be signed in to change notification settings - Fork 1.1k
/
tree.go
422 lines (357 loc) 路 10.6 KB
/
tree.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
// Copyright 2016-2022, 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.
// nolint: goconst
package display
import (
"errors"
"fmt"
"io"
"os"
"runtime"
"strings"
"sync"
"time"
"unicode/utf8"
terminal "golang.org/x/term"
gotty "github.com/ijc/Gotty"
"github.com/muesli/cancelreader"
"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/contract"
)
type treeRenderer struct {
m sync.Mutex
opts Options
// The file descriptor, width, and height of the terminal. Used so we can trim resource messages that are too long.
termFD int
termInfo termInfo
termWidth int
termHeight int
termState *terminal.State
term io.Writer
inFile cancelreader.CancelReader
dirty bool // True if the display has changed since the last redraw.
rewind int // The number of lines we need to rewind to redraw the entire screen.
treeTableRows []string
systemMessages []string
ticker *time.Ticker
keys chan string
closed chan bool
treeTableOffset int // The scroll offset into the tree table.
maxTreeTableOffset int // The maximum scroll offset.
}
type fileLike interface {
Fd() uintptr
}
func newInteractiveRenderer(in io.Reader, out io.Writer, opts Options) (progressRenderer, error) {
if !opts.IsInteractive {
return nil, fmt.Errorf("the tree display can only be used in interactive mode")
}
outFile, ok := out.(fileLike)
if !ok {
return nil, fmt.Errorf("stdout must be a terminal")
}
outFD := int(outFile.Fd())
width, height, err := terminal.GetSize(outFD)
if err != nil {
return nil, fmt.Errorf("getting terminal dimensions: %w", err)
}
if width == 0 || height == 0 {
return nil, fmt.Errorf("terminal has unusable dimensions %v x %v", width, height)
}
termType := os.Getenv("TERM")
if termType == "" {
termType = "vt102"
}
var info termInfo
if info, err = gotty.OpenTermInfo(termType); err != nil {
info = &noTermInfo{}
}
// Something about the tree renderer--possibly the raw terminal--does not yet play well with Windows, so for now
// we fall back to the legacy renderer on that platform.
if runtime.GOOS == "windows" {
return newInteractiveMessageRenderer(out, opts, width, height, info), nil
}
inFile, err := cancelreader.NewReader(in)
if err != nil {
return nil, fmt.Errorf("preparing stdin: %w", err)
}
state, err := terminal.MakeRaw(outFD)
if err != nil {
return nil, fmt.Errorf("enabling raw terminal: %w", err)
}
r := &treeRenderer{
opts: opts,
termFD: outFD,
termInfo: info,
termWidth: width,
termHeight: height,
termState: state,
term: out,
inFile: inFile,
ticker: time.NewTicker(16 * time.Millisecond),
keys: make(chan string),
closed: make(chan bool),
}
go r.handleEvents()
go r.pollInput()
return r, nil
}
func (r *treeRenderer) Close() error {
r.inFile.Cancel()
return terminal.Restore(r.termFD, r.termState)
}
func (r *treeRenderer) tick(display *ProgressDisplay) {
r.render(display)
}
func (r *treeRenderer) rowUpdated(display *ProgressDisplay, _ Row) {
r.render(display)
}
func (r *treeRenderer) systemMessage(display *ProgressDisplay, _ engine.StdoutEventPayload) {
r.render(display)
}
func (r *treeRenderer) done(display *ProgressDisplay) {
r.render(display)
r.ticker.Stop()
r.closed <- true
close(r.closed)
r.frame(true)
}
func (r *treeRenderer) println(display *ProgressDisplay, text string) {
_, err := fmt.Fprint(r.term, r.opts.Color.Colorize(strings.ReplaceAll(text, "\n", "\r\n")))
contract.IgnoreError(err)
_, err = fmt.Fprint(r.term, "\r\n")
contract.IgnoreError(err)
}
func (r *treeRenderer) render(display *ProgressDisplay) {
r.m.Lock()
defer r.m.Unlock()
if display.headerRow == nil {
return
}
// Render the resource tree table into rows.
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 treeTableRows [][]string
var maxColumnLengths []int
display.convertNodesToRows(rootNodes, maxSuffixLength, &treeTableRows, &maxColumnLengths)
removeInfoColumnIfUnneeded(treeTableRows)
r.treeTableRows = r.treeTableRows[:0]
for _, row := range treeTableRows {
r.treeTableRows = append(r.treeTableRows, r.renderRow(display, row, maxColumnLengths))
}
// Convert system events into lines.
r.systemMessages = r.systemMessages[:0]
for _, payload := range display.systemEventPayloads {
msg := payload.Color.Colorize(payload.Message)
r.systemMessages = append(r.systemMessages, splitIntoDisplayableLines(msg)...)
}
r.dirty = true
}
func (r *treeRenderer) markDirty() {
r.m.Lock()
defer r.m.Unlock()
r.dirty = true
}
// +--------------------------------------------+
// | treetable header |
// | treetable contents... |
// +--------------------------------------------+
func (r *treeRenderer) frame(done bool) {
r.m.Lock()
defer r.m.Unlock()
if !done && !r.dirty {
return
}
r.dirty = false
// Make sure our stored dimension info is up to date
r.updateTerminalDimensions()
treeTableRows := r.treeTableRows
systemMessages := r.systemMessages
var treeTableHeight int
var treeTableHeader string
if len(r.treeTableRows) > 0 {
treeTableHeader, treeTableRows = treeTableRows[0], treeTableRows[1:]
treeTableHeight = 1 + len(treeTableRows)
}
systemMessagesHeight := len(systemMessages)
if len(systemMessages) > 0 {
systemMessagesHeight += 3 // Account for padding + title
}
// Layout the display. The extra '1' accounts for the fact that we terminate each line with a newline.
totalHeight := treeTableHeight + systemMessagesHeight + 1
r.maxTreeTableOffset = 0
// If this is not the final frame and the terminal is not large enough to show the entire display:
// - If there are no system messages, devote the entire display to the tree table
// - If there are system messages, devote the first two thirds of the display to the tree table and the
// last third to the system messages
if !done && totalHeight >= r.termHeight {
if systemMessagesHeight > 0 {
systemMessagesHeight = r.termHeight / 3
if systemMessagesHeight <= 3 {
systemMessagesHeight = 0
} else {
systemMessagesContentHeight := systemMessagesHeight - 3
if len(systemMessages) > systemMessagesContentHeight {
systemMessages = systemMessages[len(systemMessages)-systemMessagesContentHeight:]
}
}
}
treeTableHeight = r.termHeight - systemMessagesHeight - 1
r.maxTreeTableOffset = len(treeTableRows) - treeTableHeight - 1
treeTableRows = treeTableRows[r.treeTableOffset : r.treeTableOffset+treeTableHeight-1]
totalHeight = treeTableHeight + systemMessagesHeight + 1
}
// Re-home the cursor.
for ; r.rewind > 0; r.rewind-- {
cursorUp(r.term, r.termInfo, 1)
clearLine(r.term, r.termInfo)
}
r.rewind = totalHeight - 1
// Render the tree table.
r.println(nil, treeTableHeader)
for _, row := range treeTableRows {
r.println(nil, row)
}
// Render the system messages.
if systemMessagesHeight > 0 {
r.println(nil, "")
r.println(nil, colors.Yellow+"System Messages"+colors.Reset)
for _, line := range systemMessages {
r.println(nil, " "+line)
}
}
if done && totalHeight > 0 {
r.println(nil, "")
}
}
func (r *treeRenderer) renderRow(display *ProgressDisplay, colorizedColumns []string, maxColumnLengths []int) string {
uncolorizedColumns := display.uncolorizeColumns(colorizedColumns)
row := renderRow(colorizedColumns, uncolorizedColumns, maxColumnLengths)
// 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.termWidth - 1
if maxRowLength < 0 {
maxRowLength = 0
}
return colors.TrimColorizedString(row, maxRowLength)
}
// Ensure our stored dimension info is up to date.
func (r *treeRenderer) updateTerminalDimensions() {
currentTermWidth, currentTermHeight, err := terminal.GetSize(r.termFD)
contract.IgnoreError(err)
if currentTermWidth != r.termWidth ||
currentTermHeight != r.termHeight {
r.termWidth = currentTermWidth
r.termHeight = currentTermHeight
}
}
func (r *treeRenderer) handleEvents() {
for {
select {
case <-r.ticker.C:
r.frame(false)
case key := <-r.keys:
switch key {
case "ctrl+c":
sigint()
case "up":
if r.treeTableOffset > 0 {
r.treeTableOffset--
}
r.markDirty()
case "down":
if r.treeTableOffset < r.maxTreeTableOffset {
r.treeTableOffset++
}
r.markDirty()
}
case <-r.closed:
return
}
}
}
func (r *treeRenderer) pollInput() {
for {
key, err := readKey(r.inFile)
if err == nil {
r.keys <- key
} else if errors.Is(err, cancelreader.ErrCanceled) || errors.Is(err, io.EOF) {
close(r.keys)
return
}
}
}
func readKey(r io.Reader) (string, error) {
type stateFunc func(b byte) (stateFunc, string)
var stateIntermediate stateFunc
stateIntermediate = func(b byte) (stateFunc, string) {
if b >= 0x20 && b < 0x30 {
return stateIntermediate, ""
}
switch b {
case 'A':
return nil, "up"
case 'B':
return nil, "down"
default:
return nil, "<control>"
}
}
var stateParameter stateFunc
stateParameter = func(b byte) (stateFunc, string) {
if b >= 0x30 && b < 0x40 {
return stateParameter, ""
}
return stateIntermediate(b)
}
stateBracket := func(b byte) (stateFunc, string) {
if b == '[' {
return stateParameter, ""
}
return nil, "<control>"
}
stateEscape := func(b byte) (stateFunc, string) {
if b == 0x1b {
return stateBracket, ""
}
if b == 3 {
return nil, "ctrl+c"
}
return nil, string([]byte{b})
}
state := stateEscape
for {
var b [1]byte
if _, err := r.Read(b[:]); err != nil {
return "", err
}
next, key := state(b[0])
if next == nil {
return key, nil
}
state = next
}
}