Repository navigation
Expand file tree
/
Copy pathtable.go
More file actions
139 lines (126 loc) · 3.16 KB
/
Copy pathtable.go
File metadata and controls
139 lines (126 loc) · 3.16 KB
1
2
3
4
5
6
7
8
9
10
11
12
E71D
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
package table
import (
"fmt"
"io"
"sort"
"strings"
"unicode/utf8"
)
type (
// Table represents a table of data to be rendered.
Table struct {
Columns []Column
Data []Row
Sort []int
ColumnSpacing string
}
// Row is a single row of data in a table.
Row = []string
// Column represents metadata about a column in a table.
Column struct {
Header string
Width int
// If false, render this column.
Hide bool
// If true, set the width to the widest value in this column.
Flexible bool
LeftAlign bool
}
)
const defaultColumnSpacing = " "
// NewTable creates a new table with the given columns and rows.
func NewTable(cols []Column, data []Row) Table {
return Table{
Columns: cols,
Data: data,
Sort: []int{},
ColumnSpacing: defaultColumnSpacing,
}
}
// NewColumn creates a new flexible column with the given name.
func NewColumn(header string) Column {
return Column{
Header: header,
Flexible: true,
Width: utf8.RuneCountInString(header),
}
}
// WithLeftAlign turns on the left align of this column and returns it.
func (c Column) WithLeftAlign() Column {
c.LeftAlign = true
return c
}
// Render writes the full table to the given Writer.
func (t *Table) Render(w io.Writer) {
columnWidths := t.columnWidths()
t.renderRow(w, t.headerRow(), columnWidths)
t.sort()
for _, row := range t.Data {
t.renderRow(w, row, columnWidths)
}
}
func (t *Table) columnWidths() []int {
widths := make([]int, len(t.Columns))
for c, col := range t.Columns {
width := col.Width
if col.Flexible {
for _, row := range t.Data {
if utf8.RuneCountInString(row[c]) > width {
width = utf8.RuneCountInString(row[c])
}
}
}
widths[c] = width
}
return widths
}
func (t *Table) sort() {
if len(t.Sort) == 0 {
return
}
sort.Slice(t.Data, func(i, j int) bool {
for _, sortCol := range t.Sort {
if t.Data[i][sortCol] < t.Data[j][sortCol] {
return true
} else if t.Data[i][sortCol] > t.Data[j][sortCol] {
return false
}
}
return false
})
}
func (t *Table) renderRow(w io.Writer, row Row, columnWidths []int) {
for c, col := range t.Columns {
if col.Hide {
continue
}
value := row[c]
if utf8.RuneCountInString(value) > columnWidths[c] {
value = value[:columnWidths[c]]
}
padding := strings.Repeat(" ", columnWidths[c]-utf8.RuneCountInString(value))
spacing := t.ColumnSpacing
if strings.HasSuffix(value, "─") && c < len(t.Columns)-1 && strings.HasPrefix(row[c+1], "─") {
spacing = "──"
}
if strings.HasPrefix(value, "─") {
padding = strings.Repeat("─", columnWidths[c]-utf8.RuneCountInString(value))
fmt.Fprintf(w, "%s%s%s", padding, value, spacing)
} else if strings.HasSuffix(value, "─") {
padding = strings.Repeat("─", columnWidths[c]-utf8.RuneCountInString(value))
fmt.Fprintf(w, "%s%s%s", value, padding, spacing)
} else if col.LeftAlign {
fmt.Fprintf(w, "%s%s%s", value, padding, spacing)
} else {
fmt.Fprintf(w, "%s%s%s", padding, value, spacing)
}
}
fmt.Fprint(w, "\n")
}
func (t *Table) headerRow() Row {
row := make(Row, len(t.Columns))
for c, col := range t.Columns {
row[c] = col.Header
}
return row
}