-
Notifications
You must be signed in to change notification settings - Fork 0
/
datatable.go
152 lines (137 loc) · 3.9 KB
/
datatable.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
package pretty
import (
"fmt"
"os"
"reflect"
"github.com/jedib0t/go-pretty/v6/table"
"github.com/jedib0t/go-pretty/v6/text"
)
type Field struct {
Name string
Text string
}
type DataTable struct {
ShortFields []Field
LongFields []Field
Item interface{}
Slots map[string]func(item interface{}) interface{}
Title string
}
func (dataTable DataTable) Print(long bool) {
tableWriter := table.NewWriter()
tableWriter.SetStyle(table.StyleLight)
tableWriter.Style().Format.Header = text.FormatDefault
tableWriter.Style().Color.Header = text.Colors{text.FgBlue, text.Bold}
tableWriter.Style().Color.Border = text.Colors{text.FgBlue}
tableWriter.Style().Color.Separator = text.Colors{text.FgBlue}
tableWriter.SetOutputMirror(os.Stdout)
headerRow := table.Row{"Field", "Value"}
fields := dataTable.ShortFields
if long {
fields = append(fields, dataTable.LongFields...)
}
tableWriter.AppendHeader(headerRow)
reflectValue := reflect.ValueOf(dataTable.Item)
for _, field := range fields {
var (
fieldValue interface{}
fieldLabel string
)
if field.Text == "" {
fieldLabel = splitTitle(field.Name)
} else {
fieldLabel = field.Text
}
if _, ok := dataTable.Slots[field.Name]; ok {
fieldValue = dataTable.Slots[field.Name](dataTable.Item)
} else {
fieldValue = reflectValue.FieldByName(field.Name)
}
tableWriter.AppendRow(table.Row{fieldLabel, fieldValue})
}
if dataTable.Title != "" {
tableWriter.SetTitle(dataTable.Title)
tableWriter.Style().Title.Align = text.AlignCenter
}
tableWriter.Render()
}
type DataListTable struct {
ShortHeaders []string
LongHeaders []string
HeaderLabel map[string]string
Items []interface{}
SortBy []table.SortBy
ColumnConfigs []table.ColumnConfig
Slots map[string]func(item interface{}) interface{}
Title string
StyleSeparateRows bool
}
func (dataTable *DataListTable) AddItems(items interface{}) {
value := reflect.ValueOf(items)
for i := 0; i < value.Len(); i++ {
dataTable.Items = append(dataTable.Items, value.Index(i).Interface())
}
}
func (dataTable *DataListTable) CleanItems() {
if len(dataTable.Items) > 0 {
dataTable.Items = []interface{}{}
}
}
func (dataTable DataListTable) Print(long bool) {
tableWriter := table.NewWriter()
tableWriter.SetStyle(table.StyleLight)
tableWriter.Style().Format.Header = text.FormatDefault
tableWriter.Style().Color.Header = text.Colors{text.FgBlue, text.Bold}
tableWriter.Style().Color.Border = text.Colors{text.FgBlue}
tableWriter.Style().Color.Separator = text.Colors{text.FgBlue}
tableWriter.Style().Options.SeparateRows = dataTable.StyleSeparateRows
tableWriter.SortBy(dataTable.SortBy)
tableWriter.SetColumnConfigs(dataTable.ColumnConfigs)
tableWriter.SetOutputMirror(os.Stdout)
headerRow := table.Row{}
titles := dataTable.ShortHeaders
if long {
titles = append(titles, dataTable.LongHeaders...)
}
for _, header := range titles {
var title string
if _, ok := dataTable.HeaderLabel[header]; ok {
title = dataTable.HeaderLabel[header]
} else {
title = splitTitle(header)
}
headerRow = append(headerRow, title)
}
tableWriter.AppendHeader(headerRow)
for _, item := range dataTable.Items {
reflectValue := reflect.ValueOf(item)
row := table.Row{}
for _, name := range titles {
var value interface{}
if _, ok := dataTable.Slots[name]; ok {
value = dataTable.Slots[name](item)
} else {
value = reflectValue.FieldByName(name)
}
row = append(row, value)
}
tableWriter.AppendRow(row)
}
if dataTable.Title != "" {
tableWriter.SetTitle(dataTable.Title)
tableWriter.Style().Title.Align = text.AlignCenter
}
tableWriter.Render()
fmt.Printf("Total items: %d\n", len(dataTable.Items))
}
func splitTitle(s string) string {
newStr := ""
for _, c := range s {
if c < 91 && newStr != "" {
newStr += " " + string(c)
} else {
newStr += string(c)
}
}
return newStr
}