-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathoption.go
110 lines (98 loc) · 2.69 KB
/
option.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
package tablr
// TableOption represents an option for configuring a Table.
type TableOption func(*Table)
// WithHeaderAlignments sets the alignment for each header.
func WithHeaderAlignments(alignments []Alignment) TableOption {
return func(t *Table) {
copy(t.headerAlignments, alignments)
t.adjustAlignments()
for i, a := range t.headerAlignments {
if !a.IsValid() {
t.headerAlignments[i] = AlignDefault
continue
}
t.headerAlignments[i] = a
}
}
}
// WithHeaderAlignment sets the alignment for a header.
func WithHeaderAlignment(index int, alignment Alignment) TableOption {
return func(t *Table) {
if index < 0 || index >= len(t.columns) {
return
}
if !alignment.IsValid() {
alignment = AlignDefault
}
t.headerAlignments[index] = alignment
t.adjustAlignments()
}
}
// WithAlignments sets the alignments for each non-header column.
func WithAlignments(alignments []Alignment) TableOption {
return func(t *Table) {
copy(t.columnAlignments, alignments)
t.adjustAlignments()
// Update the alignments for headers to make sure the column alignments
// take precedence over the default alignments.
for i, a := range t.columnAlignments {
if !a.IsValid() {
t.columnAlignments[i] = AlignDefault
t.headerAlignments[i] = AlignDefault
continue
}
t.columnAlignments[i] = a
if t.headerAlignments[i] == AlignDefault {
t.headerAlignments[i] = a
}
}
}
}
// WithAlignment sets the alignment for a non-header column.
func WithAlignment(index int, alignment Alignment) TableOption {
return func(t *Table) {
if index < 0 || index >= len(t.columns) {
return
}
if !alignment.IsValid() {
alignment = AlignDefault
}
t.columnAlignments[index] = alignment
if t.headerAlignments[index] == AlignDefault {
t.headerAlignments[index] = alignment
}
t.adjustAlignments()
}
}
// WithMinColumnWidths sets the minimum widths for multiple columns.
func WithMinColumWidths(minColumnWidths []int) TableOption {
return func(t *Table) {
t.columnMinWidths = minColumnWidths
t.adjustColumnWidths()
}
}
// WithMinColumnWidth sets the minimum width for a column.
func WithMinColumnWidth(index, minWidth int) TableOption {
return func(t *Table) {
if index < 0 || index >= len(t.columns) {
return
}
t.columnMinWidths[index] = minWidth
}
}
type column struct {
headerAlignment Alignment
alignment Alignment
}
// ColumnOption represents an option for configuring a Column.
type ColumnOption func(*column)
func WithColumnAlignment(alignment Alignment) ColumnOption {
return func(c *column) {
c.alignment = alignment
}
}
func WithColumnHeaderAlignment(alignment Alignment) ColumnOption {
return func(c *column) {
c.headerAlignment = alignment
}
}