Skip to content

Commit 3851e7e

Browse files
committed
Update #255
1 parent f6a284d commit 3851e7e

File tree

3 files changed

+109
-16
lines changed

3 files changed

+109
-16
lines changed

β€Žconfig.go

Lines changed: 32 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -8,10 +8,16 @@ import (
88
"reflect" // Reflection for type handling
99
)
1010

11+
type Control struct {
12+
Hide tw.State
13+
}
14+
1115
// Behavior defines table behavior settings that control features like auto-hiding columns and trimming spaces.
1216
type Behavior struct {
1317
AutoHide tw.State // Controls whether empty columns are automatically hidden (ignored in streaming mode)
1418
TrimSpace tw.State // Controls whether leading/trailing spaces are trimmed from cell content
19+
Header Control
20+
Footer Control
1521
}
1622

1723
// ColumnConfigBuilder is used to configure settings for a specific column across all table sections (header, row, footer).
@@ -908,18 +914,40 @@ func WithSymbols(symbols tw.Symbols) Option {
908914
}
909915
}
910916

911-
// WithTrimSpace enables or disables automatic trimming of leading/trailing spaces.
917+
// WithTrimSpace sets whether leading and trailing spaces are automatically trimmed.
912918
// Logs the change if debugging is enabled.
913919
func WithTrimSpace(state tw.State) Option {
914920
return func(target *Table) {
915921
target.config.Behavior.TrimSpace = state
916922
if target.logger != nil {
917-
target.logger.Debugf("Option: WithAutoHide applied to Table: %v", state)
923+
target.logger.Debugf("Option: WithTrimSpace applied to Table: %v", state)
924+
}
925+
}
926+
}
927+
928+
// WithHeaderControl sets the control behavior for the table header.
929+
// Logs the change if debugging is enabled.
930+
func WithHeaderControl(control Control) Option {
931+
return func(target *Table) {
932+
target.config.Behavior.Header = control
933+
if target.logger != nil {
934+
target.logger.Debugf("Option: WithHeaderControl applied to Table: %v", control) // Fixed 'state' to 'control'
935+
}
936+
}
937+
}
938+
939+
// WithFooterControl sets the control behavior for the table footer.
940+
// Logs the change if debugging is enabled.
941+
func WithFooterControl(control Control) Option {
942+
return func(target *Table) {
943+
target.config.Behavior.Footer = control
944+
if target.logger != nil {
945+
target.logger.Debugf("Option: WithFooterControl applied to Table: %v", control) // Fixed log message and 'state' to 'control'
918946
}
919947
}
920948
}
921949

