| 1 |
package renderer |
| 2 |
|
| 3 |
import ( |
| 4 |
"io" |
| 5 |
"strings" |
| 6 |
|
| 7 |
"github.com/olekukonko/ll" |
| 8 |
"github.com/olekukonko/tablewriter/pkg/twwidth" |
| 9 |
|
| 10 |
"github.com/olekukonko/tablewriter/tw" |
| 11 |
) |
| 12 |
|
| 13 |
// Blueprint implements a primary table rendering engine with customizable borders and alignments. |
| 14 |
type Blueprint struct { |
| 15 |
config tw.Rendition // Rendering configuration for table borders and symbols |
| 16 |
logger *ll.Logger // Logger for debug trace messages |
| 17 |
w io.Writer |
| 18 |
} |
| 19 |
|
| 20 |
// NewBlueprint creates a new Blueprint instance with optional custom configurations. |
| 21 |
func NewBlueprint(configs ...tw.Rendition) *Blueprint { |
| 22 |
// Initialize with default configuration |
| 23 |
cfg := defaultBlueprint() |
| 24 |
if len(configs) > 0 { |
| 25 |
userCfg := configs[0] |
| 26 |
// Override default borders if provided |
| 27 |
if userCfg.Borders.Left != 0 { |
| 28 |
cfg.Borders.Left = userCfg.Borders.Left |
| 29 |
} |
| 30 |
if userCfg.Borders.Right != 0 { |
| 31 |
cfg.Borders.Right = userCfg.Borders.Right |
| 32 |
} |
| 33 |
if userCfg.Borders.Top != 0 { |
| 34 |
cfg.Borders.Top = userCfg.Borders.Top |
| 35 |
} |
| 36 |
if userCfg.Borders.Bottom != 0 { |
| 37 |
cfg.Borders.Bottom = userCfg.Borders.Bottom |
| 38 |
} |
| 39 |
// Override symbols if provided |
| 40 |
if userCfg.Symbols != nil { |
| 41 |
cfg.Symbols = userCfg.Symbols |
| 42 |
} |
| 43 |
|
| 44 |
// Merge user settings with default settings |
| 45 |
cfg.Settings = mergeSettings(cfg.Settings, userCfg.Settings) |
| 46 |
} |
| 47 |
return &Blueprint{config: cfg, logger: ll.New("blueprint").Disable()} |
| 48 |
} |
| 49 |
|
| 50 |
// Close performs cleanup (no-op in this implementation). |
| 51 |
func (f *Blueprint) Close() error { |
| 52 |
f.logger.Debug("Blueprint.Close() called (no-op).") |
| 53 |
return nil |
| 54 |
} |
| 55 |
|
| 56 |
// Config returns the renderer's current configuration. |
| 57 |
func (f *Blueprint) Config() tw.Rendition { |
| 58 |
return f.config |
| 59 |
} |
| 60 |
|
| 61 |
// Footer renders the table footer section with configured formatting. |
| 62 |
func (f *Blueprint) Footer(footers [][]string, ctx tw.Formatting) { |
| 63 |
f.logger.Debugf("Starting Footer render: IsSubRow=%v, Location=%v, Pos=%s", ctx.IsSubRow, ctx.Row.Location, ctx.Row.Position) |
| 64 |
// Render the footer line |
| 65 |
f.renderLine(ctx) |
| 66 |
f.logger.Debug("Completed Footer render") |
| 67 |
} |
| 68 |
|
| 69 |
// Header renders the table header section with configured formatting. |
| 70 |
func (f *Blueprint) Header(headers [][]string, ctx tw.Formatting) { |
| 71 |
f.logger.Debugf("Starting Header render: IsSubRow=%v, Location=%v, Pos=%s, lines=%d, widths=%v", |
| 72 |
ctx.IsSubRow, ctx.Row.Location, ctx.Row.Position, len(ctx.Row.Current), ctx.Row.Widths) |
| 73 |
// Render the header line |
| 74 |
f.renderLine(ctx) |
| 75 |
f.logger.Debug("Completed Header render") |
| 76 |
} |
| 77 |
|
| 78 |
// Line renders a full horizontal row line with junctions and segments. |
| 79 |
func (f *Blueprint) Line(ctx tw.Formatting) { |
| 80 |
// Initialize junction renderer |
| 81 |
jr := NewJunction(JunctionContext{ |
| 82 |
Symbols: f.config.Symbols, |
| 83 |
Ctx: ctx, |
| 84 |
ColIdx: 0, |
| 85 |
Logger: f.logger, |
| 86 |
BorderTint: Tint{}, |
| 87 |
SeparatorTint: Tint{}, |
| 88 |
}) |
| 89 |
|
| 90 |
var line strings.Builder |
| 91 |
totalLineWidth := 0 // Track total display width |
| 92 |
// Get sorted column indices |
| 93 |
sortedKeys := ctx.Row.Widths.SortedKeys() |
| 94 |
numCols := 0 |
| 95 |
if len(sortedKeys) > 0 { |
| 96 |
numCols = sortedKeys[len(sortedKeys)-1] + 1 |
| 97 |
} |
| 98 |
|
| 99 |
// Handle empty row case |
| 100 |
if numCols == 0 { |
| 101 |
prefix := tw.Empty |
| 102 |
suffix := tw.Empty |
| 103 |
if f.config.Borders.Left.Enabled() { |
| 104 |
prefix = jr.RenderLeft() |
| 105 |
} |
| 106 |
if f.config.Borders.Right.Enabled() { |
| 107 |
suffix = jr.RenderRight(-1) |
| 108 |
} |
| 109 |
if prefix != tw.Empty || suffix != tw.Empty { |
| 110 |
line.WriteString(prefix + suffix + tw.NewLine) |
| 111 |
totalLineWidth = twwidth.Width(prefix) + twwidth.Width(suffix) |
| 112 |
f.w.Write([]byte(line.String())) |
| 113 |
} |
| 114 |
f.logger.Debugf("Line: Handled empty row/widths case (total width %d)", totalLineWidth) |
| 115 |
return |
| 116 |
} |
| 117 |
|
| 118 |
// Calculate target total width based on data rows |
| 119 |
targetTotalWidth := 0 |
| 120 |
for _, colIdx := range sortedKeys { |
| 121 |
targetTotalWidth += ctx.Row.Widths.Get(colIdx) |
| 122 |
} |
| 123 |
if f.config.Borders.Left.Enabled() { |
| 124 |
targetTotalWidth += twwidth.Width(f.config.Symbols.Column()) |
| 125 |
} |
| 126 |
if f.config.Borders.Right.Enabled() { |
| 127 |
targetTotalWidth += twwidth.Width(f.config.Symbols.Column()) |
| 128 |
} |
| 129 |
if f.config.Settings.Separators.BetweenColumns.Enabled() && len(sortedKeys) > 1 { |
| 130 |
targetTotalWidth += twwidth.Width(f.config.Symbols.Column()) * (len(sortedKeys) - 1) |
| 131 |
} |
| 132 |
|
| 133 |
// Add left border if enabled |
| 134 |
leftBorderWidth := 0 |
| 135 |
if f.config.Borders.Left.Enabled() { |
| 136 |
leftBorder := jr.RenderLeft() |
| 137 |
line.WriteString(leftBorder) |
| 138 |
leftBorderWidth = twwidth.Width(leftBorder) |
| 139 |
totalLineWidth += leftBorderWidth |
| 140 |
f.logger.Debugf("Line: Left border='%s' (f.width %d)", leftBorder, leftBorderWidth) |
| 141 |
} |
| 142 |
|
| 143 |
visibleColIndices := make([]int, 0) |
| 144 |
// Calculate visible columns |
| 145 |
for _, colIdx := range sortedKeys { |
| 146 |
colWidth := ctx.Row.Widths.Get(colIdx) |
| 147 |
if colWidth > 0 { |
| 148 |
visibleColIndices = append(visibleColIndices, colIdx) |
| 149 |
} |
| 150 |
} |
| 151 |
|
| 152 |
f.logger.Debugf("Line: sortedKeys=%v, Widths=%v, visibleColIndices=%v, targetTotalWidth=%d", sortedKeys, ctx.Row.Widths, visibleColIndices, targetTotalWidth) |
| 153 |
// Render each column segment |
| 154 |
for keyIndex, currentColIdx := range visibleColIndices { |
| 155 |
jr.colIdx = currentColIdx |
| 156 |
segment := jr.GetSegment() |
| 157 |
colWidth := ctx.Row.Widths.Get(currentColIdx) |
| 158 |
// Adjust colWidth to account for wider borders |
| 159 |
adjustedColWidth := colWidth |
| 160 |
if f.config.Borders.Left.Enabled() && keyIndex == 0 { |
| 161 |
adjustedColWidth -= leftBorderWidth - twwidth.Width(f.config.Symbols.Column()) |
| 162 |
} |
| 163 |
if f.config.Borders.Right.Enabled() && keyIndex == len(visibleColIndices)-1 { |
| 164 |
rightBorderWidth := twwidth.Width(jr.RenderRight(currentColIdx)) |
| 165 |
adjustedColWidth -= rightBorderWidth - twwidth.Width(f.config.Symbols.Column()) |
| 166 |
} |
| 167 |
if adjustedColWidth < 0 { |
| 168 |
adjustedColWidth = 0 |
| 169 |
} |
| 170 |
f.logger.Debugf("Line: colIdx=%d, segment='%s', adjusted colWidth=%d", currentColIdx, segment, adjustedColWidth) |
| 171 |
if segment == tw.Empty { |
| 172 |
spaces := strings.Repeat(tw.Space, adjustedColWidth) |
| 173 |
line.WriteString(spaces) |
| 174 |
totalLineWidth += adjustedColWidth |
| 175 |
f.logger.Debugf("Line: Rendered spaces='%s' (f.width %d) for col %d", spaces, adjustedColWidth, currentColIdx) |
| 176 |
} else { |
| 177 |
segmentWidth := twwidth.Width(segment) |
| 178 |
if segmentWidth == 0 { |
| 179 |
segmentWidth = 1 // Avoid division by zero |
| 180 |
f.logger.Warnf("Line: Segment='%s' has zero width, using 1", segment) |
| 181 |
} |
| 182 |
// Calculate how many full segments fit |
| 183 |
repeat := adjustedColWidth / segmentWidth |
| 184 |
if repeat < 1 && adjustedColWidth > 0 { |
| 185 |
repeat = 1 |
| 186 |
} |
| 187 |
repeatedSegment := strings.Repeat(segment, repeat) |
| 188 |
actualWidth := twwidth.Width(repeatedSegment) |
| 189 |
if actualWidth > adjustedColWidth { |
| 190 |
// Truncate if too long |
| 191 |
repeatedSegment = twwidth.Truncate(repeatedSegment, adjustedColWidth) |
| 192 |
actualWidth = twwidth.Width(repeatedSegment) |
| 193 |
f.logger.Debugf("Line: Truncated segment='%s' to width %d", repeatedSegment, actualWidth) |
| 194 |
} else if actualWidth < adjustedColWidth { |
| 195 |
// Pad with segment character to match adjustedColWidth |
| 196 |
remainingWidth := adjustedColWidth - actualWidth |
| 197 |
for i := 0; i < remainingWidth/segmentWidth; i++ { |
| 198 |
repeatedSegment += segment |
| 199 |
} |
| 200 |
actualWidth = twwidth.Width(repeatedSegment) |
| 201 |
if actualWidth < adjustedColWidth { |
| 202 |
repeatedSegment = tw.PadRight(repeatedSegment, tw.Space, adjustedColWidth) |
| 203 |
actualWidth = adjustedColWidth |
| 204 |
f.logger.Debugf("Line: Padded segment with spaces='%s' to width %d", repeatedSegment, actualWidth) |
| 205 |
} |
| 206 |
f.logger.Debugf("Line: Padded segment='%s' to width %d", repeatedSegment, actualWidth) |
| 207 |
} |
| 208 |
line.WriteString(repeatedSegment) |
| 209 |
totalLineWidth += actualWidth |
| 210 |
f.logger.Debugf("Line: Rendered segment='%s' (f.width %d) for col %d", repeatedSegment, actualWidth, currentColIdx) |
| 211 |
} |
| 212 |
|
| 213 |
// Add junction between columns if not the last column |
| 214 |
isLast := keyIndex == len(visibleColIndices)-1 |
| 215 |
if !isLast && f.config.Settings.Separators.BetweenColumns.Enabled() { |
| 216 |
nextColIdx := visibleColIndices[keyIndex+1] |
| 217 |
junction := jr.RenderJunction(currentColIdx, nextColIdx) |
| 218 |
// Use center symbol (❀) or column separator (|) to match data rows |
| 219 |
if twwidth.Width(junction) != twwidth.Width(f.config.Symbols.Column()) { |
| 220 |
junction = f.config.Symbols.Center() |
| 221 |
if twwidth.Width(junction) != twwidth.Width(f.config.Symbols.Column()) { |
| 222 |
junction = f.config.Symbols.Column() |
| 223 |
} |
| 224 |
} |
| 225 |
junctionWidth := twwidth.Width(junction) |
| 226 |
line.WriteString(junction) |
| 227 |
totalLineWidth += junctionWidth |
| 228 |
f.logger.Debugf("Line: Junction between %d and %d: '%s' (f.width %d)", currentColIdx, nextColIdx, junction, junctionWidth) |
| 229 |
} |
| 230 |
} |
| 231 |
|
| 232 |
// Add right border |
| 233 |
rightBorderWidth := 0 |
| 234 |
if f.config.Borders.Right.Enabled() && len(visibleColIndices) > 0 { |
| 235 |
lastIdx := visibleColIndices[len(visibleColIndices)-1] |
| 236 |
rightBorder := jr.RenderRight(lastIdx) |
| 237 |
rightBorderWidth = twwidth.Width(rightBorder) |
| 238 |
line.WriteString(rightBorder) |
| 239 |
totalLineWidth += rightBorderWidth |
| 240 |
f.logger.Debugf("Line: Right border='%s' (f.width %d)", rightBorder, rightBorderWidth) |
| 241 |
} |
| 242 |
|
| 243 |
// Write the final line |
| 244 |
line.WriteString(tw.NewLine) |
| 245 |
f.w.Write([]byte(line.String())) |
| 246 |
f.logger.Debugf("Line rendered: '%s' (total width %d, target %d)", strings.TrimSuffix(line.String(), tw.NewLine), totalLineWidth, targetTotalWidth) |
| 247 |
} |
| 248 |
|
| 249 |
// Logger sets the logger for the Blueprint instance. |
| 250 |
func (f *Blueprint) Logger(logger *ll.Logger) { |
| 251 |
f.logger = logger.Namespace("blueprint") |
| 252 |
} |
| 253 |
|
| 254 |
// Row renders a table data row with configured formatting. |
| 255 |
func (f *Blueprint) Row(row []string, ctx tw.Formatting) { |
| 256 |
f.logger.Debugf("Starting Row render: IsSubRow=%v, Location=%v, Pos=%s, hasFooter=%v", |
| 257 |
ctx.IsSubRow, ctx.Row.Location, ctx.Row.Position, ctx.HasFooter) |
| 258 |
|
| 259 |
// Render the row line |
| 260 |
f.renderLine(ctx) |
| 261 |
f.logger.Debug("Completed Row render") |
| 262 |
} |
| 263 |
|
| 264 |
// Start initializes the rendering process (no-op in this implementation). |
| 265 |
func (f *Blueprint) Start(w io.Writer) error { |
| 266 |
f.w = w |
| 267 |
f.logger.Debug("Blueprint.Start() called (no-op).") |
| 268 |
return nil |
| 269 |
} |
| 270 |
|
| 271 |
// formatCell formats a cell's content with specified width, padding, and alignment, returning an empty string if width is non-positive. |
| 272 |
func (f *Blueprint) formatCell(content string, width int, padding tw.Padding, align tw.Align) string { |
| 273 |
if width <= 0 { |
| 274 |
return tw.Empty |
| 275 |
} |
| 276 |
|
| 277 |
f.logger.Debugf("Formatting cell: content='%s', width=%d, align=%s, padding={L:'%s' R:'%s'}", |
| 278 |
content, width, align, padding.Left, padding.Right) |
| 279 |
|
| 280 |
// Calculate display width of content |
| 281 |
runeWidth := twwidth.Width(content) |
| 282 |
|
| 283 |
// Set default padding characters |
| 284 |
leftPadChar := padding.Left |
| 285 |
rightPadChar := padding.Right |
| 286 |
|
| 287 |
// if f.config.Settings.Cushion.Enabled() || f.config.Settings.Cushion.Default() { |
| 288 |
// if leftPadChar == tw.Empty { |
| 289 |
// leftPadChar = tw.Space |
| 290 |
// } |
| 291 |
// if rightPadChar == tw.Empty { |
| 292 |
// rightPadChar = tw.Space |
| 293 |
// } |
| 294 |
//} |
| 295 |
|
| 296 |
// Calculate padding widths |
| 297 |
padLeftWidth := twwidth.Width(leftPadChar) |
| 298 |
padRightWidth := twwidth.Width(rightPadChar) |
| 299 |
|
| 300 |
// Calculate available width for content |
| 301 |
availableContentWidth := max(width-padLeftWidth-padRightWidth, 0) |
| 302 |
f.logger.Debugf("Available content width: %d", availableContentWidth) |
| 303 |
|
| 304 |
// Truncate content if it exceeds available width |
| 305 |
if runeWidth > availableContentWidth { |
| 306 |
content = twwidth.Truncate(content, availableContentWidth) |
| 307 |
runeWidth = twwidth.Width(content) |
| 308 |
f.logger.Debugf("Truncated content to fit %d: '%s' (new width %d)", availableContentWidth, content, runeWidth) |
| 309 |
} |
| 310 |
|
| 311 |
// Calculate total padding needed |
| 312 |
totalPaddingWidth := max(width-runeWidth, 0) |
| 313 |
f.logger.Debugf("Total padding width: %d", totalPaddingWidth) |
| 314 |
|
| 315 |
var result strings.Builder |
| 316 |
var leftPaddingWidth, rightPaddingWidth int |
| 317 |
|
| 318 |
// Apply alignment and padding |
| 319 |
switch align { |
| 320 |
case tw.AlignLeft: |
| 321 |
result.WriteString(leftPadChar) |
| 322 |
result.WriteString(content) |
| 323 |
rightPaddingWidth = totalPaddingWidth - padLeftWidth |
| 324 |
if rightPaddingWidth > 0 { |
| 325 |
padChar := rightPadChar |
| 326 |
if padChar == tw.Empty { |
| 327 |
padChar = tw.Space |
| 328 |
} |
| 329 |
result.WriteString(tw.PadRight(tw.Empty, padChar, rightPaddingWidth)) |
| 330 |
f.logger.Debugf("Applied right padding: '%s' for %d width", padChar, rightPaddingWidth) |
| 331 |
} |
| 332 |
case tw.AlignRight: |
| 333 |
leftPaddingWidth = totalPaddingWidth - padRightWidth |
| 334 |
if leftPaddingWidth > 0 { |
| 335 |
padChar := leftPadChar |
| 336 |
if padChar == tw.Empty { |
| 337 |
padChar = tw.Space |
| 338 |
} |
| 339 |
result.WriteString(tw.PadLeft(tw.Empty, padChar, leftPaddingWidth)) |
| 340 |
f.logger.Debugf("Applied left padding: '%s' for %d width", padChar, leftPaddingWidth) |
| 341 |
} |
| 342 |
result.WriteString(content) |
| 343 |
result.WriteString(rightPadChar) |
| 344 |
case tw.AlignCenter: |
| 345 |
leftPaddingWidth = (totalPaddingWidth-padLeftWidth-padRightWidth)/2 + padLeftWidth |
| 346 |
rightPaddingWidth = totalPaddingWidth - leftPaddingWidth |
| 347 |
if leftPaddingWidth > padLeftWidth { |
| 348 |
padChar := leftPadChar |
| 349 |
if padChar == tw.Empty { |
| 350 |
padChar = tw.Space |
| 351 |
} |
| 352 |
result.WriteString(tw.PadLeft(tw.Empty, padChar, leftPaddingWidth-padLeftWidth)) |
| 353 |
f.logger.Debugf("Applied left centering padding: '%s' for %d width", padChar, leftPaddingWidth-padLeftWidth) |
| 354 |
} |
| 355 |
result.WriteString(leftPadChar) |
| 356 |
result.WriteString(content) |
| 357 |
result.WriteString(rightPadChar) |
| 358 |
if rightPaddingWidth > padRightWidth { |
| 359 |
padChar := rightPadChar |
| 360 |
if padChar == tw.Empty { |
| 361 |
padChar = tw.Space |
| 362 |
} |
| 363 |
result.WriteString(tw.PadRight(tw.Empty, padChar, rightPaddingWidth-padRightWidth)) |
| 364 |
f.logger.Debugf("Applied right centering padding: '%s' for %d width", padChar, rightPaddingWidth-padRightWidth) |
| 365 |
} |
| 366 |
default: |
| 367 |
// Default to left alignment |
| 368 |
result.WriteString(leftPadChar) |
| 369 |
result.WriteString(content) |
| 370 |
rightPaddingWidth = totalPaddingWidth - padLeftWidth |
| 371 |
if rightPaddingWidth > 0 { |
| 372 |
padChar := rightPadChar |
| 373 |
if padChar == tw.Empty { |
| 374 |
padChar = tw.Space |
| 375 |
} |
| 376 |
result.WriteString(tw.PadRight(tw.Empty, padChar, rightPaddingWidth)) |
| 377 |
f.logger.Debugf("Applied right padding: '%s' for %d width", padChar, rightPaddingWidth) |
| 378 |
} |
| 379 |
} |
| 380 |
|
| 381 |
output := result.String() |
| 382 |
finalWidth := twwidth.Width(output) |
| 383 |
// Adjust output to match target width |
| 384 |
if finalWidth > width { |
| 385 |
output = twwidth.Truncate(output, width) |
| 386 |
f.logger.Debugf("formatCell: Truncated output to width %d", width) |
| 387 |
} else if finalWidth < width { |
| 388 |
output = tw.PadRight(output, tw.Space, width) |
| 389 |
f.logger.Debugf("formatCell: Padded output to meet width %d", width) |
| 390 |
} |
| 391 |
|
| 392 |
// Log warning if final width doesn't match target |
| 393 |
if f.logger.Enabled() && twwidth.Width(output) != width { |
| 394 |
f.logger.Debugf("formatCell Warning: Final width %d does not match target %d for result '%s'", |
| 395 |
twwidth.Width(output), width, output) |
| 396 |
} |
| 397 |
|
| 398 |
f.logger.Debugf("Formatted cell final result: '%s' (target width %d)", output, width) |
| 399 |
return output |
| 400 |
} |
| 401 |
|
| 402 |
// renderLine renders a single line (header, row, or footer) with borders, separators, and merge handling. |
| 403 |
func (f *Blueprint) renderLine(ctx tw.Formatting) { |
| 404 |
// Get sorted column indices |
| 405 |
sortedKeys := ctx.Row.Widths.SortedKeys() |
| 406 |
numCols := 0 |
| 407 |
if len(sortedKeys) > 0 { |
| 408 |
numCols = sortedKeys[len(sortedKeys)-1] + 1 |
| 409 |
} |
| 410 |
|
| 411 |
// Set column separator and borders |
| 412 |
columnSeparator := f.config.Symbols.Column() |
| 413 |
prefix := tw.Empty |
| 414 |
if f.config.Borders.Left.Enabled() { |
| 415 |
prefix = columnSeparator |
| 416 |
} |
| 417 |
suffix := tw.Empty |
| 418 |
if f.config.Borders.Right.Enabled() { |
| 419 |
suffix = columnSeparator |
| 420 |
} |
| 421 |
|
| 422 |
var output strings.Builder |
| 423 |
totalLineWidth := 0 // Track total display width |
| 424 |
if prefix != tw.Empty { |
| 425 |
output.WriteString(prefix) |
| 426 |
totalLineWidth += twwidth.Width(prefix) |
| 427 |
f.logger.Debugf("renderLine: Prefix='%s' (f.width %d)", prefix, twwidth.Width(prefix)) |
| 428 |
} |
| 429 |
|
| 430 |
colIndex := 0 |
| 431 |
separatorDisplayWidth := 0 |
| 432 |
if f.config.Settings.Separators.BetweenColumns.Enabled() { |
| 433 |
separatorDisplayWidth = twwidth.Width(columnSeparator) |
| 434 |
} |
| 435 |
|
| 436 |
// Process each column |
| 437 |
for colIndex < numCols { |
| 438 |
visualWidth := ctx.Row.Widths.Get(colIndex) |
| 439 |
cellCtx, ok := ctx.Row.Current[colIndex] |
| 440 |
isHMergeStart := ok && cellCtx.Merge.Horizontal.Present && cellCtx.Merge.Horizontal.Start |
| 441 |
if visualWidth == 0 && !isHMergeStart { |
| 442 |
f.logger.Debugf("renderLine: Skipping col %d (zero width, not HMerge start)", colIndex) |
| 443 |
colIndex++ |
| 444 |
continue |
| 445 |
} |
| 446 |
|
| 447 |
// Determine if a separator is needed |
| 448 |
shouldAddSeparator := false |
| 449 |
if colIndex > 0 && f.config.Settings.Separators.BetweenColumns.Enabled() { |
| 450 |
prevWidth := ctx.Row.Widths.Get(colIndex - 1) |
| 451 |
prevCellCtx, prevOk := ctx.Row.Current[colIndex-1] |
| 452 |
prevIsHMergeEnd := prevOk && prevCellCtx.Merge.Horizontal.Present && prevCellCtx.Merge.Horizontal.End |
| 453 |
if (prevWidth > 0 || prevIsHMergeEnd) && (!ok || (!cellCtx.Merge.Horizontal.Present || cellCtx.Merge.Horizontal.Start)) { |
| 454 |
shouldAddSeparator = true |
| 455 |
} |
| 456 |
} |
| 457 |
if shouldAddSeparator { |
| 458 |
output.WriteString(columnSeparator) |
| 459 |
totalLineWidth += separatorDisplayWidth |
| 460 |
f.logger.Debugf("renderLine: Added separator '%s' before col %d (f.width %d)", columnSeparator, colIndex, separatorDisplayWidth) |
| 461 |
} else if colIndex > 0 { |
| 462 |
f.logger.Debugf("renderLine: Skipped separator before col %d due to zero-width prev col or HMerge continuation", colIndex) |
| 463 |
} |
| 464 |
|
| 465 |
// Handle merged cells |
| 466 |
span := 1 |
| 467 |
if isHMergeStart { |
| 468 |
span = cellCtx.Merge.Horizontal.Span |
| 469 |
if ctx.Row.Position == tw.Row { |
| 470 |
dynamicTotalWidth := 0 |
| 471 |
for k := 0; k < span && colIndex+k < numCols; k++ { |
| 472 |
normWidth := max(ctx.NormalizedWidths.Get(colIndex+k), 0) |
| 473 |
dynamicTotalWidth += normWidth |
| 474 |
if k > 0 && separatorDisplayWidth > 0 && ctx.NormalizedWidths.Get(colIndex+k) > 0 { |
| 475 |
dynamicTotalWidth += separatorDisplayWidth |
| 476 |
} |
| 477 |
} |
| 478 |
visualWidth = dynamicTotalWidth |
| 479 |
f.logger.Debugf("renderLine: Row HMerge col %d, span %d, dynamic visualWidth %d", colIndex, span, visualWidth) |
| 480 |
} else { |
| 481 |
visualWidth = ctx.Row.Widths.Get(colIndex) |
| 482 |
f.logger.Debugf("renderLine: H/F HMerge col %d, span %d, pre-adjusted visualWidth %d", colIndex, span, visualWidth) |
| 483 |
} |
| 484 |
} else { |
| 485 |
visualWidth = ctx.Row.Widths.Get(colIndex) |
| 486 |
f.logger.Debugf("renderLine: Regular col %d, visualWidth %d", colIndex, visualWidth) |
| 487 |
} |
| 488 |
if visualWidth < 0 { |
| 489 |
visualWidth = 0 |
| 490 |
} |
| 491 |
|
| 492 |
// Skip processing for non-start merged cells |
| 493 |
if ok && cellCtx.Merge.Horizontal.Present && !cellCtx.Merge.Horizontal.Start { |
| 494 |
f.logger.Debugf("renderLine: Skipping col %d processing (part of HMerge)", colIndex) |
| 495 |
colIndex++ |
| 496 |
continue |
| 497 |
} |
| 498 |
|
| 499 |
// Handle empty cell context |
| 500 |
if !ok { |
| 501 |
if visualWidth > 0 { |
| 502 |
spaces := strings.Repeat(tw.Space, visualWidth) |
| 503 |
output.WriteString(spaces) |
| 504 |
totalLineWidth += visualWidth |
| 505 |
f.logger.Debugf("renderLine: No cell context for col %d, writing %d spaces (f.width %d)", colIndex, visualWidth, visualWidth) |
| 506 |
} else { |
| 507 |
f.logger.Debugf("renderLine: No cell context for col %d, visualWidth is 0, writing nothing", colIndex) |
| 508 |
} |
| 509 |
colIndex += span |
| 510 |
continue |
| 511 |
} |
| 512 |
|
| 513 |
// Set cell padding and alignment |
| 514 |
padding := cellCtx.Padding |
| 515 |
align := cellCtx.Align |
| 516 |
switch align { |
| 517 |
case tw.AlignNone: |
| 518 |
switch ctx.Row.Position { |
| 519 |
case tw.Header: |
| 520 |
align = tw.AlignCenter |
| 521 |
case tw.Footer: |
| 522 |
align = tw.AlignRight |
| 523 |
default: |
| 524 |
align = tw.AlignLeft |
| 525 |
} |
| 526 |
f.logger.Debugf("renderLine: col %d (data: '%s') using renderer default align '%s' for position %s.", colIndex, cellCtx.Data, align, ctx.Row.Position) |
| 527 |
case tw.Skip: |
| 528 |
switch ctx.Row.Position { |
| 529 |
case tw.Header: |
| 530 |
align = tw.AlignCenter |
| 531 |
case tw.Footer: |
| 532 |
align = tw.AlignRight |
| 533 |
default: |
| 534 |
align = tw.AlignLeft |
| 535 |
} |
| 536 |
f.logger.Debugf("renderLine: col %d (data: '%s') cellCtx.Align was Skip/empty, falling back to basic default '%s'.", colIndex, cellCtx.Data, align) |
| 537 |
} |
| 538 |
|
| 539 |
isTotalPattern := false |
| 540 |
|
| 541 |
// Case-insensitive check for "total" |
| 542 |
if isHMergeStart && colIndex > 0 { |
| 543 |
if prevCellCtx, ok := ctx.Row.Current[colIndex-1]; ok { |
| 544 |
if strings.Contains(strings.ToLower(prevCellCtx.Data), "total") { |
| 545 |
isTotalPattern = true |
| 546 |
f.logger.Debugf("renderLine: total pattern in row in %d", colIndex) |
| 547 |
} |
| 548 |
} |
| 549 |
} |
| 550 |
|
| 551 |
// Get the alignment from the configuration |
| 552 |
align = cellCtx.Align |
| 553 |
|
| 554 |
// Override alignment for footer merged cells |
| 555 |
if (ctx.Row.Position == tw.Footer && isHMergeStart) || isTotalPattern { |
| 556 |
if align == tw.AlignNone { |
| 557 |
f.logger.Debugf("renderLine: Applying AlignRight HMerge/TOTAL override for Footer col %d. Original/default align was: %s", colIndex, align) |
| 558 |
align = tw.AlignRight |
| 559 |
} |
| 560 |
} |
| 561 |
|
| 562 |
// Handle vertical/hierarchical merges |
| 563 |
cellData := cellCtx.Data |
| 564 |
if (cellCtx.Merge.Vertical.Present && !cellCtx.Merge.Vertical.Start) || |
| 565 |
(cellCtx.Merge.Hierarchical.Present && !cellCtx.Merge.Hierarchical.Start) { |
| 566 |
cellData = tw.Empty |
| 567 |
f.logger.Debugf("renderLine: Blanked data for col %d (non-start V/Hierarchical)", colIndex) |
| 568 |
} |
| 569 |
|
| 570 |
// Format and render the cell |
| 571 |
formattedCell := f.formatCell(cellData, visualWidth, padding, align) |
| 572 |
if len(formattedCell) > 0 { |
| 573 |
output.WriteString(formattedCell) |
| 574 |
cellWidth := twwidth.Width(formattedCell) |
| 575 |
totalLineWidth += cellWidth |
| 576 |
f.logger.Debugf("renderLine: Rendered col %d, formattedCell='%s' (f.width %d), totalLineWidth=%d", colIndex, formattedCell, cellWidth, totalLineWidth) |
| 577 |
} |
| 578 |
|
| 579 |
// Log rendering details |
| 580 |
if isHMergeStart { |
| 581 |
f.logger.Debugf("renderLine: Rendered HMerge START col %d (span %d, visualWidth %d, align %v): '%s'", |
| 582 |
colIndex, span, visualWidth, align, formattedCell) |
| 583 |
} else { |
| 584 |
f.logger.Debugf("renderLine: Rendered regular col %d (visualWidth %d, align %v): '%s'", |
| 585 |
colIndex, visualWidth, align, formattedCell) |
| 586 |
} |
| 587 |
colIndex += span |
| 588 |
} |
| 589 |
|
| 590 |
// Add suffix and adjust total width |
| 591 |
if output.Len() > len(prefix) || f.config.Borders.Right.Enabled() { |
| 592 |
output.WriteString(suffix) |
| 593 |
totalLineWidth += twwidth.Width(suffix) |
| 594 |
f.logger.Debugf("renderLine: Suffix='%s' (f.width %d)", suffix, twwidth.Width(suffix)) |
| 595 |
} |
| 596 |
output.WriteString(tw.NewLine) |
| 597 |
f.w.Write([]byte(output.String())) |
| 598 |
f.logger.Debugf("renderLine: Final rendered line: '%s' (total width %d)", strings.TrimSuffix(output.String(), tw.NewLine), totalLineWidth) |
| 599 |
} |
| 600 |
|
| 601 |
// Rendition updates the Blueprint's configuration. |
| 602 |
func (f *Blueprint) Rendition(config tw.Rendition) { |
| 603 |
f.config = mergeRendition(f.config, config) |
| 604 |
f.logger.Debugf("Blueprint.Rendition updated. New config: %+v", f.config) |
| 605 |
} |
| 606 |
|
| 607 |
// Ensure Blueprint implements tw.Renditioning |
| 608 |
var _ tw.Renditioning = (*Blueprint)(nil) |