Skip to content

Commit ada7500

Browse files
authored
table: defaults for row/header/footer align/valign; fixes #340 (#341)
1 parent 840c578 commit ada7500

File tree

3 files changed

+91
-19
lines changed

3 files changed

+91
-19
lines changed

table/render_test.go

+57-12
Original file line numberDiff line numberDiff line change
@@ -112,20 +112,64 @@ A Song of Ice and Fire`)
112112
}
113113

114114
func TestTable_Render_Align(t *testing.T) {
115-
tw := NewWriter()
116-
tw.AppendHeader(testHeader)
117-
tw.AppendRows(testRows)
118-
tw.AppendRow(Row{500, "Jamie", "Lannister", "Kingslayer", "The things I do for love."})
119-
tw.AppendRow(Row{1000, "Tywin", "Lannister", nil})
120-
tw.AppendFooter(testFooter)
121-
tw.SetColumnConfigs([]ColumnConfig{
122-
{Name: "First Name", Align: text.AlignLeft, AlignHeader: text.AlignLeft, AlignFooter: text.AlignLeft},
123-
{Name: "Last Name", Align: text.AlignRight, AlignHeader: text.AlignRight, AlignFooter: text.AlignRight},
124-
{Name: "Salary", Align: text.AlignAuto, AlignHeader: text.AlignRight, AlignFooter: text.AlignAuto},
125-
{Number: 5, Align: text.AlignJustify, AlignHeader: text.AlignJustify, AlignFooter: text.AlignJustify},
115+
t.Run("defaults", func(t *testing.T) {
116+
tw := NewWriter()
117+
tw.AppendHeader(Row{"#", "First\nName", "Last\nName", "Final\nState", "Misc.\nMulti-line\nNotes"})
118+
tw.AppendRows([]Row{
119+
{1, "Arya", "Stark", ":) 8)"},
120+
{20, "Jon", "Snow", ":( :( :(", "You know nothing, Jon Snow!"},
121+
{300, "Tyrion", "Lannister", ":)"},
122+
})
123+
tw.AppendFooter(Row{"#", "First\nName", "Last\nName", "Final\nState", "Misc.\nMulti-line\nNotes"})
124+
tw.Style().Format = FormatOptions{
125+
FooterAlign: text.AlignRight,
126+
FooterVAlign: text.VAlignTop,
127+
HeaderAlign: text.AlignLeft,
128+
HeaderVAlign: text.VAlignBottom,
129+
RowAlign: text.AlignCenter,
130+
RowVAlign: text.VAlignMiddle,
131+
}
132+
tw.SetColumnConfigs([]ColumnConfig{ // takes precedence
133+
{
134+
Name: "Final\nState",
135+
Align: text.AlignLeft, VAlign: text.VAlignTop,
136+
AlignHeader: text.AlignLeft, VAlignHeader: text.VAlignTop,
137+
AlignFooter: text.AlignLeft, VAlignFooter: text.VAlignBottom,
138+
},
139+
})
140+
141+
compareOutput(t, tw.Render(), `
142+
+-----+--------+-----------+----------+-----------------------------+
143+
| | | | Final | Misc. |
144+
| | First | Last | State | Multi-line |
145+
| # | Name | Name | | Notes |
146+
+-----+--------+-----------+----------+-----------------------------+
147+
| 1 | Arya | Stark | :) 8) | |
148+
| 20 | Jon | Snow | :( :( :( | You know nothing, Jon Snow! |
149+
| 300 | Tyrion | Lannister | :) | |
150+
+-----+--------+-----------+----------+-----------------------------+
151+
| # | First | Last | | Misc. |
152+
| | Name | Name | Final | Multi-line |
153+
| | | | State | Notes |
154+
+-----+--------+-----------+----------+-----------------------------+`)
155+
126156
})
127157

128-
compareOutput(t, tw.Render(), `
158+
t.Run("column overrides", func(t *testing.T) {
159+
tw := NewWriter()
160+
tw.AppendHeader(testHeader)
161+
tw.AppendRows(testRows)
162+
tw.AppendRow(Row{500, "Jamie", "Lannister", "Kingslayer", "The things I do for love."})
163+
tw.AppendRow(Row{1000, "Tywin", "Lannister", nil})
164+
tw.AppendFooter(testFooter)
165+
tw.SetColumnConfigs([]ColumnConfig{
166+
{Name: "First Name", Align: text.AlignLeft, AlignHeader: text.AlignLeft, AlignFooter: text.AlignLeft},
167+
{Name: "Last Name", Align: text.AlignRight, AlignHeader: text.AlignRight, AlignFooter: text.AlignRight},
168+
{Name: "Salary", Align: text.AlignAuto, AlignHeader: text.AlignRight, AlignFooter: text.AlignAuto},
169+
{Number: 5, Align: text.AlignJustify, AlignHeader: text.AlignJustify, AlignFooter: text.AlignJustify},
170+
})
171+
172+
compareOutput(t, tw.Render(), `
129173
+------+------------+-----------+------------+-----------------------------+
130174
| # | FIRST NAME | LAST NAME | SALARY | |
131175
+------+------------+-----------+------------+-----------------------------+
@@ -137,6 +181,7 @@ func TestTable_Render_Align(t *testing.T) {
137181
+------+------------+-----------+------------+-----------------------------+
138182
| | | TOTAL | 10000 | |
139183
+------+------------+-----------+------------+-----------------------------+`)
184+
})
140185
}
141186

142187
func TestTable_Render_AutoIndex(t *testing.T) {

table/style.go

+19-7
Original file line numberDiff line numberDiff line change
@@ -690,17 +690,29 @@ var (
690690

691691
// FormatOptions defines the text-formatting to perform on parts of the Table.
692692
type FormatOptions struct {
693-
Direction text.Direction // (forced) BiDi direction for each Column
694-
Footer text.Format // footer row(s) text format
695-
Header text.Format // header row(s) text format
696-
Row text.Format // (data) row(s) text format
693+
Direction text.Direction // (forced) BiDi direction for each Column
694+
Footer text.Format // default text format
695+
FooterAlign text.Align // default horizontal align
696+
FooterVAlign text.VAlign // default vertical align
697+
Header text.Format // default text format
698+
HeaderAlign text.Align // default horizontal align
699+
HeaderVAlign text.VAlign // default vertical align
700+
Row text.Format // default text format
701+
RowAlign text.Align // default horizontal align
702+
RowVAlign text.VAlign // default vertical align
697703
}
698704

699705
// FormatOptionsDefault defines sensible formatting options.
700706
var FormatOptionsDefault = FormatOptions{
701-
Footer: text.FormatUpper,
702-
Header: text.FormatUpper,
703-
Row: text.FormatDefault,
707+
Footer: text.FormatUpper,
708+
FooterAlign: text.AlignDefault,
709+
FooterVAlign: text.VAlignDefault,
710+
Header: text.FormatUpper,
711+
HeaderAlign: text.AlignDefault,
712+
HeaderVAlign: text.VAlignDefault,
713+
Row: text.FormatDefault,
714+
RowAlign: text.AlignDefault,
715+
RowVAlign: text.VAlignDefault,
704716
}
705717

706718
// HTMLOptions defines the global options to control HTML rendering.

table/table.go

+15
Original file line numberDiff line numberDiff line change
@@ -356,6 +356,12 @@ func (t *Table) getAlign(colIdx int, hint renderHint) text.Align {
356356
align = text.AlignRight
357357
} else if hint.isAutoIndexRow {
358358
align = text.AlignCenter
359+
} else if hint.isHeaderRow {
360+
align = t.style.Format.HeaderAlign
361+
} else if hint.isFooterRow {
362+
align = t.style.Format.FooterAlign
363+
} else {
364+
align = t.style.Format.RowAlign
359365
}
360366
}
361367
return align
@@ -671,6 +677,15 @@ func (t *Table) getVAlign(colIdx int, hint renderHint) text.VAlign {
671677
vAlign = cfg.VAlign
672678
}
673679
}
680+
if vAlign == text.VAlignDefault {
681+
if hint.isHeaderRow {
682+
vAlign = t.style.Format.HeaderVAlign
683+
} else if hint.isFooterRow {
684+
vAlign = t.style.Format.FooterVAlign
685+
} else {
686+
vAlign = t.style.Format.RowVAlign
687+
}
688+
}
674689
return vAlign
675690
}
676691

0 commit comments

Comments
 (0)