forked from pterm/pterm
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtable_printer.go
233 lines (196 loc) · 6.54 KB
/
table_printer.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
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
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
package pterm
import (
"encoding/csv"
"io"
"strings"
"github.com/mattn/go-runewidth"
"github.com/pterm/pterm/internal"
)
// DefaultTable contains standards, which can be used to print a TablePrinter.
var DefaultTable = TablePrinter{
Style: &ThemeDefault.TableStyle,
HeaderStyle: &ThemeDefault.TableHeaderStyle,
HeaderRowSeparator: "",
HeaderRowSeparatorStyle: &ThemeDefault.TableSeparatorStyle,
Separator: " | ",
SeparatorStyle: &ThemeDefault.TableSeparatorStyle,
RowSeparator: "",
RowSeparatorStyle: &ThemeDefault.TableSeparatorStyle,
LeftAlignment: true,
RightAlignment: false,
}
// TableData is the type that contains the data of a TablePrinter.
type TableData [][]string
// TablePrinter is able to render tables.
type TablePrinter struct {
Style *Style
HasHeader bool
HeaderStyle *Style
HeaderRowSeparator string
HeaderRowSeparatorStyle *Style
Separator string
SeparatorStyle *Style
RowSeparator string
RowSeparatorStyle *Style
Data TableData
Boxed bool
LeftAlignment bool
RightAlignment bool
Writer io.Writer
}
// WithStyle returns a new TablePrinter with a specific Style.
func (p TablePrinter) WithStyle(style *Style) *TablePrinter {
p.Style = style
return &p
}
// WithHasHeader returns a new TablePrinter, where the first line is marked as a header.
func (p TablePrinter) WithHasHeader(b ...bool) *TablePrinter {
p.HasHeader = internal.WithBoolean(b)
return &p
}
// WithHeaderStyle returns a new TablePrinter with a specific HeaderStyle.
func (p TablePrinter) WithHeaderStyle(style *Style) *TablePrinter {
p.HeaderStyle = style
return &p
}
// WithHeaderRowSeparator returns a new TablePrinter with a specific header HeaderRowSeparator.
func (p TablePrinter) WithHeaderRowSeparator(separator string) *TablePrinter {
p.HeaderRowSeparator = separator
return &p
}
// WithHeaderRowSeparatorStyle returns a new TablePrinter with a specific header HeaderRowSeparatorStyle.
func (p TablePrinter) WithHeaderRowSeparatorStyle(style *Style) *TablePrinter {
p.HeaderRowSeparatorStyle = style
return &p
}
// WithSeparator returns a new TablePrinter with a specific separator.
func (p TablePrinter) WithSeparator(separator string) *TablePrinter {
p.Separator = separator
return &p
}
// WithSeparatorStyle returns a new TablePrinter with a specific SeparatorStyle.
func (p TablePrinter) WithSeparatorStyle(style *Style) *TablePrinter {
p.SeparatorStyle = style
return &p
}
// WithRowSeparator returns a new TablePrinter with a specific RowSeparator.
func (p TablePrinter) WithRowSeparator(separator string) *TablePrinter {
p.RowSeparator = separator
return &p
}
// WithRowSeparatorStyle returns a new TablePrinter with a specific RowSeparatorStyle.
func (p TablePrinter) WithRowSeparatorStyle(style *Style) *TablePrinter {
p.RowSeparatorStyle = style
return &p
}
// WithData returns a new TablePrinter with specific Data.
func (p TablePrinter) WithData(data [][]string) *TablePrinter {
p.Data = data
return &p
}
// WithCSVReader return a new TablePrinter with specified Data extracted from CSV.
func (p TablePrinter) WithCSVReader(reader *csv.Reader) *TablePrinter {
if records, err := reader.ReadAll(); err == nil {
p.Data = records
}
return &p
}
// WithBoxed returns a new TablePrinter with a box around the table.
func (p TablePrinter) WithBoxed(b ...bool) *TablePrinter {
p.Boxed = internal.WithBoolean(b)
return &p
}
// WithLeftAlignment returns a new TablePrinter with left alignment.
func (p TablePrinter) WithLeftAlignment(b ...bool) *TablePrinter {
b2 := internal.WithBoolean(b)
p.LeftAlignment = b2
p.RightAlignment = false
return &p
}
// WithRightAlignment returns a new TablePrinter with right alignment.
func (p TablePrinter) WithRightAlignment(b ...bool) *TablePrinter {
b2 := internal.WithBoolean(b)
p.LeftAlignment = false
p.RightAlignment = b2
return &p
}
// WithWriter sets the Writer.
func (p TablePrinter) WithWriter(writer io.Writer) *TablePrinter {
p.Writer = writer
return &p
}
// Srender renders the TablePrinter as a string.
func (p TablePrinter) Srender() (string, error) {
if p.Style == nil {
p.Style = NewStyle()
}
if p.SeparatorStyle == nil {
p.SeparatorStyle = NewStyle()
}
if p.HeaderStyle == nil {
p.HeaderStyle = NewStyle()
}
if p.HeaderRowSeparatorStyle == nil {
p.HeaderRowSeparatorStyle = NewStyle()
}
if p.RowSeparatorStyle == nil {
p.RowSeparatorStyle = NewStyle()
}
var ret string
maxColumnWidth := make(map[int]int)
for _, row := range p.Data {
for ci, column := range row {
columnLength := runewidth.StringWidth(RemoveColorFromString(column))
if columnLength > maxColumnWidth[ci] {
maxColumnWidth[ci] = columnLength
}
}
}
for ri, row := range p.Data {
rowWidth := 0
for ci, column := range row {
columnString := p.createColumnString(column, maxColumnWidth[ci])
rowWidth += runewidth.StringWidth(RemoveColorFromString(columnString))
if ci != len(row) && ci != 0 {
ret += p.Style.Sprint(p.SeparatorStyle.Sprint(p.Separator))
rowWidth += runewidth.StringWidth(RemoveColorFromString(p.SeparatorStyle.Sprint(p.Separator)))
}
if p.HasHeader && ri == 0 {
ret += p.Style.Sprint(p.HeaderStyle.Sprint(columnString))
} else {
ret += p.Style.Sprint(columnString)
}
}
if p.HasHeader && ri == 0 && p.HeaderRowSeparator != "" {
ret += p.createHeaderRowSeparatorString(rowWidth)
}
if ri != len(p.Data)-1 && ri != 0 && p.RowSeparator != "" {
ret += p.createRowSeparatorString(rowWidth)
}
ret += "\n"
}
ret = strings.TrimSuffix(ret, "\n")
if p.Boxed {
ret = DefaultBox.Sprint(ret)
}
return ret, nil
}
func (p TablePrinter) createColumnString(data string, maxColumnWidth int) string {
columnLength := runewidth.StringWidth(RemoveColorFromString(data))
if p.RightAlignment {
return strings.Repeat(" ", maxColumnWidth-columnLength) + data
}
return data + strings.Repeat(" ", maxColumnWidth-columnLength)
}
func (p TablePrinter) createHeaderRowSeparatorString(rowWidth int) string {
return "\n" + p.Style.Sprint(p.HeaderRowSeparatorStyle.Sprint(strings.Repeat(p.HeaderRowSeparator, rowWidth)))
}
func (p TablePrinter) createRowSeparatorString(rowWidth int) string {
return "\n" + p.Style.Sprint(p.RowSeparatorStyle.Sprint(strings.Repeat(p.RowSeparator, rowWidth)))
}
// Render prints the TablePrinter to the terminal.
func (p TablePrinter) Render() error {
s, _ := p.Srender()
Fprintln(p.Writer, s)
return nil
}