Skip to content

Commit

Permalink
feat: add suppressLastColumnSpaces to remove all trailing spaces in t…
Browse files Browse the repository at this point in the history
…he final column
  • Loading branch information
shreddedbacon committed Jan 3, 2024
1 parent 63a8784 commit 4143601
Show file tree
Hide file tree
Showing 6 changed files with 50 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 @@ -21,6 +21,7 @@ Pretty-print tables into ASCII/Unicode strings.
- Mirror output to an `io.Writer` (ex. `os.StdOut`) (`SetOutputMirror`)
- Sort by one or more Columns (`SortBy`)
- Suppress/hide columns with no content (`SuppressEmptyColumns`)
- Suppress trailing spaces in the last column (`SuppressLastColumnSpaces`)
- 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
6 changes: 6 additions & 0 deletions table/render_init.go
Original file line number Diff line number Diff line change
Expand Up @@ -57,6 +57,12 @@ func (t *Table) extractMaxColumnLengths(rows []rowStr, hint renderHint) {

func (t *Table) extractMaxColumnLengthsFromRow(row rowStr, mci mergedColumnIndices) {
for colIdx, colStr := range row {
// if this is the last column in the row, and suppressLastColumnSpaces is true
if colIdx == len(row)-1 && t.suppressLastColumnSpaces {
// set the column max to 0 so no additional padding is added to the column
t.maxColumnLengths[colIdx] = 0
continue
}
longestLineLen := text.LongestLineLen(colStr)
maxColWidth := t.getColumnWidthMax(colIdx)
if maxColWidth > 0 && maxColWidth < longestLineLen {
Expand Down
33 changes: 33 additions & 0 deletions table/render_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -1175,3 +1175,36 @@ func TestTable_Render_WidthEnforcer(t *testing.T) {
| R123 | Small name | 2021-04-19 13:37 | Abcdefghijklmnopqrstuvwxyz |
+------+----------------------+------------------+----------------------------+`)
}

func TestTable_Render_SuppressLastColumnSpaces(t *testing.T) {
tw := NewWriter()
tw.AppendHeader(testHeader2)
tw.AppendRows([]Row{
{"U2", "Hey", "2021-04-19 13:37", "Yuh yuh yuh"},
{"S12", "Uhhhh", "2021-04-19 13:37", "Some dummy data here"},
{"R123", "Lobsters", "2021-04-19 13:37", "I like lobsters"},
{"R123", "Some big name here and it's pretty big", "2021-04-19 13:37", "Abcdefghijklmnopqrstuvwxyz"},
{"R123", "Small name", "2021-04-19 13:37", "Abcdefghijklmnopqrstuvwxyz"},
})
tw.SetStyle(StyleDefault)
tw.Style().Options = OptionsNoBordersAndSeparators
tw.Style().Box.PaddingLeft = "" // trim left space
tw.Style().Box.PaddingRight = "\t" // pad right with tab

compareOutput(t, tw.Render(), `
ID TEXT1 DATE TEXT2
U2 Hey 2021-04-19 13:37 Yuh yuh yuh
S12 Uhhhh 2021-04-19 13:37 Some dummy data here
R123 Lobsters 2021-04-19 13:37 I like lobsters
R123 Some big name here and it's pretty big 2021-04-19 13:37 Abcdefghijklmnopqrstuvwxyz
R123 Small name 2021-04-19 13:37 Abcdefghijklmnopqrstuvwxyz `)

tw.SuppressLastColumnSpaces()
compareOutput(t, tw.Render(), `
ID TEXT1 DATE TEXT2
U2 Hey 2021-04-19 13:37 Yuh yuh yuh
S12 Uhhhh 2021-04-19 13:37 Some dummy data here
R123 Lobsters 2021-04-19 13:37 I like lobsters
R123 Some big name here and it's pretty big 2021-04-19 13:37 Abcdefghijklmnopqrstuvwxyz
R123 Small name 2021-04-19 13:37 Abcdefghijklmnopqrstuvwxyz `)
}
8 changes: 8 additions & 0 deletions table/table.go
Original file line number Diff line number Diff line change
Expand Up @@ -106,6 +106,8 @@ type Table struct {
// suppressEmptyColumns hides columns which have no content on all regular
// rows
suppressEmptyColumns bool
// suppressLastColumnSpaces removes all trailing spaces from the end of the last column
suppressLastColumnSpaces bool
// title contains the text to appear above the table
title string
}
Expand Down Expand Up @@ -297,6 +299,12 @@ func (t *Table) SuppressEmptyColumns() {
t.suppressEmptyColumns = true
}

// SuppressLastColumnSpaces removes all trailing spaces from the end of the last column
// this is useful when OptionsNoBordersAndSeparators is used
func (t *Table) SuppressLastColumnSpaces() {
t.suppressLastColumnSpaces = true
}

func (t *Table) getAlign(colIdx int, hint renderHint) text.Align {
align := text.AlignDefault
if cfg, ok := t.columnConfigMap[colIdx]; ok {
Expand Down
1 change: 1 addition & 0 deletions table/table_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@ var (
testFooterMultiLine = Row{"", "", "Total\nSalary", 10000}
testHeader = Row{"#", "First Name", "Last Name", "Salary"}
testHeaderMultiLine = Row{"#", "First\nName", "Last\nName", "Salary"}
testHeader2 = Row{"ID", "Text1", "Date", "Text2"}
testRows = []Row{
{1, "Arya", "Stark", 3000},
{20, "Jon", "Snow", 2000, "You know nothing, Jon Snow!"},
Expand Down
1 change: 1 addition & 0 deletions table/writer.go
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,7 @@ type Writer interface {
SortBy(sortBy []SortBy)
Style() *Style
SuppressEmptyColumns()
SuppressLastColumnSpaces()

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

0 comments on commit 4143601

Please sign in to comment.