-
Notifications
You must be signed in to change notification settings - Fork 0
/
tablePrinter.go
152 lines (115 loc) · 3.07 KB
/
tablePrinter.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 display
import (
"strings"
"unicode/utf8"
)
// LineData represents a displayable table line
type LineData struct {
Values []string
HorizontalRuleAfter bool
}
// NewLineData creates a new LineData object
func NewLineData(horizontalRuleAfter bool, values []string) *LineData {
return &LineData{
Values: values,
HorizontalRuleAfter: horizontalRuleAfter,
}
}
// CreateTableString creates an ASCII table having header as table header and a LineData slice as table rows
// It automatically resize itself based on the lengths of every cell
func CreateTableString(header []string, data []*LineData) (string, error) {
err := checkValidity(header, data)
if err != nil {
return "", err
}
columnsWidths := computeColumnsWidths(header, data)
builder := &strings.Builder{}
drawHorizontalRule(builder, columnsWidths)
drawLine(builder, columnsWidths, header)
drawHorizontalRule(builder, columnsWidths)
lastLineHadHR := false
for i := 0; i < len(data); i++ {
drawLine(builder, columnsWidths, data[i].Values)
lastLineHadHR = data[i].HorizontalRuleAfter
if data[i].HorizontalRuleAfter {
drawHorizontalRule(builder, columnsWidths)
}
}
if !lastLineHadHR {
drawHorizontalRule(builder, columnsWidths)
}
return builder.String(), nil
}
func checkValidity(header []string, data []*LineData) error {
if header == nil {
return ErrNilHeader
}
if data == nil {
return ErrNilDataLines
}
if len(data) == 0 && len(header) == 0 {
return ErrEmptySlices
}
maxElemFound := len(header)
for _, ld := range data {
if ld == nil {
return ErrNilLineDataInSlice
}
if ld.Values == nil {
return ErrNilValuesOfLineDataInSlice
}
if maxElemFound < len(ld.Values) {
maxElemFound = len(ld.Values)
}
}
if maxElemFound == 0 {
return ErrEmptySlices
}
return nil
}
func computeColumnsWidths(header []string, data []*LineData) []int {
widths := make([]int, len(header))
for i := 0; i < len(header); i++ {
widths[i] = len(header[i])
}
for i := 0; i < len(data); i++ {
line := data[i]
for j := 0; j < len(line.Values); j++ {
if len(widths) <= j {
widths = append(widths, utf8.RuneCountInString(line.Values[j]))
continue
}
width := utf8.RuneCountInString(line.Values[j])
if widths[j] < width {
widths[j] = width
}
}
}
return widths
}
func drawHorizontalRule(builder *strings.Builder, columnsWidths []int) {
_ = builder.WriteByte('+')
for i := 0; i < len(columnsWidths); i++ {
for j := 0; j < columnsWidths[i]+2; j++ {
_ = builder.WriteByte('-')
}
_ = builder.WriteByte('+')
}
_, _ = builder.Write([]byte{'\r', '\n'})
}
func drawLine(builder *strings.Builder, columnsWidths []int, strings []string) {
_ = builder.WriteByte('|')
for i := 0; i < len(columnsWidths); i++ {
_ = builder.WriteByte(' ')
lenStr := 0
if i < len(strings) {
lenStr = utf8.RuneCountInString(strings[i])
_, _ = builder.WriteString(strings[i])
}
for j := lenStr; j < columnsWidths[i]; j++ {
_ = builder.WriteByte(' ')
}
_, _ = builder.Write([]byte{' ', '|'})
}
_, _ = builder.Write([]byte{'\r', '\n'})
}