Skip to content

Commit

Permalink
table: suppress empty columns if asked to; fixes #124 (#126)
Browse files Browse the repository at this point in the history
  • Loading branch information
jedib0t authored Jun 11, 2020
1 parent fccf2a0 commit 88e6896
Show file tree
Hide file tree
Showing 4 changed files with 73 additions and 0 deletions.
1 change: 1 addition & 0 deletions table/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@ Pretty-print tables into ASCII/Unicode strings.
- Custom (vertical) VAlign per column with multi-line cell support (`ColumnConfig.VAlign*`)
- Mirror output to an `io.Writer` (ex. `os.StdOut`) (`SetOutputMirror`)
- Sort by one or more Columns (`SortBy`)
- Suppress/hide columns with no content (`SuppressEmptyColumns`)
- Customizable Cell rendering per Column (`ColumnConfig.Transformer*`)
- Hide any columns that you don't want displayed (`ColumnConfig.Hidden`)
- Reset Headers/Rows/Footers at will to reuse the same Table Writer (`Reset*`)
Expand Down
38 changes: 38 additions & 0 deletions table/render_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -1140,6 +1140,44 @@ func TestTable_Render_Styles(t *testing.T) {
}
}

func TestTable_Render_SuppressEmptyColumns(t *testing.T) {
tw := NewWriter()
tw.AppendHeader(testHeader)
tw.AppendRows([]Row{
{1, "Arya", "", 3000},
{20, "Jon", "", 2000, "You know nothing, Jon Snow!"},
{300, "Tyrion", "", 5000},
})
tw.AppendRow(Row{11, "Sansa", "", 6000})
tw.AppendFooter(Row{"", "", "TOTAL", 10000})
tw.SetStyle(StyleLight)

expectedOut := `┌─────┬────────────┬───────────┬────────┬─────────────────────────────┐
│ # │ FIRST NAME │ LAST NAME │ SALARY │ │
├─────┼────────────┼───────────┼────────┼─────────────────────────────┤
│ 1 │ Arya │ │ 3000 │ │
│ 20 │ Jon │ │ 2000 │ You know nothing, Jon Snow! │
│ 300 │ Tyrion │ │ 5000 │ │
│ 11 │ Sansa │ │ 6000 │ │
├─────┼────────────┼───────────┼────────┼─────────────────────────────┤
│ │ │ TOTAL │ 10000 │ │
└─────┴────────────┴───────────┴────────┴─────────────────────────────┘`
assert.Equal(t, expectedOut, tw.Render())

tw.SuppressEmptyColumns()
expectedOut = `┌─────┬────────────┬────────┬─────────────────────────────┐
│ # │ FIRST NAME │ SALARY │ │
├─────┼────────────┼────────┼─────────────────────────────┤
│ 1 │ Arya │ 3000 │ │
│ 20 │ Jon │ 2000 │ You know nothing, Jon Snow! │
│ 300 │ Tyrion │ 5000 │ │
│ 11 │ Sansa │ 6000 │ │
├─────┼────────────┼────────┼─────────────────────────────┤
│ │ │ 10000 │ │
└─────┴────────────┴────────┴─────────────────────────────┘`
assert.Equal(t, expectedOut, tw.Render())
}

func TestTable_Render_TableWithinTable(t *testing.T) {
twInner := NewWriter()
twInner.AppendHeader(testHeader)
Expand Down
33 changes: 33 additions & 0 deletions table/table.go
Original file line number Diff line number Diff line change
Expand Up @@ -94,6 +94,9 @@ type Table struct {
sortBy []SortBy
// style contains all the strings used to draw the table, and more
style *Style
// suppressEmptyColumns hides columns which have no content on all regular
// rows
suppressEmptyColumns bool
// title contains the text to appear above the table
title string
}
Expand Down Expand Up @@ -278,6 +281,12 @@ func (t *Table) Style() *Style {
return t.style
}

// SuppressEmptyColumns hides columns when the column is empty in ALL the
// regular rows.
func (t *Table) SuppressEmptyColumns() {
t.suppressEmptyColumns = true
}

func (t *Table) analyzeAndStringify(row Row, hint renderHint) rowStr {
// update t.numColumns if this row is the longest seen till now
if len(row) > t.numColumns {
Expand Down Expand Up @@ -699,6 +708,9 @@ func (t *Table) initForRenderRows() {
// sort the rows as requested
t.initForRenderSortRows()

// suppress columns without any content
t.initForRenderSuppressColumns()

// strip out hidden columns
t.initForRenderHideColumns()
}
Expand Down Expand Up @@ -761,6 +773,27 @@ func (t *Table) initForRenderSortRows() {
}
}

func (t *Table) initForRenderSuppressColumns() {
shouldSuppressColumn := func(colIdx int) bool {
for _, row := range t.rows {
if colIdx < len(row) && row[colIdx] != "" {
return false
}
}
return true
}

if t.suppressEmptyColumns {
for colIdx := 0; colIdx < t.numColumns; colIdx++ {
if shouldSuppressColumn(colIdx) {
cc := t.columnConfigMap[colIdx]
cc.Hidden = true
t.columnConfigMap[colIdx] = cc
}
}
}
}

func (t *Table) isIndexColumn(colIdx int, hint renderHint) bool {
return t.indexColumn == colIdx+1 || hint.isAutoIndexColumn
}
Expand Down
1 change: 1 addition & 0 deletions table/writer.go
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,7 @@ type Writer interface {
SetTitle(format string, a ...interface{})
SortBy(sortBy []SortBy)
Style() *Style
SuppressEmptyColumns()

// deprecated; in favor of Style().HTML.CSSClass
SetHTMLCSSClass(cssClass string)
Expand Down

0 comments on commit 88e6896

Please sign in to comment.