@@ -154,38 +154,33 @@ func (m *Markdown) formatCell(content string, width int, align tw.Align, padding
154
154
155
155
// formatSeparator generates a Markdown separator (e.g., `---`, `:--`, `:-:`) with alignment indicators.
156
156
func (m * Markdown ) formatSeparator (width int , align tw.Align ) string {
157
- targetWidth := tw .Max (3 , width )
158
- leftColon := align == tw .AlignLeft || align == tw .AlignCenter
159
- rightColon := align == tw .AlignRight || align == tw .AlignCenter
160
-
161
- numDashes := targetWidth
162
- if leftColon {
163
- numDashes --
164
- }
165
- if rightColon {
166
- numDashes --
167
- }
168
- if numDashes < 1 {
169
- numDashes = 1
170
- }
171
-
157
+ targetWidth := tw .Max (3 , width ) // Minimum width of 3
172
158
var sb strings.Builder
173
- if leftColon {
159
+
160
+ switch align {
161
+ case tw .AlignLeft :
174
162
sb .WriteRune (':' )
175
- }
176
- sb . WriteString ( strings . Repeat ( "-" , numDashes ))
177
- if rightColon {
163
+ sb . WriteString ( strings . Repeat ( "-" , targetWidth - 1 ))
164
+ case tw . AlignRight :
165
+ sb . WriteString ( strings . Repeat ( "-" , targetWidth - 1 ))
178
166
sb .WriteRune (':' )
167
+ case tw .AlignCenter :
168
+ sb .WriteRune (':' )
169
+ sb .WriteString (strings .Repeat ("-" , targetWidth - 2 ))
170
+ sb .WriteRune (':' )
171
+ default : // Fallback to left alignment for unspecified
172
+ sb .WriteRune (':' )
173
+ sb .WriteString (strings .Repeat ("-" , targetWidth - 1 ))
179
174
}
180
175
181
- currentLen := sb .Len ()
176
+ result := sb .String ()
177
+ currentLen := tw .DisplayWidth (result )
182
178
if currentLen < targetWidth {
183
- sb . WriteString ( strings .Repeat ("-" , targetWidth - currentLen ) )
179
+ result += strings .Repeat ("-" , targetWidth - currentLen )
184
180
} else if currentLen > targetWidth {
185
- m . logger . Debug ( "Markdown formatSeparator: WARNING final length %d > target %d for '%s'" , currentLen , targetWidth , sb . String () )
181
+ result = tw . TruncateString ( result , targetWidth )
186
182
}
187
183
188
- result := sb .String ()
189
184
m .logger .Debug ("Markdown formatSeparator: width=%d, align=%s -> '%s'" , width , align , result )
190
185
return result
191
186
}
@@ -234,13 +229,12 @@ func (m *Markdown) renderMarkdownLine(w io.Writer, line []string, ctx tw.Formatt
234
229
separatorWidth := tw .DisplayWidth (separator )
235
230
236
231
for colIndex < numCols {
237
- // Fetch cell context
238
232
cellCtx , ok := ctx .Row .Current [colIndex ]
239
233
defaultPadding := tw.Padding {Left : " " , Right : " " }
240
234
if ! ok {
241
- defaultAlign := tw .AlignLeft
235
+ defaultAlign := tw .AlignLeft // Default for rows
242
236
if ctx .Row .Position == tw .Header && ! isHeaderSep {
243
- defaultAlign = tw .AlignCenter
237
+ defaultAlign = tw .AlignCenter // Default for headers
244
238
}
245
239
if ctx .Row .Position == tw .Footer {
246
240
defaultAlign = tw .AlignRight
@@ -258,8 +252,6 @@ func (m *Markdown) renderMarkdownLine(w io.Writer, line []string, ctx tw.Formatt
258
252
if colIndex > 0 && ! isContinuation {
259
253
output .WriteString (separator )
260
254
m .logger .Debug ("renderMarkdownLine: Added separator '%s' before col %d" , separator , colIndex )
261
- } else if colIndex > 0 {
262
- m .logger .Debug ("renderMarkdownLine: Skipped separator before col %d due to HMerge continuation" , colIndex )
263
255
}
264
256
265
257
// Calculate width and span
@@ -273,7 +265,7 @@ func (m *Markdown) renderMarkdownLine(w io.Writer, line []string, ctx tw.Formatt
273
265
} else {
274
266
align = tw .AlignLeft
275
267
}
276
- m .logger .Debug ("renderMarkdownLine: Col %d using renderer default align '%s'" , colIndex , align )
268
+ m .logger .Debug ("renderMarkdownLine: Col %d using default align '%s'" , colIndex , align )
277
269
}
278
270
279
271
visualWidth := 0
@@ -292,7 +284,7 @@ func (m *Markdown) renderMarkdownLine(w io.Writer, line []string, ctx tw.Formatt
292
284
}
293
285
}
294
286
visualWidth = totalWidth
295
- m .logger .Debug ("renderMarkdownLine: HMerge col %d, span %d, calculated visualWidth %d from normalized widths " , colIndex , span , visualWidth )
287
+ m .logger .Debug ("renderMarkdownLine: HMerge col %d, span %d, visualWidth %d" , colIndex , span , visualWidth )
296
288
} else {
297
289
visualWidth = ctx .Row .Widths .Get (colIndex )
298
290
m .logger .Debug ("renderMarkdownLine: Regular col %d, visualWidth %d" , colIndex , visualWidth )
@@ -303,16 +295,22 @@ func (m *Markdown) renderMarkdownLine(w io.Writer, line []string, ctx tw.Formatt
303
295
304
296
// Render segment
305
297
if isContinuation {
306
- m .logger .Debug ("renderMarkdownLine: Skipping col %d rendering (part of HMerge)" , colIndex )
298
+ m .logger .Debug ("renderMarkdownLine: Skipping col %d ( HMerge continuation )" , colIndex )
307
299
} else {
308
300
var formattedSegment string
309
301
if isHeaderSep {
310
- headerAlign := tw .AlignCenter
302
+ // Use the header's alignment from ctx.Row.Previous (header row)
303
+ headerAlign := tw .AlignCenter // Default for headers
311
304
if headerCellCtx , headerOK := ctx .Row .Previous [colIndex ]; headerOK {
312
305
headerAlign = headerCellCtx .Align
313
306
if headerAlign == tw .AlignNone || headerAlign == "" {
314
307
headerAlign = tw .AlignCenter
315
308
}
309
+ } else if cellCtx , ok := ctx .Row .Current [colIndex ]; ok {
310
+ headerAlign = cellCtx .Align
311
+ if headerAlign == tw .AlignNone || headerAlign == "" {
312
+ headerAlign = tw .AlignCenter
313
+ }
316
314
}
317
315
formattedSegment = m .formatSeparator (visualWidth , headerAlign )
318
316
} else {
@@ -323,7 +321,7 @@ func (m *Markdown) renderMarkdownLine(w io.Writer, line []string, ctx tw.Formatt
323
321
formattedSegment = m .formatCell (content , visualWidth , align , cellCtx .Padding )
324
322
}
325
323
output .WriteString (formattedSegment )
326
- m .logger .Debug ("renderMarkdownLine: Wrote segment for col %d (span %d, visualWidth %d): '%s'" , colIndex , span , visualWidth , formattedSegment )
324
+ m .logger .Debug ("renderMarkdownLine: Wrote col %d (span %d, width %d): '%s'" , colIndex , span , visualWidth , formattedSegment )
327
325
}
328
326
329
327
colIndex += span
@@ -332,7 +330,7 @@ func (m *Markdown) renderMarkdownLine(w io.Writer, line []string, ctx tw.Formatt
332
330
output .WriteString (suffix )
333
331
output .WriteString (tw .NewLine )
334
332
fmt .Fprint (w , output .String ())
335
- m .logger .Debug ("renderMarkdownLine: Final rendered line: %s" , strings .TrimSuffix (output .String (), tw .NewLine ))
333
+ m .logger .Debug ("renderMarkdownLine: Final line: %s" , strings .TrimSuffix (output .String (), tw .NewLine ))
336
334
}
337
335
338
336
// Header renders the Markdown table header and its separator line.
0 commit comments