-
Notifications
You must be signed in to change notification settings - Fork 262
/
table.go
78 lines (64 loc) · 2.05 KB
/
table.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
package controldisplay
import (
"strings"
"github.com/spf13/viper"
"github.com/turbot/steampipe/pkg/constants"
"github.com/turbot/steampipe/pkg/control/controlexecute"
)
type TableRenderer struct {
resultTree *controlexecute.ExecutionTree
// screen width
width int
maxFailedControls int
maxTotalControls int
}
func NewTableRenderer(resultTree *controlexecute.ExecutionTree) *TableRenderer {
return &TableRenderer{
resultTree: resultTree,
maxFailedControls: resultTree.Root.Summary.Status.FailedCount(),
maxTotalControls: resultTree.Root.Summary.Status.TotalCount(),
}
}
// MinimumWidth is the width we require
// It is determined by the left indent, title, severity, counter and counter graph
func (r TableRenderer) MinimumWidth() int {
minimumWidthRequired := r.maxIndent() + minimumGroupTitleWidth + severityMaxLen + minimumCounterWidth + counterGraphSegments
return minimumWidthRequired
}
func (r TableRenderer) maxIndent() int {
depth := r.groupDepth(r.resultTree.Root, 0)
// each indent level is "| " or "+ " (2 characters)
return (depth * 2)
}
func (r TableRenderer) groupDepth(g *controlexecute.ResultGroup, myDepth int) int {
if len(g.Groups) == 0 {
return 0
}
maxDepth := 0
for _, subGroup := range g.Groups {
branchDepth := r.groupDepth(subGroup, myDepth+1)
if branchDepth > maxDepth {
maxDepth = branchDepth
}
}
return myDepth + maxDepth
}
func (r TableRenderer) Render(width int) string {
r.width = width
// the buffer to put the output data in
builder := strings.Builder{}
builder.WriteString(r.renderResult())
builder.WriteString("\n")
builder.WriteString(r.renderSummary())
return builder.String()
}
func (r TableRenderer) renderSummary() string {
// no need to render the summary when the dry-run flag is set
if viper.GetBool(constants.ArgDryRun) {
return ""
}
return NewSummaryRenderer(r.resultTree, r.width).Render()
}
func (r TableRenderer) renderResult() string {
return NewGroupRenderer(r.resultTree.Root, nil, r.maxFailedControls, r.maxTotalControls, r.resultTree, r.width).Render()
}