forked from filswan/go-computing-provider
-
Notifications
You must be signed in to change notification settings - Fork 11
/
tablewriter.go
66 lines (58 loc) · 1.42 KB
/
tablewriter.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
package main
import (
"github.com/olekukonko/tablewriter"
"os"
)
type VisualTable struct {
Header []string
Data [][]string
RowColor []RowColor
}
type RowColor struct {
row int
column []int
color []tablewriter.Colors
}
func NewVisualTable(header []string, data [][]string, rowColor []RowColor) *VisualTable {
return &VisualTable{
Header: header,
Data: data,
RowColor: rowColor,
}
}
func (v *VisualTable) Generate(formatHeaders bool) {
table := tablewriter.NewWriter(os.Stdout)
for index, datum := range v.Data {
var rowColors []tablewriter.Colors
for _, rowColor := range v.RowColor {
if index == rowColor.row {
for dIndex := range datum {
var defaultFlag = true
for n, colIndex := range rowColor.column {
if dIndex == colIndex {
rowColors = append(rowColors, rowColor.color[n])
defaultFlag = false
}
}
if defaultFlag {
rowColors = append(rowColors, tablewriter.Colors{})
}
}
}
}
table.Rich(v.Data[index], rowColors)
}
table.SetHeader(v.Header)
table.SetAutoWrapText(false)
table.SetAutoFormatHeaders(formatHeaders)
table.SetHeaderAlignment(tablewriter.ALIGN_LEFT)
table.SetHeaderLine(false)
table.SetAlignment(tablewriter.ALIGN_LEFT)
table.SetCenterSeparator("")
table.SetColumnSeparator("")
table.SetRowSeparator("")
table.SetBorder(false)
table.SetTablePadding("\t")
table.SetNoWhiteSpace(true)
table.Render()
}