-
Notifications
You must be signed in to change notification settings - Fork 1
/
table.go
119 lines (104 loc) · 2.34 KB
/
table.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
package table
import (
"fmt"
"io"
"math"
"strings"
)
const (
ALIGN_LEFT = 0
ALIGN_CENTER = 1
ALIGN_RIGHT = 2
)
type Table struct {
Out io.Writer
Rows [][]string
ColumnWidths []int
ColumnAlign []int
}
func CreateTable(out io.Writer) Table {
return Table{
Out: out,
}
}
func (t *Table) AddRow(col ...string) {
t.Rows = append(t.Rows, col)
}
func (t *Table) Render() {
// lets figure out the max widths of each column
for _, row := range t.Rows {
for ci, col := range row {
l := len(col)
t.ColumnWidths = ensureArrayCanContain(t.ColumnWidths, ci)
if l > t.ColumnWidths[ci] {
t.ColumnWidths[ci] = l
}
}
}
out := t.Out
for _, row := range t.Rows {
for ci, col := range row {
if ci > 0 {
fmt.Fprint(out, " ")
}
l := t.ColumnWidths[ci]
align := t.GetColumnAlign(ci)
fmt.Fprint(out, pad(col, " ", l, align))
}
fmt.Fprint(out, "\n")
}
}
// SetColumnsAligns sets the alignment of the columns
func (t *Table) SetColumnsAligns(colAligns []int) {
t.ColumnAlign = colAligns
}
// GetColumnAlign return the column alignment
func (t *Table) GetColumnAlign(i int) int {
t.ColumnAlign = ensureArrayCanContain(t.ColumnAlign, i)
return t.ColumnAlign[i]
}
// SetColumnAlign sets the column alignment for the given column index
func (t *Table) SetColumnAlign(i int, align int) {
t.ColumnAlign = ensureArrayCanContain(t.ColumnAlign, i)
t.ColumnAlign[i] = align
}
func ensureArrayCanContain(array []int, idx int) []int {
diff := idx + 1 - len(array)
for i := 0; i < diff; i++ {
array = append(array, 0)
}
return array
}
func pad(s, pad string, width int, align int) string {
switch align {
case ALIGN_CENTER:
return padCenter(s, pad, width)
case ALIGN_RIGHT:
return padLeft(s, pad, width)
default:
return padRight(s, pad, width)
}
}
func padRight(s, pad string, width int) string {
gap := width - len(s)
if gap > 0 {
return s + strings.Repeat(string(pad), gap)
}
return s
}
func padLeft(s, pad string, width int) string {
gap := width - len(s)
if gap > 0 {
return strings.Repeat(string(pad), gap) + s
}
return s
}
func padCenter(s, pad string, width int) string {
gap := width - len(s)
if gap > 0 {
gapLeft := int(math.Ceil(float64(gap / 2)))
gapRight := gap - gapLeft
return strings.Repeat(string(pad), gapLeft) + s + strings.Repeat(string(pad), gapRight)
}
return s
}