Debian Codegraph @

option.go

raw
1 package tablewriter
2
3 import (
4 "reflect"
5
6 "github.com/olekukonko/ll"
7 "github.com/olekukonko/tablewriter/pkg/twcache"
8 "github.com/olekukonko/tablewriter/pkg/twwidth"
9 "github.com/olekukonko/tablewriter/tw"
10 )
11
12 // Option defines a function type for configuring a Table instance.
13 type Option func(target *Table)
14
15 // WithAutoHide enables or disables automatic hiding of columns with empty data rows.
16 // Logs the change if debugging is enabled.
17 func WithAutoHide(state tw.State) Option {
18 return func(target *Table) {
19 target.config.Behavior.AutoHide = state
20 if target.logger != nil {
21 target.logger.Debugf("Option: WithAutoHide applied to Table: %v", state)
22 }
23 }
24 }
25
26 // WithColumnMax sets a global maximum column width for the table in streaming mode.
27 // Negative values are ignored, and the change is logged if debugging is enabled.
28 func WithColumnMax(width int) Option {
29 return func(target *Table) {
30 if width < 0 {
31 return
32 }
33 target.config.Widths.Global = width
34 if target.logger != nil {
35 target.logger.Debugf("Option: WithColumnMax applied to Table: %v", width)
36 }
37 }
38 }
39
40 // WithMaxWidth sets a global maximum table width for the table.
41 // Negative values are ignored, and the change is logged if debugging is enabled.
42 func WithMaxWidth(width int) Option {
43 return func(target *Table) {
44 if width < 0 {
45 return
46 }
47 target.config.MaxWidth = width
48 if target.logger != nil {
49 target.logger.Debugf("Option: WithTableMax applied to Table: %v", width)
50 }
51 }
52 }
53
54 // WithWidths sets per-column widths for the table.
55 // Negative widths are removed, and the change is logged if debugging is enabled.
56 func WithWidths(width tw.CellWidth) Option {
57 return func(target *Table) {
58 target.config.Widths = width
59 if target.logger != nil {
60 target.logger.Debugf("Option: WithColumnWidths applied to Table: %v", width)
61 }
62 }
63 }
64
65 // WithColumnWidths sets per-column widths for the table.
66 // Negative widths are removed, and the change is logged if debugging is enabled.
67 func WithColumnWidths(widths tw.Mapper[int, int]) Option {
68 return func(target *Table) {
69 for k, v := range widths {
70 if v < 0 {
71 delete(widths, k)
72 }
73 }
74 target.config.Widths.PerColumn = widths
75 if target.logger != nil {
76 target.logger.Debugf("Option: WithColumnWidths applied to Table: %v", widths)
77 }
78 }
79 }
80
81 // WithConfig applies a custom configuration to the table by merging it with the default configuration.
82 func WithConfig(cfg Config) Option {
83 return func(target *Table) {
84 target.config = mergeConfig(defaultConfig(), cfg)
85 }
86 }
87
88 // WithDebug enables or disables debug logging and adjusts the logger level accordingly.
89 // Logs the change if debugging is enabled.
90 func WithDebug(debug bool) Option {
91 return func(target *Table) {
92 target.config.Debug = debug
93 }
94 }
95
96 // WithFooter sets the table footers by calling the Footer method.
97 func WithFooter(footers []string) Option {
98 return func(target *Table) {
99 target.Footer(footers)
100 }
101 }
102
103 // WithFooterConfig applies a full footer configuration to the table.
104 // Logs the change if debugging is enabled.
105 func WithFooterConfig(config tw.CellConfig) Option {
106 return func(target *Table) {
107 target.config.Footer = config
108 if target.logger != nil {
109 target.logger.Debug("Option: WithFooterConfig applied to Table.")
110 }
111 }
112 }
113
114 // WithFooterAlignmentConfig applies a footer alignment configuration to the table.
115 // Logs the change if debugging is enabled.
116 func WithFooterAlignmentConfig(alignment tw.CellAlignment) Option {
117 return func(target *Table) {
118 target.config.Footer.Alignment = alignment
119 if target.logger != nil {
120 target.logger.Debugf("Option: WithFooterAlignmentConfig applied to Table: %+v", alignment)
121 }
122 }
123 }
124
125 // Deprecated: Use a ConfigBuilder with .Footer().CellMerging().WithMode(...) instead.
126 // This option will be removed in a future version.
127 func WithFooterMergeMode(mergeMode int) Option {
128 return func(target *Table) {
129 if mergeMode < tw.MergeNone || mergeMode > tw.MergeHierarchical {
130 return
131 }
132 target.config.Footer.Merging.Mode = mergeMode
133 target.config.Footer.Formatting.MergeMode = mergeMode
134 if target.logger != nil {
135 target.logger.Debugf("Option: WithFooterMergeMode applied to Table: %v", mergeMode)
136 }
137 }
138 }
139
140 // WithFooterAutoWrap sets the wrapping behavior for footer cells.
141 // Invalid wrap modes are ignored, and the change is logged if debugging is enabled.
142 func WithFooterAutoWrap(wrap int) Option {
143 return func(target *Table) {
144 if wrap < tw.WrapNone || wrap > tw.WrapBreak {
145 return
146 }
147 target.config.Footer.Formatting.AutoWrap = wrap
148 if target.logger != nil {
149 target.logger.Debugf("Option: WithFooterAutoWrap applied to Table: %v", wrap)
150 }
151 }
152 }
153
154 // WithFooterFilter sets the filter configuration for footer cells.
155 // Logs the change if debugging is enabled.
156 func WithFooterFilter(filter tw.CellFilter) Option {
157 return func(target *Table) {
158 target.config.Footer.Filter = filter
159 if target.logger != nil {
160 target.logger.Debug("Option: WithFooterFilter applied to Table.")
161 }
162 }
163 }
164
165 // WithFooterCallbacks sets the callback configuration for footer cells.
166 // Logs the change if debugging is enabled.
167 func WithFooterCallbacks(callbacks tw.CellCallbacks) Option {
168 return func(target *Table) {
169 target.config.Footer.Callbacks = callbacks
170 if target.logger != nil {
171 target.logger.Debug("Option: WithFooterCallbacks applied to Table.")
172 }
173 }
174 }
175
176 // WithFooterPaddingPerColumn sets per-column padding for footer cells.
177 // Logs the change if debugging is enabled.
178 func WithFooterPaddingPerColumn(padding []tw.Padding) Option {
179 return func(target *Table) {
180 target.config.Footer.Padding.PerColumn = padding
181 if target.logger != nil {
182 target.logger.Debugf("Option: WithFooterPaddingPerColumn applied to Table: %+v", padding)
183 }
184 }
185 }
186
187 // WithFooterMaxWidth sets the maximum content width for footer cells.
188 // Negative values are ignored, and the change is logged if debugging is enabled.
189 func WithFooterMaxWidth(maxWidth int) Option {
190 return func(target *Table) {
191 if maxWidth < 0 {
192 return
193 }
194 target.config.Footer.ColMaxWidths.Global = maxWidth
195 if target.logger != nil {
196 target.logger.Debugf("Option: WithFooterMaxWidth applied to Table: %v", maxWidth)
197 }
198 }
199 }
200
201 // WithHeader sets the table headers by calling the Header method.
202 func WithHeader(headers []string) Option {
203 return func(target *Table) {
204 target.Header(headers)
205 }
206 }
207
208 // WithHeaderAlignment sets the text alignment for header cells.
209 // Invalid alignments are ignored, and the change is logged if debugging is enabled.
210 func WithHeaderAlignment(align tw.Align) Option {
211 return func(target *Table) {
212 if align != tw.AlignLeft && align != tw.AlignRight && align != tw.AlignCenter && align != tw.AlignNone {
213 return
214 }
215 target.config.Header.Alignment.Global = align
216 if target.logger != nil {
217 target.logger.Debugf("Option: WithHeaderAlignment applied to Table: %v", align)
218 }
219 }
220 }
221
222 // WithHeaderAutoWrap sets the wrapping behavior for header cells.
223 // Invalid wrap modes are ignored, and the change is logged if debugging is enabled.
224 func WithHeaderAutoWrap(wrap int) Option {
225 return func(target *Table) {
226 if wrap < tw.WrapNone || wrap > tw.WrapBreak {
227 return
228 }
229 target.config.Header.Formatting.AutoWrap = wrap
230 if target.logger != nil {
231 target.logger.Debugf("Option: WithHeaderAutoWrap applied to Table: %v", wrap)
232 }
233 }
234 }
235
236 // Deprecated: Use a ConfigBuilder with .Header().CellMerging().WithMode(...) instead.
237 // This option will be removed in a future version.
238 func WithHeaderMergeMode(mergeMode int) Option {
239 return func(target *Table) {
240 if mergeMode < tw.MergeNone || mergeMode > tw.MergeHierarchical {
241 return
242 }
243 target.config.Header.Merging.Mode = mergeMode
244 target.config.Header.Formatting.MergeMode = mergeMode
245 if target.logger != nil {
246 target.logger.Debugf("Option: WithHeaderMergeMode applied to Table: %v", mergeMode)
247 }
248 }
249 }
250
251 // WithHeaderFilter sets the filter configuration for header cells.
252 // Logs the change if debugging is enabled.
253 func WithHeaderFilter(filter tw.CellFilter) Option {
254 return func(target *Table) {
255 target.config.Header.Filter = filter
256 if target.logger != nil {
257 target.logger.Debug("Option: WithHeaderFilter applied to Table.")
258 }
259 }
260 }
261
262 // WithHeaderCallbacks sets the callback configuration for header cells.
263 // Logs the change if debugging is enabled.
264 func WithHeaderCallbacks(callbacks tw.CellCallbacks) Option {
265 return func(target *Table) {
266 target.config.Header.Callbacks = callbacks
267 if target.logger != nil {
268 target.logger.Debug("Option: WithHeaderCallbacks applied to Table.")
269 }
270 }
271 }
272
273 // WithHeaderPaddingPerColumn sets per-column padding for header cells.
274 // Logs the change if debugging is enabled.
275 func WithHeaderPaddingPerColumn(padding []tw.Padding) Option {
276 return func(target *Table) {
277 target.config.Header.Padding.PerColumn = padding
278 if target.logger != nil {
279 target.logger.Debugf("Option: WithHeaderPaddingPerColumn applied to Table: %+v", padding)
280 }
281 }
282 }
283
284 // WithHeaderMaxWidth sets the maximum content width for header cells.
285 // Negative values are ignored, and the change is logged if debugging is enabled.
286 func WithHeaderMaxWidth(maxWidth int) Option {
287 return func(target *Table) {
288 if maxWidth < 0 {
289 return
290 }
291 target.config.Header.ColMaxWidths.Global = maxWidth
292 if target.logger != nil {
293 target.logger.Debugf("Option: WithHeaderMaxWidth applied to Table: %v", maxWidth)
294 }
295 }
296 }
297
298 // WithRowAlignment sets the text alignment for row cells.
299 // Invalid alignments are ignored, and the change is logged if debugging is enabled.
300 func WithRowAlignment(align tw.Align) Option {
301 return func(target *Table) {
302 if err := align.Validate(); err != nil {
303 return
304 }
305 target.config.Row.Alignment.Global = align
306 if target.logger != nil {
307 target.logger.Debugf("Option: WithRowAlignment applied to Table: %v", align)
308 }
309 }
310 }
311
312 // WithRowAutoWrap sets the wrapping behavior for row cells.
313 // Invalid wrap modes are ignored, and the change is logged if debugging is enabled.
314 func WithRowAutoWrap(wrap int) Option {
315 return func(target *Table) {
316 if wrap < tw.WrapNone || wrap > tw.WrapBreak {
317 return
318 }
319 target.config.Row.Formatting.AutoWrap = wrap
320 if target.logger != nil {
321 target.logger.Debugf("Option: WithRowAutoWrap applied to Table: %v", wrap)
322 }
323 }
324 }
325
326 // Deprecated: Use a ConfigBuilder with .Row().CellMerging().WithMode(...) instead.
327 // This option will be removed in a future version.
328 func WithRowMergeMode(mergeMode int) Option {
329 return func(target *Table) {
330 if mergeMode < tw.MergeNone || mergeMode > tw.MergeHierarchical {
331 return
332 }
333 target.config.Row.Merging.Mode = mergeMode
334 target.config.Row.Formatting.MergeMode = mergeMode
335 if target.logger != nil {
336 target.logger.Debugf("Option: WithRowMergeMode applied to Table: %v", mergeMode)
337 }
338 }
339 }
340
341 // WithRowFilter sets the filter configuration for row cells.
342 // Logs the change if debugging is enabled.
343 func WithRowFilter(filter tw.CellFilter) Option {
344 return func(target *Table) {
345 target.config.Row.Filter = filter
346 if target.logger != nil {
347 target.logger.Debug("Option: WithRowFilter applied to Table.")
348 }
349 }
350 }
351
352 // WithRowCallbacks sets the callback configuration for row cells.
353 // Logs the change if debugging is enabled.
354 func WithRowCallbacks(callbacks tw.CellCallbacks) Option {
355 return func(target *Table) {
356 target.config.Row.Callbacks = callbacks
357 if target.logger != nil {
358 target.logger.Debug("Option: WithRowCallbacks applied to Table.")
359 }
360 }
361 }
362
363 // WithRowPaddingPerColumn sets per-column padding for row cells.
364 // Logs the change if debugging is enabled.
365 func WithRowPaddingPerColumn(padding []tw.Padding) Option {
366 return func(target *Table) {
367 target.config.Row.Padding.PerColumn = padding
368 if target.logger != nil {
369 target.logger.Debugf("Option: WithRowPaddingPerColumn applied to Table: %+v", padding)
370 }
371 }
372 }
373
374 // WithHeaderAlignmentConfig applies a header alignment configuration to the table.
375 // Logs the change if debugging is enabled.
376 func WithHeaderAlignmentConfig(alignment tw.CellAlignment) Option {
377 return func(target *Table) {
378 target.config.Header.Alignment = alignment
379 if target.logger != nil {
380 target.logger.Debugf("Option: WithHeaderAlignmentConfig applied to Table: %+v", alignment)
381 }
382 }
383 }
384
385 // WithHeaderConfig applies a full header configuration to the table.
386 // Logs the change if debugging is enabled.
387 func WithHeaderConfig(config tw.CellConfig) Option {
388 return func(target *Table) {
389 target.config.Header = config
390 if target.logger != nil {
391 target.logger.Debug("Option: WithHeaderConfig applied to Table.")
392 }
393 }
394 }
395
396 // WithLogger sets a custom logger for the table and updates the renderer if present.
397 // Logs the change if debugging is enabled.
398 func WithLogger(logger *ll.Logger) Option {
399 return func(target *Table) {
400 target.logger = logger
401 if target.logger != nil {
402 target.logger.Debug("Option: WithLogger applied to Table.")
403 if target.renderer != nil {
404 target.renderer.Logger(target.logger)
405 }
406 }
407 }
408 }
409
410 // WithRenderer sets a custom renderer for the table and attaches the logger if present.
411 // Logs the change if debugging is enabled.
412 func WithRenderer(f tw.Renderer) Option {
413 return func(target *Table) {
414 target.renderer = f
415 if target.logger != nil {
416 target.logger.Debugf("Option: WithRenderer applied to Table: %T", f)
417 f.Logger(target.logger)
418 }
419 }
420 }
421
422 // WithRowConfig applies a full row configuration to the table.
423 // Logs the change if debugging is enabled.
424 func WithRowConfig(config tw.CellConfig) Option {
425 return func(target *Table) {
426 target.config.Row = config
427 if target.logger != nil {
428 target.logger.Debug("Option: WithRowConfig applied to Table.")
429 }
430 }
431 }
432
433 // WithRowAlignmentConfig applies a row alignment configuration to the table.
434 // Logs the change if debugging is enabled.
435 func WithRowAlignmentConfig(alignment tw.CellAlignment) Option {
436 return func(target *Table) {
437 target.config.Row.Alignment = alignment
438 if target.logger != nil {
439 target.logger.Debugf("Option: WithRowAlignmentConfig applied to Table: %+v", alignment)
440 }
441 }
442 }
443
444 // WithRowMaxWidth sets the maximum content width for row cells.
445 // Negative values are ignored, and the change is logged if debugging is enabled.
446 func WithRowMaxWidth(maxWidth int) Option {
447 return func(target *Table) {
448 if maxWidth < 0 {
449 return
450 }
451 target.config.Row.ColMaxWidths.Global = maxWidth
452 if target.logger != nil {
453 target.logger.Debugf("Option: WithRowMaxWidth applied to Table: %v", maxWidth)
454 }
455 }
456 }
457
458 // WithStreaming applies a streaming configuration to the table by merging it with the existing configuration.
459 // Logs the change if debugging is enabled.
460 func WithStreaming(c tw.StreamConfig) Option {
461 return func(target *Table) {
462 target.config.Stream = mergeStreamConfig(target.config.Stream, c)
463 if target.logger != nil {
464 target.logger.Debug("Option: WithStreaming applied to Table.")
465 }
466 }
467 }
468
469 // WithStringer sets a custom stringer function for converting row data and clears the stringer cache.
470 // Logs the change if debugging is enabled.
471 func WithStringer(stringer interface{}) Option {
472 return func(t *Table) {
473 t.stringer = stringer
474 t.stringerCache = twcache.NewLRU[reflect.Type, reflect.Value](tw.DefaultCacheStringCapacity)
475 if t.logger != nil {
476 t.logger.Debug("Stringer updated, cache cleared")
477 }
478 }
479 }
480
481 // WithStringerCache enables the default LRU caching for the stringer function.
482 // It initializes the cache with a default capacity if one does not already exist.
483 func WithStringerCache() Option {
484 return func(t *Table) {
485 // Initialize default cache if strictly necessary (nil),
486 // or if you want to ensure the default implementation is used.
487 if t.stringerCache == nil {
488 // NewLRU returns (Instance, error). We ignore the error here assuming capacity > 0.
489 cache := twcache.NewLRU[reflect.Type, reflect.Value](tw.DefaultCacheStringCapacity)
490 t.stringerCache = cache
491 }
492
493 if t.logger != nil {
494 t.logger.Debug("Option: WithStringerCache enabled (Default LRU)")
495 }
496 }
497 }
498
499 // WithStringerCacheCustom enables caching for the stringer function using a specific implementation.
500 // Passing nil disables caching entirely.
501 func WithStringerCacheCustom(cache twcache.Cache[reflect.Type, reflect.Value]) Option {
502 return func(t *Table) {
503 if cache == nil {
504 t.stringerCache = nil
505 if t.logger != nil {
506 t.logger.Debug("Option: WithStringerCacheCustom called with nil (Caching Disabled)")
507 }
508 return
509 }
510
511 // Set the custom cache and enable the flag
512 t.stringerCache = cache
513
514 if t.logger != nil {
515 t.logger.Debug("Option: WithStringerCacheCustom enabled")
516 }
517 }
518 }
519
520 // WithTrimSpace sets whether leading and trailing spaces are automatically trimmed.
521 // Logs the change if debugging is enabled.
522 func WithTrimSpace(state tw.State) Option {
523 return func(target *Table) {
524 target.config.Behavior.TrimSpace = state
525 if target.logger != nil {
526 target.logger.Debugf("Option: WithTrimSpace applied to Table: %v", state)
527 }
528 }
529 }
530
531 // WithTrimTab sets whether leading and trailing tab characters are automatically trimmed.
532 // Logs the change if debugging is enabled.
533 func WithTrimTab(state tw.State) Option {
534 return func(target *Table) {
535 target.config.Behavior.TrimTab = state
536 if target.logger != nil {
537 target.logger.Debugf("Option: WithTrimTab applied to Table: %v", state)
538 }
539 }
540 }
541
542 // WithTrimLine sets whether empty visual lines within a cell are trimmed.
543 // Logs the change if debugging is enabled.
544 func WithTrimLine(state tw.State) Option {
545 return func(target *Table) {
546 target.config.Behavior.TrimLine = state
547 if target.logger != nil {
548 target.logger.Debugf("Option: WithTrimLine applied to Table: %v", state)
549 }
550 }
551 }
552
553 // WithHeaderAutoFormat enables or disables automatic formatting for header cells.
554 // Logs the change if debugging is enabled.
555 func WithHeaderAutoFormat(state tw.State) Option {
556 return func(target *Table) {
557 target.config.Header.Formatting.AutoFormat = state
558 if target.logger != nil {
559 target.logger.Debugf("Option: WithHeaderAutoFormat applied to Table: %v", state)
560 }
561 }
562 }
563
564 // WithFooterAutoFormat enables or disables automatic formatting for footer cells.
565 // Logs the change if debugging is enabled.
566 func WithFooterAutoFormat(state tw.State) Option {
567 return func(target *Table) {
568 target.config.Footer.Formatting.AutoFormat = state
569 if target.logger != nil {
570 target.logger.Debugf("Option: WithFooterAutoFormat applied to Table: %v", state)
571 }
572 }
573 }
574
575 // WithRowAutoFormat enables or disables automatic formatting for row cells.
576 // Logs the change if debugging is enabled.
577 func WithRowAutoFormat(state tw.State) Option {
578 return func(target *Table) {
579 target.config.Row.Formatting.AutoFormat = state
580 if target.logger != nil {
581 target.logger.Debugf("Option: WithRowAutoFormat applied to Table: %v", state)
582 }
583 }
584 }
585
586 // WithHeaderControl sets the control behavior for the table header.
587 // Logs the change if debugging is enabled.
588 func WithHeaderControl(control tw.Control) Option {
589 return func(target *Table) {
590 target.config.Behavior.Header = control
591 if target.logger != nil {
592 target.logger.Debugf("Option: WithHeaderControl applied to Table: %v", control)
593 }
594 }
595 }
596
597 // WithFooterControl sets the control behavior for the table footer.
598 // Logs the change if debugging is enabled.
599 func WithFooterControl(control tw.Control) Option {
600 return func(target *Table) {
601 target.config.Behavior.Footer = control
602 if target.logger != nil {
603 target.logger.Debugf("Option: WithFooterControl applied to Table: %v", control)
604 }
605 }
606 }
607
608 // WithAlignment sets the default column alignment for the header, rows, and footer.
609 // Logs the change if debugging is enabled.
610 func WithAlignment(alignment tw.Alignment) Option {
611 return func(target *Table) {
612 target.config.Header.Alignment.PerColumn = alignment
613 target.config.Row.Alignment.PerColumn = alignment
614 target.config.Footer.Alignment.PerColumn = alignment
615 if target.logger != nil {
616 target.logger.Debugf("Option: WithAlignment applied to Table: %+v", alignment)
617 }
618 }
619 }
620
621 // WithBehavior applies a behavior configuration to the table.
622 // Logs the change if debugging is enabled.
623 func WithBehavior(behavior tw.Behavior) Option {
624 return func(target *Table) {
625 target.config.Behavior = behavior
626 if target.logger != nil {
627 target.logger.Debugf("Option: WithBehavior applied to Table: %+v", behavior)
628 }
629 }
630 }
631
632 // WithPadding sets the global padding for the header, rows, and footer.
633 // Logs the change if debugging is enabled.
634 func WithPadding(padding tw.Padding) Option {
635 return func(target *Table) {
636 target.config.Header.Padding.Global = padding
637 target.config.Row.Padding.Global = padding
638 target.config.Footer.Padding.Global = padding
639 if target.logger != nil {
640 target.logger.Debugf("Option: WithPadding applied to Table: %+v", padding)
641 }
642 }
643 }
644
645 // WithRendition allows updating the active renderer's rendition configuration
646 // by merging the provided rendition.
647 // If the renderer does not implement tw.Renditioning, a warning is logged.
648 // Logs the change if debugging is enabled.
649 func WithRendition(rendition tw.Rendition) Option {
650 return func(target *Table) {
651 if target.renderer == nil {
652 if target.logger != nil {
653 target.logger.Warn("Option: WithRendition: No renderer set on table.")
654 }
655 return
656 }
657 if ru, ok := target.renderer.(tw.Renditioning); ok {
658 ru.Rendition(rendition)
659 if target.logger != nil {
660 target.logger.Debugf("Option: WithRendition: Applied to renderer via Renditioning.SetRendition(): %+v", rendition)
661 }
662 } else if target.logger != nil {
663 target.logger.Warnf("Option: WithRendition: Current renderer type %T does not implement tw.Renditioning. Rendition may not be applied as expected.", target.renderer)
664 }
665 }
666 }
667
668 // WithEastAsian configures the global East Asian width calculation setting.
669 // - state=tw.On: Enables East Asian width calculations. CJK and ambiguous characters
670 // are typically measured as double width.
671 // - state=tw.Off: Disables East Asian width calculations. Characters are generally
672 // measured as single width, subject to Unicode standards.
673 //
674 // This setting affects all subsequent display width calculations using the twdw package.
675 func WithEastAsian(state tw.State) Option {
676 return func(target *Table) {
677 if state.Enabled() {
678 twwidth.SetEastAsian(true)
679 }
680 if state.Disabled() {
681 twwidth.SetEastAsian(false)
682 }
683 }
684 }
685
686 // WithSymbols sets the symbols used for drawing table borders and separators.
687 // The symbols are applied to the table's renderer configuration, if a renderer is set.
688 // If no renderer is set (target.renderer is nil), this option has no effect. .
689 func WithSymbols(symbols tw.Symbols) Option {
690 return func(target *Table) {
691 if target.renderer != nil {
692 cfg := target.renderer.Config()
693 cfg.Symbols = symbols
694
695 if ru, ok := target.renderer.(tw.Renditioning); ok {
696 ru.Rendition(cfg)
697 if target.logger != nil {
698 target.logger.Debugf("Option: WithRendition: Applied to renderer via Renditioning.SetRendition(): %+v", cfg)
699 }
700 } else if target.logger != nil {
701 target.logger.Warnf("Option: WithRendition: Current renderer type %T does not implement tw.Renditioning. Rendition may not be applied as expected.", target.renderer)
702 }
703 }
704 }
705 }
706
707 // WithCounters enables line counting by wrapping the table's writer.
708 // If a custom counter (that implements tw.Counter) is provided, it will be used.
709 // If the provided counter is nil, a default tw.LineCounter will be used.
710 // The final count can be retrieved via the table.Lines() method after Render() is called.
711 func WithCounters(counters ...tw.Counter) Option {
712 return func(target *Table) {
713 // Iterate through the provided counters and add any non-nil ones.
714 for _, c := range counters {
715 if c != nil {
716 target.counters = append(target.counters, c)
717 }
718 }
719 }
720 }
721
722 // WithLineCounter enables the default line counter.
723 // A new instance of tw.LineCounter is added to the table's list of counters.
724 // The total count can be retrieved via the table.Lines() method after Render() is called.
725 func WithLineCounter() Option {
726 return func(target *Table) {
727 // Important: Create a new instance so tables don't share counters.
728 target.counters = append(target.counters, &tw.LineCounter{})
729 }
730 }
731
732 // defaultConfig returns a default Config with sensible settings for headers, rows, footers, and behavior.
733 func defaultConfig() Config {
734 return Config{
735 MaxWidth: 0,
736 Header: tw.CellConfig{
737 Formatting: tw.CellFormatting{
738 AutoWrap: tw.WrapTruncate,
739 AutoFormat: tw.On,
740 MergeMode: tw.MergeNone,
741 },
742 Merging: tw.CellMerging{
743 Mode: tw.MergeNone,
744 },
745 Padding: tw.CellPadding{
746 Global: tw.PaddingDefault,
747 },
748 Alignment: tw.CellAlignment{
749 Global: tw.AlignCenter,
750 PerColumn: []tw.Align{},
751 },
752 },
753 Row: tw.CellConfig{
754 Formatting: tw.CellFormatting{
755 AutoWrap: tw.WrapNormal,
756 AutoFormat: tw.Off,
757 MergeMode: tw.MergeNone,
758 },
759 Merging: tw.CellMerging{
760 Mode: tw.MergeNone,
761 },
762 Padding: tw.CellPadding{
763 Global: tw.PaddingDefault,
764 },
765 Alignment: tw.CellAlignment{
766 Global: tw.AlignLeft,
767 PerColumn: []tw.Align{},
768 },
769 },
770 Footer: tw.CellConfig{
771 Formatting: tw.CellFormatting{
772 AutoWrap: tw.WrapNormal,
773 AutoFormat: tw.Off,
774 MergeMode: tw.MergeNone,
775 },
776 Merging: tw.CellMerging{
777 Mode: tw.MergeNone,
778 },
779 Padding: tw.CellPadding{
780 Global: tw.PaddingDefault,
781 },
782 Alignment: tw.CellAlignment{
783 Global: tw.AlignRight,
784 PerColumn: []tw.Align{},
785 },
786 },
787 Stream: tw.StreamConfig{
788 Enable: false,
789 StrictColumns: false,
790 },
791 Debug: false,
792 Behavior: tw.Behavior{
793 AutoHide: tw.Off,
794 TrimSpace: tw.On,
795 TrimTab: tw.On,
796 TrimLine: tw.On,
797 Structs: tw.Struct{
798 AutoHeader: tw.Off,
799 Tags: []string{"json", "db"},
800 },
801 },
802 }
803 }
804
805 // mergeCellConfig merges a source CellConfig into a destination CellConfig, prioritizing non-default source values.
806 // It handles deep merging for complex fields like padding and callbacks.
807 func mergeCellConfig(dst, src tw.CellConfig) tw.CellConfig {
808 if src.Formatting.Alignment != tw.Empty {
809 dst.Formatting.Alignment = src.Formatting.Alignment
810 }
811
812 if src.Formatting.AutoWrap != 0 {
813 dst.Formatting.AutoWrap = src.Formatting.AutoWrap
814 }
815 if src.ColMaxWidths.Global != 0 {
816 dst.ColMaxWidths.Global = src.ColMaxWidths.Global
817 }
818
819 // Handle merging of the new CellMerging struct and the deprecated MergeMode
820 if src.Merging.Mode != 0 {
821 dst.Merging.Mode = src.Merging.Mode
822 dst.Formatting.MergeMode = src.Merging.Mode
823 } else if src.Formatting.MergeMode != 0 {
824 dst.Merging.Mode = src.Formatting.MergeMode
825 dst.Formatting.MergeMode = src.Formatting.MergeMode
826 }
827
828 if src.Merging.ByColumnIndex != nil {
829 dst.Merging.ByColumnIndex = src.Merging.ByColumnIndex.Clone()
830 }
831
832 dst.Formatting.AutoFormat = src.Formatting.AutoFormat
833
834 if src.Padding.Global.Paddable() {
835 dst.Padding.Global = src.Padding.Global
836 }
837
838 if len(src.Padding.PerColumn) > 0 {
839 if dst.Padding.PerColumn == nil {
840 dst.Padding.PerColumn = make([]tw.Padding, len(src.Padding.PerColumn))
841 } else if len(src.Padding.PerColumn) > len(dst.Padding.PerColumn) {
842 dst.Padding.PerColumn = append(dst.Padding.PerColumn, make([]tw.Padding, len(src.Padding.PerColumn)-len(dst.Padding.PerColumn))...)
843 }
844 for i, pad := range src.Padding.PerColumn {
845 if pad.Paddable() {
846 dst.Padding.PerColumn[i] = pad
847 }
848 }
849 }
850 if src.Callbacks.Global != nil {
851 dst.Callbacks.Global = src.Callbacks.Global
852 }
853 if len(src.Callbacks.PerColumn) > 0 {
854 if dst.Callbacks.PerColumn == nil {
855 dst.Callbacks.PerColumn = make([]func(), len(src.Callbacks.PerColumn))
856 } else if len(src.Callbacks.PerColumn) > len(dst.Callbacks.PerColumn) {
857 dst.Callbacks.PerColumn = append(dst.Callbacks.PerColumn, make([]func(), len(src.Callbacks.PerColumn)-len(dst.Callbacks.PerColumn))...)
858 }
859 for i, cb := range src.Callbacks.PerColumn {
860 if cb != nil {
861 dst.Callbacks.PerColumn[i] = cb
862 }
863 }
864 }
865 if src.Filter.Global != nil {
866 dst.Filter.Global = src.Filter.Global
867 }
868 if len(src.Filter.PerColumn) > 0 {
869 if dst.Filter.PerColumn == nil {
870 dst.Filter.PerColumn = make([]func(string) string, len(src.Filter.PerColumn))
871 } else if len(src.Filter.PerColumn) > len(dst.Filter.PerColumn) {
872 dst.Filter.PerColumn = append(dst.Filter.PerColumn, make([]func(string) string, len(src.Filter.PerColumn)-len(dst.Filter.PerColumn))...)
873 }
874 for i, filter := range src.Filter.PerColumn {
875 if filter != nil {
876 dst.Filter.PerColumn[i] = filter
877 }
878 }
879 }
880
881 // Merge Alignment
882 if src.Alignment.Global != tw.Empty {
883 dst.Alignment.Global = src.Alignment.Global
884 }
885
886 if len(src.Alignment.PerColumn) > 0 {
887 if dst.Alignment.PerColumn == nil {
888 dst.Alignment.PerColumn = make([]tw.Align, len(src.Alignment.PerColumn))
889 } else if len(src.Alignment.PerColumn) > len(dst.Alignment.PerColumn) {
890 dst.Alignment.PerColumn = append(dst.Alignment.PerColumn, make([]tw.Align, len(src.Alignment.PerColumn)-len(dst.Alignment.PerColumn))...)
891 }
892 for i, align := range src.Alignment.PerColumn {
893 if align != tw.Skip {
894 dst.Alignment.PerColumn[i] = align
895 }
896 }
897 }
898
899 if len(src.ColumnAligns) > 0 {
900 if dst.ColumnAligns == nil {
901 dst.ColumnAligns = make([]tw.Align, len(src.ColumnAligns))
902 } else if len(src.ColumnAligns) > len(dst.ColumnAligns) {
903 dst.ColumnAligns = append(dst.ColumnAligns, make([]tw.Align, len(src.ColumnAligns)-len(dst.ColumnAligns))...)
904 }
905 for i, align := range src.ColumnAligns {
906 if align != tw.Skip {
907 dst.ColumnAligns[i] = align
908 }
909 }
910 }
911
912 if len(src.ColMaxWidths.PerColumn) > 0 {
913 if dst.ColMaxWidths.PerColumn == nil {
914 dst.ColMaxWidths.PerColumn = make(map[int]int)
915 }
916 for k, v := range src.ColMaxWidths.PerColumn {
917 if v != 0 {
918 dst.ColMaxWidths.PerColumn[k] = v
919 }
920 }
921 }
922 return dst
923 }
924
925 // mergeConfig merges a source Config into a destination Config, prioritizing non-default source values.
926 // It performs deep merging for complex types like Header, Row, Footer, and Stream.
927 func mergeConfig(dst, src Config) Config {
928 if src.MaxWidth != 0 {
929 dst.MaxWidth = src.MaxWidth
930 }
931
932 dst.Debug = src.Debug || dst.Debug
933 dst.Behavior.AutoHide = src.Behavior.AutoHide
934 dst.Behavior.TrimSpace = src.Behavior.TrimSpace
935 dst.Behavior.TrimTab = src.Behavior.TrimTab
936 dst.Behavior.Compact = src.Behavior.Compact
937 dst.Behavior.Header = src.Behavior.Header
938 dst.Behavior.Footer = src.Behavior.Footer
939 dst.Behavior.Footer = src.Behavior.Footer
940
941 dst.Behavior.Structs.AutoHeader = src.Behavior.Structs.AutoHeader
942
943 // check lent of tags
944 if len(src.Behavior.Structs.Tags) > 0 {
945 dst.Behavior.Structs.Tags = src.Behavior.Structs.Tags
946 }
947
948 if src.Widths.Global != 0 {
949 dst.Widths.Global = src.Widths.Global
950 }
951 if len(src.Widths.PerColumn) > 0 {
952 if dst.Widths.PerColumn == nil {
953 dst.Widths.PerColumn = make(map[int]int)
954 }
955 for k, v := range src.Widths.PerColumn {
956 if v != 0 {
957 dst.Widths.PerColumn[k] = v
958 }
959 }
960 }
961
962 dst.Header = mergeCellConfig(dst.Header, src.Header)
963 dst.Row = mergeCellConfig(dst.Row, src.Row)
964 dst.Footer = mergeCellConfig(dst.Footer, src.Footer)
965 dst.Stream = mergeStreamConfig(dst.Stream, src.Stream)
966
967 return dst
968 }
969
970 // mergeStreamConfig merges a source StreamConfig into a destination StreamConfig, prioritizing non-default source values.
971 func mergeStreamConfig(dst, src tw.StreamConfig) tw.StreamConfig {
972 if src.Enable {
973 dst.Enable = true
974 }
975
976 dst.StrictColumns = src.StrictColumns
977 return dst
978 }
979
980 // padLine pads a line to the specified column count by appending empty strings as needed.
981 func padLine(line []string, numCols int) []string {
982 if len(line) >= numCols {
983 return line
984 }
985 padded := make([]string, numCols)
986 copy(padded, line)
987 for i := len(line); i < numCols; i++ {
988 padded[i] = tw.Empty
989 }
990 return padded
991 }