fix: properly set table alignments

This commit is contained in:
Maas Lalani
2024-03-06 15:52:11 +01:00
committed by Christian Muehlhaeuser
parent ba919abf98
commit 9e3abf6f65
2 changed files with 20 additions and 2 deletions
+2 -1
View File
@@ -295,7 +295,8 @@ func (tr *ANSIRenderer) NewElement(node ast.Node, source []byte) Element {
// Tables
case astext.KindTable:
te := &TableElement{}
table := node.(*astext.Table)
te := &TableElement{table: table}
return Element{
Entering: "\n",
Renderer: te,
+18 -1
View File
@@ -5,6 +5,7 @@ import (
"github.com/muesli/reflow/indent"
"github.com/olekukonko/tablewriter"
astext "github.com/yuin/goldmark/extension/ast"
)
// A TableElement is used to render tables.
@@ -13,6 +14,7 @@ type TableElement struct {
styleWriter *StyleWriter
header []string
cell []string
table *astext.Table
}
// A TableRowElement is used to render a single row in a table.
@@ -51,7 +53,22 @@ func (e *TableElement) Render(w io.Writer, ctx RenderContext) error {
renderText(w, ctx.options.ColorProfile, bs.Current().Style.StylePrimitive, rules.BlockPrefix)
renderText(ctx.table.styleWriter, ctx.options.ColorProfile, style, rules.Prefix)
ctx.table.writer = tablewriter.NewWriter(ctx.table.styleWriter)
table := tablewriter.NewWriter(ctx.table.styleWriter)
alignments := make([]int, len(e.table.Alignments))
for i, a := range e.table.Alignments {
switch a {
case astext.AlignLeft:
alignments[i] = tablewriter.ALIGN_LEFT
case astext.AlignCenter:
alignments[i] = tablewriter.ALIGN_CENTER
case astext.AlignRight:
alignments[i] = tablewriter.ALIGN_RIGHT
}
}
table.SetColumnAlignment(alignments)
ctx.table.writer = table
return nil
}