Skip to content

Commit

Permalink
fix: properly set table alignments
Browse files Browse the repository at this point in the history
  • Loading branch information
maaslalani committed Feb 26, 2024
1 parent e526301 commit 3477c0e
Show file tree
Hide file tree
Showing 2 changed files with 20 additions and 2 deletions.
3 changes: 2 additions & 1 deletion ansi/elements.go
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down
19 changes: 18 additions & 1 deletion ansi/table.go
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand All @@ -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.
Expand Down Expand Up @@ -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
}

Expand Down

0 comments on commit 3477c0e

Please sign in to comment.