-
Notifications
You must be signed in to change notification settings - Fork 7
/
tree.go
162 lines (134 loc) · 3.51 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
package progrock
import (
"fmt"
"io"
"sort"
"strings"
"github.com/muesli/termenv"
"github.com/vito/progrock/ui"
"golang.org/x/exp/slices"
)
func RenderTree(tape *Tape, w io.Writer, u *UI) error {
b := tape.bouncer()
out := ui.NewOutput(w, termenv.WithProfile(tape.ColorProfile))
var groups []*Group
for _, group := range b.groups {
groups = append(groups, group)
}
// TODO: (likely) stable order
sort.Slice(groups, func(i, j int) bool {
gi := groups[i]
gj := groups[j]
return gi.Started.AsTime().Before(gj.Started.AsTime())
})
renderVtx := func(vtx *Vertex, indent string) error {
var symbol string
var color termenv.Color
if vtx.Completed != nil {
if vtx.Error != nil {
symbol = ui.IconFailure
color = termenv.ANSIRed
} else if vtx.Canceled {
symbol = ui.IconSkipped
color = termenv.ANSIBrightBlack
} else {
symbol = ui.IconSuccess
color = termenv.ANSIGreen
}
} else {
symbol, _, _ = u.Spinner.ViewFrame(ui.DotFrames)
color = termenv.ANSIYellow
}
symbol = out.String(symbol).Foreground(color).String()
fmt.Fprintf(w, "%s%s ", indent, symbol)
if err := u.RenderVertexTree(w, vtx); err != nil {
return err
}
// TODO dedup from renderGraph
if tape.IsClosed || tape.ShowCompletedLogs || vtx.Completed == nil || vtx.Error != nil {
tasks := tape.VertexTasks[vtx.Id]
for _, t := range tasks {
fmt.Fprint(w, indent, ui.VertRightBar, " ")
if err := u.RenderTask(w, t); err != nil {
return err
}
}
term := tape.VertexLogs(vtx.Id)
if vtx.Error != nil {
term.SetHeight(term.UsedHeight())
} else {
term.SetHeight(tape.ReasonableTermHeight)
}
term.SetPrefix(indent + out.String(ui.VertBoldBar).Foreground(color).String() + " ")
if tape.IsClosed {
term.SetHeight(term.UsedHeight())
}
if err := u.RenderTerm(w, term); err != nil {
return err
}
}
return nil
}
// TODO: this ended up being super complicated after a lot of flailing and
// can probably be simplified. the goal is to render the groups depth-first,
// in a stable order. i tried harder and harder and it ended up being an
// unrelated mutation bug. -_-
rendered := map[string]struct{}{}
var render func(g *Group) error
render = func(g *Group) error {
if _, f := rendered[g.Id]; f {
return nil
} else {
rendered[g.Id] = struct{}{}
}
defer func() {
for _, sub := range groups {
if sub.Parent != nil && *sub.Parent == g.Id {
render(sub)
}
}
}()
vs := b.VisibleVertices(g)
if len(vs) == 0 {
return nil
}
sort.Slice(vs, func(i, j int) bool {
return slices.Index(tape.ChronologicalVertexIDs, vs[i].Id) < slices.Index(tape.ChronologicalVertexIDs, vs[j].Id)
})
depth := b.VisibleDepth(g) - 1
if depth < 0 {
depth = 0
}
indent := strings.Repeat(" ", depth)
if g.Name != RootGroup {
var names []string
for _, ancestor := range b.HiddenAncestors(g) {
if ancestor.Name == RootGroup {
continue
}
names = append(names, ancestor.Name)
}
names = append(names, g.Name)
groupPath := strings.Join(names, " "+ui.CaretRightFilled+" ")
fmt.Fprintf(w, "%s%s %s\n", indent, ui.CaretRightFilled, out.String(groupPath).Bold())
indent += " "
}
for _, vtx := range vs {
if err := renderVtx(vtx, indent); err != nil {
return err
}
}
return nil
}
for _, v := range b.VisibleVertices(nil) {
if err := renderVtx(v, ""); err != nil {
return err
}
}
for _, g := range groups {
if err := render(g); err != nil {
return err
}
}
return nil
}