Skip to content

Commit

Permalink
fix: tables
Browse files Browse the repository at this point in the history
  • Loading branch information
caarlos0 committed Jul 22, 2024
1 parent 2e84e54 commit cc0a410
Show file tree
Hide file tree
Showing 15 changed files with 82 additions and 118 deletions.
47 changes: 26 additions & 21 deletions ansi/table.go
Original file line number Diff line number Diff line change
Expand Up @@ -52,30 +52,34 @@ func (e *TableElement) Render(w io.Writer, ctx RenderContext) error {
renderText(iw, ctx.options.ColorProfile, bs.Current().Style.StylePrimitive, rules.BlockPrefix)
renderText(iw, ctx.options.ColorProfile, style, rules.Prefix)
width := int(ctx.blockStack.Width(ctx))
ctx.table.lipgloss = table.New().
Width(width).
StyleFunc(func(row, col int) lipgloss.Style {
st := lipgloss.NewStyle()
if m := ctx.options.Styles.Table.Margin; m != nil {
st = st.Margin(0, int(*m))
}
if row == 0 {
st = st.Bold(true)
}
ctx.table.lipgloss = table.New().Width(width)

switch e.table.Alignments[col] {
case astext.AlignLeft:
st = st.Align(lipgloss.Left)
case astext.AlignCenter:
st = st.Align(lipgloss.Center)
case astext.AlignRight:
st = st.Align(lipgloss.Right)
}
return nil
}

func (e *TableElement) setStyles(ctx RenderContext) {
ctx.table.lipgloss = ctx.table.lipgloss.StyleFunc(func(row, col int) lipgloss.Style {
st := lipgloss.NewStyle()
if m := ctx.options.Styles.Table.Margin; m != nil {
st = st.Padding(0, int(*m))
} else {
st = st.Padding(0, 1)
}
if row == 0 {
st = st.Bold(true)
}

return st
})
switch e.table.Alignments[col] {
case astext.AlignLeft:
st = st.Align(lipgloss.Left)
case astext.AlignCenter:
st = st.Align(lipgloss.Center)
case astext.AlignRight:
st = st.Align(lipgloss.Right)
}

return nil
return st
})
}

func (e *TableElement) setBorders(ctx RenderContext) {
Expand All @@ -101,6 +105,7 @@ func (e *TableElement) setBorders(ctx RenderContext) {
func (e *TableElement) Finish(_ io.Writer, ctx RenderContext) error {
rules := ctx.options.Styles.Table

e.setStyles(ctx)
e.setBorders(ctx)

ow := ctx.blockStack.Current().Block
Expand Down
4 changes: 2 additions & 2 deletions ansi/testdata/TestRendererIssues/117.golden

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

4 changes: 2 additions & 2 deletions ansi/testdata/TestRendererIssues/315.golden

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

6 changes: 3 additions & 3 deletions ansi/testdata/TestRendererIssues/316.golden

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

12 changes: 6 additions & 6 deletions ansi/testdata/TestRendererIssues/44.golden

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Loading

0 comments on commit cc0a410

Please sign in to comment.