Skip to content

Commit 6ea4b17

Browse files
table: ignore non-printable characters when suppressing empty columns (#327)
This change ensures that non-printable characters (e.g., text direction markers) are ignored when determining whether to suppress empty columns. This is required to use FormatOptions.Direction in conjunction with SuppressEmptyColumns().
1 parent b0affc2 commit 6ea4b17

File tree

2 files changed

+26
-1
lines changed

2 files changed

+26
-1
lines changed

table/render_init.go

+10-1
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@ package table
33
import (
44
"fmt"
55
"strings"
6+
"unicode"
67

78
"github.com/jedib0t/go-pretty/v6/text"
89
)
@@ -255,7 +256,15 @@ func (t *Table) initForRenderSuppressColumns() {
255256
shouldSuppressColumn := func(colIdx int) bool {
256257
for _, row := range t.rows {
257258
if colIdx < len(row) && row[colIdx] != "" {
258-
return false
259+
// Columns may contain non-printable characters. For example
260+
// the text.Direction modifiers. These should not be considered
261+
// when deciding to suppress a column.
262+
for _, r := range row[colIdx] {
263+
if unicode.IsPrint(r) {
264+
return false
265+
}
266+
}
267+
return true
259268
}
260269
}
261270
return true

table/render_test.go

+16
Original file line numberDiff line numberDiff line change
@@ -1165,6 +1165,22 @@ func TestTable_Render_SuppressEmptyColumns(t *testing.T) {
11651165
├─────┼────────────┼────────┼─────────────────────────────┤
11661166
│ │ │ 10000 │ │
11671167
└─────┴────────────┴────────┴─────────────────────────────┘`)
1168+
1169+
tw.SetStyle(Style{
1170+
Format: FormatOptions{
1171+
Direction: text.LeftToRight,
1172+
},
1173+
})
1174+
// Columns with non-printable characters should still be suppressed.
1175+
compareOutput(t, tw.Render(), strings.Join([]string{
1176+
"\u202a \u202a#\u202aFirst Name\u202aSalary ",
1177+
"\u202a \u202a1\u202aArya \u202a3000 ",
1178+
"\u202a \u202a20\u202aJon \u202a2000\u202aYou know nothing, Jon Snow!",
1179+
"\u202a\u202a300\u202aTyrion \u202a5000 ",
1180+
"\u202a \u202a11\u202aSansa \u202a6000 ",
1181+
"\u202a \u202a\u202a \u202a10000 ",
1182+
}, "\n"))
1183+
11681184
}
11691185

11701186
func TestTable_Render_TableWithinTable(t *testing.T) {

0 commit comments

Comments
 (0)