-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathtable.go
More file actions
207 lines (165 loc) · 3.87 KB
/
table.go
File metadata and controls
207 lines (165 loc) · 3.87 KB
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
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
// Package gomdtable package is needed for generating Markdown tables.
package gomdtable
import (
"errors"
"unicode/utf8"
)
type tableList struct {
tables []Table
maxCountChars []int
}
type Table struct {
header []CellWithStyle
rows [][]CellWithStyle
}
var errorCountCellIncorrect = errors.New("count cell incorrect")
func (t *tableList) validateCountElements() error {
count := -1
for _, table := range t.tables {
if count == -1 {
count = len(table.header)
continue
}
if count != len(table.header) {
return errorCountCellIncorrect
}
for _, row := range table.rows {
if count != len(row) {
return errorCountCellIncorrect
}
}
}
return nil
}
func (t *Table) validateCountElements(row []CellWithStyle) error {
if len(t.header) != 0 && len(t.header) != len(row) {
return errorCountCellIncorrect
}
for _, r := range t.rows {
if len(r) != len(row) {
return errorCountCellIncorrect
}
}
return nil
}
func (t *Table) SetHeader(header []string) error {
headersWithStyle := []CellWithStyle{}
for _, h := range header {
headersWithStyle = append(headersWithStyle, CellWithStyle{Value: h})
}
return t.SetHeaderWithStyle(headersWithStyle)
}
func (t *Table) SetHeaderWithStyle(header []CellWithStyle) error {
t.header = []CellWithStyle{}
err := t.validateCountElements(header)
if err != nil {
return err
}
t.header = header
return nil
}
func (t *Table) AddRow(row []string) error {
rowsWithStyle := []CellWithStyle{}
for _, r := range row {
rowsWithStyle = append(rowsWithStyle, CellWithStyle{Value: r})
}
return t.AddRowWithStyle(rowsWithStyle)
}
func (t *Table) AddRowWithStyle(row []CellWithStyle) error {
err := t.validateCountElements(row)
if err != nil {
return err
}
t.rows = append(t.rows, row)
return nil
}
func NewTable() Table {
t := Table{}
return t
}
func GenerateTable(t Table) (string, error) {
strList, err := GenerateTableList([]Table{t})
if err != nil {
return "", err
}
return strList[0], nil
}
func GenerateTableList(tables []Table) ([]string, error) {
response := []string{}
tl := tableList{}
tl.tables = tables
err := tl.validateCountElements()
if err != nil {
return nil, err
}
for i, table := range tl.tables {
if i == 0 {
for _, cell := range table.header {
l := utf8.RuneCountInString(cell.Value)
tl.maxCountChars = append(tl.maxCountChars, l)
}
}
for index, cell := range table.header {
l := utf8.RuneCountInString(cell.Value)
tl.maxCountChars[index] = max(tl.maxCountChars[index], l)
}
for _, row := range table.rows {
for index, cell := range row {
l := utf8.RuneCountInString(cell.Value)
tl.maxCountChars[index] = max(tl.maxCountChars[index], l)
}
}
}
for _, table := range tl.tables {
resp := ""
delimiter := ""
for i, h := range table.header {
isLast := false
if i == len(table.header)-1 {
isLast = true
}
resp += generateCell(h, tl.maxCountChars[i], isLast)
delimiter += generateDelimiter(tl.maxCountChars[i], isLast)
}
resp += "\n"
resp += delimiter
resp += "\n"
for _, row := range table.rows {
for i, r := range row {
isLast := false
if i == len(table.header)-1 {
isLast = true
}
resp += generateCell(r, tl.maxCountChars[i], isLast)
}
resp += "\n"
}
response = append(response, resp)
}
return response, nil
}
func generateCell(cell CellWithStyle, countChar int, isLast bool) string {
l := utf8.RuneCountInString(cell.Value)
if cell.ColorCli != "" {
cell.Value = "| " + string(cell.ColorCli) + cell.Value + ResetColorCli
} else {
cell.Value = "| " + cell.Value
}
for i := l; i <= countChar; i++ {
cell.Value += " "
}
if isLast {
cell.Value += "|"
}
return cell.Value
}
func generateDelimiter(countChar int, isLast bool) string {
delimiter := "|"
for i := 0; i < countChar+2; i++ {
delimiter += "-"
}
if isLast {
delimiter += "|"
}
return delimiter
}