-
Notifications
You must be signed in to change notification settings - Fork 1.1k
/
tree.go
339 lines (285 loc) · 8.5 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
// 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"
"strings"
"sync"
"time"
"unicode/utf8"
"github.com/pulumi/pulumi/pkg/v3/backend/display/internal/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/contract"
"github.com/rivo/uniseg"
)
type treeRenderer struct {
m sync.Mutex
opts Options
term terminal.Terminal
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.
}
func newInteractiveRenderer(term terminal.Terminal, opts Options) progressRenderer {
// 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 !term.IsRaw() {
return newInteractiveMessageRenderer(term, opts)
}
term.HideCursor()
r := &treeRenderer{
opts: opts,
term: term,
ticker: time.NewTicker(16 * time.Millisecond),
keys: make(chan string),
closed: make(chan bool),
}
if opts.deterministicOutput {
r.ticker.Stop()
}
go r.handleEvents()
go r.pollInput()
return r
}
func (r *treeRenderer) Close() error {
r.term.ShowCursor()
return r.term.Close()
}
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(false, true)
}
func (r *treeRenderer) print(text string) {
_, err := r.term.Write([]byte(r.opts.Color.Colorize(text)))
contract.IgnoreError(err)
}
func (r *treeRenderer) println(display *ProgressDisplay, text string) {
r.print(text)
r.print("\n")
}
func (r *treeRenderer) over(text string) {
r.print(text)
r.term.ClearEnd()
}
func (r *treeRenderer) overln(text string) {
r.over(text)
r.print("\n")
}
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 {
rendered := renderRow(row, maxColumnLengths)
r.treeTableRows = append(r.treeTableRows, rendered)
}
// 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
if r.opts.deterministicOutput {
r.frame(true, false)
}
}
func (r *treeRenderer) markDirty() {
r.m.Lock()
defer r.m.Unlock()
r.dirty = true
}
// +--------------------------------------------+
// | treetable header |
// | treetable contents... |
// | treetable footer |
// | system messages header |
// | system messages contents... |
// +--------------------------------------------+
func (r *treeRenderer) frame(locked, done bool) {
if !locked {
r.m.Lock()
defer r.m.Unlock()
}
if !done && !r.dirty {
return
}
r.dirty = false
termWidth, termHeight, err := r.term.Size()
contract.IgnoreError(err)
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
var treeTableFooter string
if !done && totalHeight >= termHeight {
if systemMessagesHeight > 0 {
systemMessagesHeight = termHeight / 3
if systemMessagesHeight <= 3 {
systemMessagesHeight = 0
} else {
systemMessagesContentHeight := systemMessagesHeight - 3
if len(systemMessages) > systemMessagesContentHeight {
systemMessages = systemMessages[len(systemMessages)-systemMessagesContentHeight:]
}
}
}
treeTableHeight = termHeight - systemMessagesHeight - 1
r.maxTreeTableOffset = len(treeTableRows) - treeTableHeight + 1
treeTableRows = treeTableRows[r.treeTableOffset : r.treeTableOffset+treeTableHeight-1]
totalHeight = treeTableHeight + systemMessagesHeight + 1
upArrow := " "
if r.treeTableOffset != 0 {
upArrow = "⬆ "
}
downArrow := " "
if r.treeTableOffset != r.maxTreeTableOffset {
downArrow = "⬇ "
}
footer := fmt.Sprintf("%smore%s", upArrow, downArrow)
padding := termWidth - uniseg.GraphemeClusterCount(footer)
treeTableFooter = strings.Repeat(" ", padding) + footer
if systemMessagesHeight > 0 {
treeTableFooter += "\n"
}
}
// Re-home the cursor.
r.print("\r")
for ; r.rewind > 0; r.rewind-- {
r.term.CursorUp(1)
}
r.rewind = totalHeight - 1
// Render the tree table.
r.overln(r.clampLine(treeTableHeader, termWidth))
for _, row := range treeTableRows {
r.overln(r.clampLine(row, termWidth))
}
if treeTableFooter != "" {
r.over(treeTableFooter)
}
// Render the system messages.
if systemMessagesHeight > 0 {
r.overln("")
r.overln(colors.Yellow + "System Messages" + colors.Reset)
for _, line := range systemMessages {
r.overln(" " + line)
}
}
if done && totalHeight > 0 {
r.overln("")
}
}
func (r *treeRenderer) clampLine(line string, maxWidth int) string {
// 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 := maxWidth - 1
if maxRowLength < 0 {
maxRowLength = 0
}
return colors.TrimColorizedString(line, maxRowLength)
}
func (r *treeRenderer) handleEvents() {
for {
select {
case <-r.ticker.C:
r.frame(false, 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 := r.term.ReadKey()
if err == nil {
r.keys <- key
} else if errors.Is(err, io.EOF) {
close(r.keys)
return
}
}
}