-
Notifications
You must be signed in to change notification settings - Fork 315
/
table.go
117 lines (98 loc) · 2.46 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
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
// Copyright (c) HashiCorp, Inc.
// SPDX-License-Identifier: MPL-2.0
package terminal
import (
"github.com/olekukonko/tablewriter"
)
const (
Yellow = "yellow"
Green = "green"
Red = "red"
Blue = "blue"
Magenta = "magenta"
HiWhite = "hiwhite"
)
var colorMapping = map[string]int{
Green: tablewriter.FgGreenColor,
Yellow: tablewriter.FgYellowColor,
Red: tablewriter.FgRedColor,
Blue: tablewriter.FgBlueColor,
Magenta: tablewriter.FgMagentaColor,
HiWhite: tablewriter.FgHiWhiteColor,
}
// Passed to UI.Table to provide a nicely formatted table.
type Table struct {
Headers []string
Rows [][]Cell
}
// Cell is a single entry for a table.
type Cell struct {
Value string
Color string
}
// Table creates a new Table structure that can be used with UI.Table.
func NewTable(headers ...string) *Table {
return &Table{
Headers: headers,
}
}
// AddRow adds a row to the table.
func (t *Table) AddRow(cols []string, colors []string) {
var row []Cell
for i, col := range cols {
if i < len(colors) {
row = append(row, Cell{Value: col, Color: colors[i]})
} else {
row = append(row, Cell{Value: col})
}
}
t.Rows = append(t.Rows, row)
}
func (t *Table) ToJson() []map[string]interface{} {
if t == nil {
return make([]map[string]interface{}, 0)
}
jsonRes := make([]map[string]interface{}, 0)
for _, row := range t.Rows {
jsonRow := make(map[string]interface{})
for i, ent := range row {
jsonRow[t.Headers[i]] = ent.Value
}
jsonRes = append(jsonRes, jsonRow)
}
return jsonRes
}
// Table implements UI.
func (u *basicUI) Table(tbl *Table, opts ...Option) {
// Build our config and set our options
cfg := &config{Writer: u.bufOut}
for _, opt := range opts {
opt(cfg)
}
table := tablewriter.NewWriter(cfg.Writer)
table.SetHeader(tbl.Headers)
table.SetBorder(false)
table.SetAutoWrapText(false)
table.SetAutoFormatHeaders(false)
table.SetHeaderAlignment(tablewriter.ALIGN_LEFT)
table.SetAlignment(tablewriter.ALIGN_LEFT)
table.SetCenterSeparator("")
table.SetColumnSeparator("")
table.SetRowSeparator("")
table.SetHeaderLine(false)
table.SetTablePadding("\t") // pad with tabs
table.SetNoWhiteSpace(true)
for _, row := range tbl.Rows {
colors := make([]tablewriter.Colors, len(row))
entries := make([]string, len(row))
for i, ent := range row {
entries[i] = ent.Value
color, ok := colorMapping[ent.Color]
if ok {
colors[i] = tablewriter.Colors{color}
}
}
table.Rich(entries, colors)
}
table.Render()
}