Skip to content

Commit

Permalink
table: adjust columns to use least space when merged; fixes #226 (#238)
Browse files Browse the repository at this point in the history
  • Loading branch information
jedib0t committed Nov 5, 2022
1 parent e88b800 commit 006d8ca
Show file tree
Hide file tree
Showing 7 changed files with 557 additions and 187 deletions.
18 changes: 12 additions & 6 deletions table/render_csv_test.go
Expand Up @@ -17,7 +17,8 @@ func TestTable_RenderCSV(t *testing.T) {
tw.SetCaption(testCaption)
tw.SetTitle(testTitle1)

compareOutput(t, tw.RenderCSV(), `Game of Thrones
compareOutput(t, tw.RenderCSV(), `
Game of Thrones
#,First Name,Last Name,Salary,
1,Arya,Stark,3000,
20,Jon,Snow,2000,"You know nothing\, Jon Snow!"
Expand Down Expand Up @@ -49,7 +50,8 @@ func TestTable_RenderCSV_AutoIndex(t *testing.T) {
tw.SetAutoIndex(true)
tw.SetStyle(StyleLight)

compareOutput(t, tw.RenderCSV(), `,A,B,C,D,E,F,G,H,I,J
compareOutput(t, tw.RenderCSV(), `
,A,B,C,D,E,F,G,H,I,J
1,A1,B1,C1,D1,E1,F1,G1,H1,I1,J1
2,A2,B2,C2,D2,E2,F2,G2,H2,I2,J2
3,A3,B3,C3,D3,E3,F3,G3,H3,I3,J3
Expand Down Expand Up @@ -88,7 +90,8 @@ func TestTable_RenderCSV_HiddenColumns(t *testing.T) {
t.Run("first column hidden", func(t *testing.T) {
tw.SetColumnConfigs(generateColumnConfigsWithHiddenColumns([]int{0}))

compareOutput(t, tw.RenderCSV(), `First Name,Last Name,Salary,
compareOutput(t, tw.RenderCSV(), `
First Name,Last Name,Salary,
>>Tyrion,Lannister<<,5013,
>>Arya,Stark<<,3013,
>>Jon,Snow<<,2013,"~You know nothing\, Jon Snow!~"
Expand All @@ -98,7 +101,8 @@ func TestTable_RenderCSV_HiddenColumns(t *testing.T) {
t.Run("column hidden in the middle", func(t *testing.T) {
tw.SetColumnConfigs(generateColumnConfigsWithHiddenColumns([]int{1}))

compareOutput(t, tw.RenderCSV(), `#,Last Name,Salary,
compareOutput(t, tw.RenderCSV(), `
#,Last Name,Salary,
307,Lannister<<,5013,
8,Stark<<,3013,
27,Snow<<,2013,"~You know nothing\, Jon Snow!~"
Expand All @@ -108,7 +112,8 @@ func TestTable_RenderCSV_HiddenColumns(t *testing.T) {
t.Run("last column hidden", func(t *testing.T) {
tw.SetColumnConfigs(generateColumnConfigsWithHiddenColumns([]int{4}))

compareOutput(t, tw.RenderCSV(), `#,First Name,Last Name,Salary
compareOutput(t, tw.RenderCSV(), `
#,First Name,Last Name,Salary
307,>>Tyrion,Lannister<<,5013
8,>>Arya,Stark<<,3013
27,>>Jon,Snow<<,2013
Expand All @@ -124,7 +129,8 @@ func TestTable_RenderCSV_Sorted(t *testing.T) {
tw.AppendFooter(testFooter)
tw.SortBy([]SortBy{{Name: "Last Name", Mode: Asc}, {Name: "First Name", Mode: Asc}})

compareOutput(t, tw.RenderCSV(), `#,First Name,Last Name,Salary,
compareOutput(t, tw.RenderCSV(), `
#,First Name,Last Name,Salary,
300,Tyrion,Lannister,5000,
20,Jon,Snow,2000,"You know nothing\, Jon Snow!"
1,Arya,Stark,3000,
Expand Down
34 changes: 34 additions & 0 deletions table/render_hint.go
@@ -0,0 +1,34 @@
package table

// renderHint has hints for the Render*() logic
type renderHint struct {
isAutoIndexColumn bool // auto-index column?
isAutoIndexRow bool // auto-index row?
isBorderBottom bool // bottom-border?
isBorderTop bool // top-border?
isFirstRow bool // first-row of header/footer/regular-rows?
isFooterRow bool // footer row?
isHeaderRow bool // header row?
isLastLineOfRow bool // last-line of the current row?
isLastRow bool // last-row of header/footer/regular-rows?
isSeparatorRow bool // separator row?
rowLineNumber int // the line number for a multi-line row
rowNumber int // the row number/index
}

func (h *renderHint) isRegularRow() bool {
return !h.isHeaderRow && !h.isFooterRow
}

func (h *renderHint) isRegularNonSeparatorRow() bool {
return !h.isHeaderRow && !h.isFooterRow && !h.isSeparatorRow
}

func (h *renderHint) isHeaderOrFooterSeparator() bool {
return h.isSeparatorRow && !h.isBorderBottom && !h.isBorderTop &&
((h.isHeaderRow && !h.isLastRow) || (h.isFooterRow && (!h.isFirstRow || h.rowNumber > 0)))
}

func (h *renderHint) isLastLineOfLastRow() bool {
return h.isLastLineOfRow && h.isLastRow
}
24 changes: 16 additions & 8 deletions table/render_html_test.go
Expand Up @@ -26,7 +26,8 @@ func TestTable_RenderHTML(t *testing.T) {
Format: text.FormatTitle,
}

compareOutput(t, tw.RenderHTML(), `<table class="go-pretty-table">
compareOutput(t, tw.RenderHTML(), `
<table class="go-pretty-table">
<caption class="title" align="left" class="bg-black bold fg-hi-blue">Game Of Thrones</caption>
<thead>
<tr>
Expand Down Expand Up @@ -100,7 +101,8 @@ func TestTable_RenderHTML_AutoIndex(t *testing.T) {
tw.SetAutoIndex(true)
tw.SetStyle(StyleLight)

compareOutput(t, tw.RenderHTML(), `<table class="go-pretty-table">
compareOutput(t, tw.RenderHTML(), `
<table class="go-pretty-table">
<thead>
<tr>
<th>&nbsp;</th>
Expand Down Expand Up @@ -178,7 +180,8 @@ func TestTable_RenderHTML_Colored(t *testing.T) {
},
})

compareOutput(t, tw.RenderHTML(), `<table class="go-pretty-table-colored">
compareOutput(t, tw.RenderHTML(), `
<table class="go-pretty-table-colored">
<caption class="title">Game of Thrones</caption>
<thead>
<tr>
Expand Down Expand Up @@ -248,7 +251,8 @@ func TestTable_RenderHTML_CustomStyle(t *testing.T) {
}
tw.SetOutputMirror(nil)

compareOutput(t, tw.RenderHTML(), `<table class="game-of-thrones">
compareOutput(t, tw.RenderHTML(), `
<table class="game-of-thrones">
<thead>
<tr>
<th><!-- test -->&nbsp;</th>
Expand Down Expand Up @@ -323,7 +327,8 @@ func TestTable_RenderHTML_HiddenColumns(t *testing.T) {
t.Run("first column hidden", func(t *testing.T) {
tw.SetColumnConfigs(generateColumnConfigsWithHiddenColumns([]int{0}))

compareOutput(t, tw.RenderHTML(), `<table class="go-pretty-table">
compareOutput(t, tw.RenderHTML(), `
<table class="go-pretty-table">
<thead>
<tr>
<th>First Name</th>
Expand Down Expand Up @@ -366,7 +371,8 @@ func TestTable_RenderHTML_HiddenColumns(t *testing.T) {
t.Run("column hidden in the middle", func(t *testing.T) {
tw.SetColumnConfigs(generateColumnConfigsWithHiddenColumns([]int{1}))

compareOutput(t, tw.RenderHTML(), `<table class="go-pretty-table">
compareOutput(t, tw.RenderHTML(), `
<table class="go-pretty-table">
<thead>
<tr>
<th align="right">#</th>
Expand Down Expand Up @@ -409,7 +415,8 @@ func TestTable_RenderHTML_HiddenColumns(t *testing.T) {
t.Run("last column hidden", func(t *testing.T) {
tw.SetColumnConfigs(generateColumnConfigsWithHiddenColumns([]int{4}))

compareOutput(t, tw.RenderHTML(), `<table class="go-pretty-table">
compareOutput(t, tw.RenderHTML(), `
<table class="go-pretty-table">
<thead>
<tr>
<th align="right">#</th>
Expand Down Expand Up @@ -458,7 +465,8 @@ func TestTable_RenderHTML_Sorted(t *testing.T) {
tw.AppendFooter(testFooter)
tw.SortBy([]SortBy{{Name: "Last Name", Mode: Asc}, {Name: "First Name", Mode: Asc}})

compareOutput(t, tw.RenderHTML(), `<table class="go-pretty-table">
compareOutput(t, tw.RenderHTML(), `
<table class="go-pretty-table">
<thead>
<tr>
<th align="right">#</th>
Expand Down
18 changes: 12 additions & 6 deletions table/render_markdown_test.go
Expand Up @@ -17,7 +17,8 @@ func TestTable_RenderMarkdown(t *testing.T) {
tw.SetCaption(testCaption)
tw.SetTitle(testTitle1)

compareOutput(t, tw.RenderMarkdown(), `# Game of Thrones
compareOutput(t, tw.RenderMarkdown(), `
# Game of Thrones
| # | First Name | Last Name | Salary | |
| ---:| --- | --- | ---:| --- |
| 1 | Arya | Stark | 3000 | |
Expand Down Expand Up @@ -48,7 +49,8 @@ func TestTable_RenderMarkdown_AutoIndex(t *testing.T) {
tw.SetAutoIndex(true)
tw.SetStyle(StyleLight)

compareOutput(t, tw.RenderMarkdown(), `| | A | B | C | D | E | F | G | H | I | J |
compareOutput(t, tw.RenderMarkdown(), `
| | A | B | C | D | E | F | G | H | I | J |
| ---:| --- | --- | --- | --- | --- | --- | --- | --- | --- | --- |
| 1 | A1 | B1 | C1 | D1 | E1 | F1 | G1 | H1 | I1 | J1 |
| 2 | A2 | B2 | C2 | D2 | E2 | F2 | G2 | H2 | I2 | J2 |
Expand Down Expand Up @@ -88,7 +90,8 @@ func TestTable_RenderMarkdown_HiddenColumns(t *testing.T) {
t.Run("first column hidden", func(t *testing.T) {
tw.SetColumnConfigs(generateColumnConfigsWithHiddenColumns([]int{0}))

compareOutput(t, tw.RenderMarkdown(), `| First Name | Last Name | Salary | |
compareOutput(t, tw.RenderMarkdown(), `
| First Name | Last Name | Salary | |
| --- | --- | ---:| --- |
| >>Tyrion | Lannister<< | 5013 | |
| >>Arya | Stark<< | 3013 | |
Expand All @@ -99,7 +102,8 @@ func TestTable_RenderMarkdown_HiddenColumns(t *testing.T) {
t.Run("column hidden in the middle", func(t *testing.T) {
tw.SetColumnConfigs(generateColumnConfigsWithHiddenColumns([]int{1}))

compareOutput(t, tw.RenderMarkdown(), `| # | Last Name | Salary | |
compareOutput(t, tw.RenderMarkdown(), `
| # | Last Name | Salary | |
| ---:| --- | ---:| --- |
| 307 | Lannister<< | 5013 | |
| 8 | Stark<< | 3013 | |
Expand All @@ -110,7 +114,8 @@ func TestTable_RenderMarkdown_HiddenColumns(t *testing.T) {
t.Run("last column hidden", func(t *testing.T) {
tw.SetColumnConfigs(generateColumnConfigsWithHiddenColumns([]int{4}))

compareOutput(t, tw.RenderMarkdown(), `| # | First Name | Last Name | Salary |
compareOutput(t, tw.RenderMarkdown(), `
| # | First Name | Last Name | Salary |
| ---:| --- | --- | ---:|
| 307 | >>Tyrion | Lannister<< | 5013 |
| 8 | >>Arya | Stark<< | 3013 |
Expand All @@ -127,7 +132,8 @@ func TestTable_RendeMarkdown_Sorted(t *testing.T) {
tw.AppendFooter(testFooter)
tw.SortBy([]SortBy{{Name: "Last Name", Mode: Asc}, {Name: "First Name", Mode: Asc}})

compareOutput(t, tw.RenderMarkdown(), `| # | First Name | Last Name | Salary | |
compareOutput(t, tw.RenderMarkdown(), `
| # | First Name | Last Name | Salary | |
| ---:| --- | --- | ---:| --- |
| 300 | Tyrion | Lannister | 5000 | |
| 20 | Jon | Snow | 2000 | You know nothing, Jon Snow! |
Expand Down

0 comments on commit 006d8ca

Please sign in to comment.