922-
// WithAlignment helps to set default alignments
950+
// WithAlignment sets the default column alignment for the header, rows, and footer.
923951
func WithAlignment(alignment tw.Alignment) Option {
924952
return func(target *Table) {
925953
target.config.Header.ColumnAligns = alignment
@@ -928,7 +956,7 @@ func WithAlignment(alignment tw.Alignment) Option {
928956
}
929957
}
930958

931-
// WithPadding helps to set global padding
959+
// WithPadding sets the global padding for the header, rows, and footer.
932960
func WithPadding(padding tw.Padding) Option {
933961
return func(target *Table) {
934962
target.config.Header.Padding.Global = padding

β€Žtablewriter.go

Lines changed: 19 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -266,8 +266,12 @@ func (t *Table) Debug() *bytes.Buffer {
266266
// In streaming mode, this processes and renders the header immediately.
267267
func (t *Table) Header(elements ...any) {
268268
t.ensureInitialized()
269-
t.logger.Debugf("Header() method called with raw variadic elements: %v (len %d). Streaming: %v, Started: %v",
270-
elements, len(elements), t.config.Stream.Enable, t.hasPrinted)
269+
t.logger.Debugf("Header() method called with raw variadic elements: %v (len %d). Streaming: %v, Started: %v", elements, len(elements), t.config.Stream.Enable, t.hasPrinted)
270+
271+
// just forget
272+
if t.config.Behavior.Header.Hide.Enabled() {
273+
return
274+
}
271275

272276
if t.config.Stream.Enable && t.hasPrinted {
273277
// Streaming Path
@@ -300,14 +304,13 @@ func (t *Table) Header(elements ...any) {
300304
preparedHeaderLines := t.prepareContent(headersAsStrings, t.config.Header)
301305
t.headers = preparedHeaderLines // Store directly. Padding to t.maxColumns() will happen in prepareContexts.
302306

303-
t.logger.Debugf("Header set (batch mode), lines stored: %d. First line if exists: %v",
304-
len(t.headers), func() []string {
305-
if len(t.headers) > 0 {
306-
return t.headers[0]
307-
} else {
308-
return nil
309-
}
310-
}())
307+
t.logger.Debugf("Header set (batch mode), lines stored: %d. First line if exists: %v", len(t.headers), func() []string {
308+
if len(t.headers) > 0 {
309+
return t.headers[0]
310+
} else {
311+
return nil
312+
}
313+
}())
311314
}
312315

313316
// Footer sets the table's footer content, padding to match column count.
@@ -318,8 +321,12 @@ func (t *Table) Header(elements ...any) {
318321
// In streaming mode, this processes and stores the footer for rendering by Close().
319322
func (t *Table) Footer(elements ...any) {
320323
t.ensureInitialized()
321-
t.logger.Debugf("Footer() method called with raw variadic elements: %v (len %d). Streaming: %v, Started: %v",
322-
elements, len(elements), t.config.Stream.Enable, t.hasPrinted)
324+
t.logger.Debugf("Footer() method called with raw variadic elements: %v (len %d). Streaming: %v, Started: %v", elements, len(elements), t.config.Stream.Enable, t.hasPrinted)
325+
326+
// just forget
327+
if t.config.Behavior.Footer.Hide.Enabled() {
328+
return
329+
}
323330

324331
if t.config.Stream.Enable && t.hasPrinted {
325332
// Streaming Path

β€Žtests/extra_test.go

Lines changed: 58 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -452,3 +452,61 @@ func TestSpaces(t *testing.T) {
452452
})
453453

454454
}
455+
456+
func TestControl(t *testing.T) {
457+
var buf bytes.Buffer
458+
var data = [][]string{
459+
{"No", "Age", " City"},
460+
{" 1", "25", "New York"},
461+
{"2", "30", "x"},
462+
{" 3", "28", " Lagos"},
463+
}
464+
t.Run("Trim", func(t *testing.T) {
465+
buf.Reset()
466+
table := tablewriter.NewTable(&buf,
467+
tablewriter.WithDebug(false),
468+
tablewriter.WithTrimSpace(tw.On),
469+
tablewriter.WithHeaderControl(tablewriter.Control{Hide: tw.On}),
470+
)
471+
table.Header(data[0])
472+
table.Bulk(data[1:])
473+
table.Render()
474+
475+
expected := `
476+
β”Œβ”€β”€β”€β”¬β”€β”€β”€β”€β”¬β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”
477+
β”‚ 1 β”‚ 25 β”‚ New York β”‚
478+
β”‚ 2 β”‚ 30 β”‚ x β”‚
479+
β”‚ 3 β”‚ 28 β”‚ Lagos β”‚
480+
β””β”€β”€β”€β”΄β”€β”€β”€β”€β”΄β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”˜
481+
482+
`
483+
if !visualCheck(t, "UnicodeTableRendering", buf.String(), expected) {
484+
t.Log(table.Debug())
485+
}
486+
})
487+
488+
t.Run("NoTrim", func(t *testing.T) {
489+
buf.Reset()
490+
table := tablewriter.NewTable(&buf,
491+
tablewriter.WithTrimSpace(tw.On),
492+
tablewriter.WithHeaderControl(tablewriter.Control{Hide: tw.Off}),
493+
)
494+
table.Header(data[0])
495+
table.Bulk(data[1:])
496+
table.Render()
497+
498+
expected := `
499+
β”Œβ”€β”€β”€β”€β”¬β”€β”€β”€β”€β”€β”¬β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”
500+
β”‚ NO β”‚ AGE β”‚ CITY β”‚
501+
β”œβ”€β”€β”€β”€β”Όβ”€β”€β”€β”€β”€β”Όβ”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€
502+
β”‚ 1 β”‚ 25 β”‚ New York β”‚
503+
β”‚ 2 β”‚ 30 β”‚ x β”‚
504+
β”‚ 3 β”‚ 28 β”‚ Lagos β”‚
505+
β””β”€β”€β”€β”€β”΄β”€β”€β”€β”€β”€β”΄β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”˜
506+
507+
508+
`
509+
visualCheck(t, "UnicodeTableRendering", buf.String(), expected)
510+
})
511+
512+
}

0 commit comments

Comments
Β (0)