| 1 |
package tablewriter |
| 2 |
|
| 3 |
import ( |
| 4 |
"github.com/mattn/go-runewidth" |
| 5 |
"github.com/olekukonko/tablewriter/pkg/twwidth" |
| 6 |
"github.com/olekukonko/tablewriter/tw" |
| 7 |
) |
| 8 |
|
| 9 |
// WithBorders configures the table's border settings by updating the renderer's border configuration. |
| 10 |
// This function is deprecated and will be removed in a future version. |
| 11 |
// |
| 12 |
// Deprecated: Use [WithRendition] to configure border settings for renderers that support |
| 13 |
// [tw.Renditioning], or update the renderer's [tw.RenderConfig] directly via its Config() method. |
| 14 |
// This function has no effect if no renderer is set on the table. |
| 15 |
// |
| 16 |
// Example migration: |
| 17 |
// |
| 18 |
// // Old (deprecated) |
| 19 |
// table.Options(WithBorders(tw.Border{Top: true, Bottom: true})) |
| 20 |
// // New (recommended) |
| 21 |
// table.Options(WithRendition(tw.Rendition{Borders: tw.Border{Top: true, Bottom: true}})) |
| 22 |
// |
| 23 |
// Parameters: |
| 24 |
// - borders: The [tw.Border] configuration to apply to the renderer's borders. |
| 25 |
// |
| 26 |
// Returns: |
| 27 |
// |
| 28 |
// An [Option] that updates the renderer's border settings if a renderer is set. |
| 29 |
// Logs a debug message if debugging is enabled and a renderer is present. |
| 30 |
func WithBorders(borders tw.Border) Option { |
| 31 |
return func(target *Table) { |
| 32 |
if target.renderer != nil { |
| 33 |
cfg := target.renderer.Config() |
| 34 |
cfg.Borders = borders |
| 35 |
if target.logger != nil { |
| 36 |
target.logger.Debugf("Option: WithBorders applied to Table: %+v", borders) |
| 37 |
} |
| 38 |
} |
| 39 |
} |
| 40 |
} |
| 41 |
|
| 42 |
// Behavior is an alias for [tw.Behavior] to configure table behavior settings. |
| 43 |
// This type is deprecated and will be removed in a future version. |
| 44 |
// |
| 45 |
// Deprecated: Use [tw.Behavior] directly to configure settings such as auto-hiding empty |
| 46 |
// columns, trimming spaces, or controlling header/footer visibility. |
| 47 |
// |
| 48 |
// Example migration: |
| 49 |
// |
| 50 |
// // Old (deprecated) |
| 51 |
// var b tablewriter.Behavior = tablewriter.Behavior{AutoHide: tw.On} |
| 52 |
// // New (recommended) |
| 53 |
// var b tw.Behavior = tw.Behavior{AutoHide: tw.On} |
| 54 |
type Behavior tw.Behavior |
| 55 |
|
| 56 |
// Settings is an alias for [tw.Settings] to configure renderer settings. |
| 57 |
// This type is deprecated and will be removed in a future version. |
| 58 |
// |
| 59 |
// Deprecated: Use [tw.Settings] directly to configure renderer settings, such as |
| 60 |
// separators and line styles. |
| 61 |
// |
| 62 |
// Example migration: |
| 63 |
// |
| 64 |
// // Old (deprecated) |
| 65 |
// var s tablewriter.Settings = tablewriter.Settings{Separator: "|"} |
| 66 |
// // New (recommended) |
| 67 |
// var s tw.Settings = tw.Settings{Separator: "|"} |
| 68 |
type Settings tw.Settings |
| 69 |
|
| 70 |
// WithRendererSettings updates the renderer's settings, such as separators and line styles. |
| 71 |
// This function is deprecated and will be removed in a future version. |
| 72 |
// |
| 73 |
// Deprecated: Use [WithRendition] to update renderer settings for renderers that implement |
| 74 |
// [tw.Renditioning], or configure the renderer's [tw.Settings] directly via its |
| 75 |
// [tw.Renderer.Config] method. This function has no effect if no renderer is set. |
| 76 |
// |
| 77 |
// Example migration: |
| 78 |
// |
| 79 |
// // Old (deprecated) |
| 80 |
// table.Options(WithRendererSettings(tw.Settings{Separator: "|"})) |
| 81 |
// // New (recommended) |
| 82 |
// table.Options(WithRendition(tw.Rendition{Settings: tw.Settings{Separator: "|"}})) |
| 83 |
// |
| 84 |
// Parameters: |
| 85 |
// - settings: The [tw.Settings] configuration to apply to the renderer. |
| 86 |
// |
| 87 |
// Returns: |
| 88 |
// |
| 89 |
// An [Option] that updates the renderer's settings if a renderer is set. |
| 90 |
// Logs a debug message if debugging is enabled and a renderer is present. |
| 91 |
func WithRendererSettings(settings tw.Settings) Option { |
| 92 |
return func(target *Table) { |
| 93 |
if target.renderer != nil { |
| 94 |
cfg := target.renderer.Config() |
| 95 |
cfg.Settings = settings |
| 96 |
if target.logger != nil { |
| 97 |
target.logger.Debugf("Option: WithRendererSettings applied to Table: %+v", settings) |
| 98 |
} |
| 99 |
} |
| 100 |
} |
| 101 |
} |
| 102 |
|
| 103 |
// WithAlignment sets the text alignment for footer cells within the formatting configuration. |
| 104 |
// This method is deprecated and will be removed in the next version. |
| 105 |
// |
| 106 |
// Deprecated: Use [FooterConfigBuilder.Alignment] with [AlignmentConfigBuilder.WithGlobal] |
| 107 |
// or [AlignmentConfigBuilder.WithPerColumn] to configure footer alignments. |
| 108 |
// Alternatively, apply a complete [tw.CellAlignment] configuration using |
| 109 |
// [WithFooterAlignmentConfig]. |
| 110 |
// |
| 111 |
// Example migration: |
| 112 |
// |
| 113 |
// // Old (deprecated) |
| 114 |
// builder.Footer().Formatting().WithAlignment(tw.AlignRight) |
| 115 |
// // New (recommended) |
| 116 |
// builder.Footer().Alignment().WithGlobal(tw.AlignRight) |
| 117 |
// // Or |
| 118 |
// table.Options(WithFooterAlignmentConfig(tw.CellAlignment{Global: tw.AlignRight})) |
| 119 |
// |
| 120 |
// Parameters: |
| 121 |
// - align: The [tw.Align] value to set for footer cells. Valid values are |
| 122 |
// [tw.AlignLeft], [tw.AlignRight], [tw.AlignCenter], and [tw.AlignNone]. |
| 123 |
// Invalid alignments are ignored. |
| 124 |
// |
| 125 |
// Returns: |
| 126 |
// |
| 127 |
// The [FooterFormattingBuilder] instance for method chaining. |
| 128 |
func (ff *FooterFormattingBuilder) WithAlignment(align tw.Align) *FooterFormattingBuilder { |
| 129 |
if align != tw.AlignLeft && align != tw.AlignRight && align != tw.AlignCenter && align != tw.AlignNone { |
| 130 |
return ff |
| 131 |
} |
| 132 |
ff.config.Alignment = align |
| 133 |
return ff |
| 134 |
} |
| 135 |
|
| 136 |
// WithAlignment sets the text alignment for header cells within the formatting configuration. |
| 137 |
// This method is deprecated and will be removed in the next version. |
| 138 |
// |
| 139 |
// Deprecated: Use [HeaderConfigBuilder.Alignment] with [AlignmentConfigBuilder.WithGlobal] |
| 140 |
// or [AlignmentConfigBuilder.WithPerColumn] to configure header alignments. |
| 141 |
// Alternatively, apply a complete [tw.CellAlignment] configuration using |
| 142 |
// [WithHeaderAlignmentConfig]. |
| 143 |
// |
| 144 |
// Example migration: |
| 145 |
// |
| 146 |
// // Old (deprecated) |
| 147 |
// builder.Header().Formatting().WithAlignment(tw.AlignCenter) |
| 148 |
// // New (recommended) |
| 149 |
// builder.Header().Alignment().WithGlobal(tw.AlignCenter) |
| 150 |
// // Or |
| 151 |
// table.Options(WithHeaderAlignmentConfig(tw.CellAlignment{Global: tw.AlignCenter})) |
| 152 |
// |
| 153 |
// Parameters: |
| 154 |
// - align: The [tw.Align] value to set for header cells. Valid values are |
| 155 |
// [tw.AlignLeft], [tw.AlignRight], [tw.AlignCenter], and [tw.AlignNone]. |
| 156 |
// Invalid alignments are ignored. |
| 157 |
// |
| 158 |
// Returns: |
| 159 |
// |
| 160 |
// The [HeaderFormattingBuilder] instance for method chaining. |
| 161 |
func (hf *HeaderFormattingBuilder) WithAlignment(align tw.Align) *HeaderFormattingBuilder { |
| 162 |
if align != tw.AlignLeft && align != tw.AlignRight && align != tw.AlignCenter && align != tw.AlignNone { |
| 163 |
return hf |
| 164 |
} |
| 165 |
hf.config.Alignment = align |
| 166 |
return hf |
| 167 |
} |
| 168 |
|
| 169 |
// WithAlignment sets the text alignment for row cells within the formatting configuration. |
| 170 |
// This method is deprecated and will be removed in the next version. |
| 171 |
// |
| 172 |
// Deprecated: Use [RowConfigBuilder.Alignment] with [AlignmentConfigBuilder.WithGlobal] |
| 173 |
// or [AlignmentConfigBuilder.WithPerColumn] to configure row alignments. |
| 174 |
// Alternatively, apply a complete [tw.CellAlignment] configuration using |
| 175 |
// [WithRowAlignmentConfig]. |
| 176 |
// |
| 177 |
// Example migration: |
| 178 |
// |
| 179 |
// // Old (deprecated) |
| 180 |
// builder.Row().Formatting().WithAlignment(tw.AlignLeft) |
| 181 |
// // New (recommended) |
| 182 |
// builder.Row().Alignment().WithGlobal(tw.AlignLeft) |
| 183 |
// // Or |
| 184 |
// table.Options(WithRowAlignmentConfig(tw.CellAlignment{Global: tw.AlignLeft})) |
| 185 |
// |
| 186 |
// Parameters: |
| 187 |
// - align: The [tw.Align] value to set for row cells. Valid values are |
| 188 |
// [tw.AlignLeft], [tw.AlignRight], [tw.AlignCenter], and [tw.AlignNone]. |
| 189 |
// Invalid alignments are ignored. |
| 190 |
// |
| 191 |
// Returns: |
| 192 |
// |
| 193 |
// The [RowFormattingBuilder] instance for method chaining. |
| 194 |
func (rf *RowFormattingBuilder) WithAlignment(align tw.Align) *RowFormattingBuilder { |
| 195 |
if align != tw.AlignLeft && align != tw.AlignRight && align != tw.AlignCenter && align != tw.AlignNone { |
| 196 |
return rf |
| 197 |
} |
| 198 |
rf.config.Alignment = align |
| 199 |
return rf |
| 200 |
} |
| 201 |
|
| 202 |
// WithTableMax sets the maximum width of the entire table in characters. |
| 203 |
// Negative values are ignored, and the change is logged if debugging is enabled. |
| 204 |
// The width constrains the table's rendering, potentially causing text wrapping or truncation |
| 205 |
// based on the configuration's wrapping settings (e.g., tw.WrapTruncate). |
| 206 |
// If debug logging is enabled via WithDebug(true), the applied width is logged. |
| 207 |
// |
| 208 |
// Deprecated: Use WithMaxWidth instead, which provides the same functionality with a clearer name |
| 209 |
// and consistent naming across the package. For example: |
| 210 |
// |
| 211 |
// tablewriter.NewTable(os.Stdout, tablewriter.WithMaxWidth(80)) |
| 212 |
func WithTableMax(width int) Option { |
| 213 |
return func(target *Table) { |
| 214 |
if width < 0 { |
| 215 |
return |
| 216 |
} |
| 217 |
target.config.MaxWidth = width |
| 218 |
if target.logger != nil { |
| 219 |
target.logger.Debugf("Option: WithTableMax applied to Table: %v", width) |
| 220 |
} |
| 221 |
} |
| 222 |
} |
| 223 |
|
| 224 |
// Deprecated: use WithEastAsian instead. |
| 225 |
// WithCondition provides a way to set a custom global runewidth.Condition |
| 226 |
// that will be used for all subsequent display width calculations by the twwidth (twdw) package. |
| 227 |
// |
| 228 |
// The runewidth.Condition object allows for more fine-grained control over how rune widths |
| 229 |
// are determined, beyond just toggling EastAsianWidth. This could include settings for |
| 230 |
// ambiguous width characters or other future properties of runewidth.Condition. |
| 231 |
func WithCondition(cond *runewidth.Condition) Option { |
| 232 |
return func(target *Table) { |
| 233 |
twwidth.SetCondition(cond) |
| 234 |
} |
| 235 |
} |