1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170
|
package ansi
import (
"bytes"
"io"
"github.com/charmbracelet/lipgloss"
"github.com/charmbracelet/lipgloss/table"
"github.com/muesli/reflow/indent"
astext "github.com/yuin/goldmark/extension/ast"
)
// A TableElement is used to render tables.
type TableElement struct {
lipgloss *table.Table
table *astext.Table
header []string
row []string
}
// A TableRowElement is used to render a single row in a table.
type TableRowElement struct{}
// A TableHeadElement is used to render a table's head element.
type TableHeadElement struct{}
// A TableCellElement is used to render a single cell in a row.
type TableCellElement struct {
Children []ElementRenderer
Head bool
}
func (e *TableElement) Render(w io.Writer, ctx RenderContext) error {
bs := ctx.blockStack
var indentation uint
var margin uint
rules := ctx.options.Styles.Table
if rules.Indent != nil {
indentation = *rules.Indent
}
if rules.Margin != nil {
margin = *rules.Margin
}
iw := indent.NewWriterPipe(w, indentation+margin, func(wr io.Writer) {
renderText(w, ctx.options.ColorProfile, bs.Current().Style.StylePrimitive, " ")
})
style := bs.With(rules.StylePrimitive)
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)
return nil
}
func (e *TableElement) setStyles(ctx RenderContext) {
ctx.table.lipgloss = ctx.table.lipgloss.StyleFunc(func(row, col int) lipgloss.Style {
st := lipgloss.NewStyle().Inline(true)
if m := ctx.options.Styles.Table.Margin; m != nil {
st = st.Padding(0, int(*m))
}
if row == 0 {
st = st.Bold(true)
}
switch e.table.Alignments[col] {
case astext.AlignLeft:
st = st.Align(lipgloss.Left).PaddingRight(0)
case astext.AlignCenter:
st = st.Align(lipgloss.Center)
case astext.AlignRight:
st = st.Align(lipgloss.Right).PaddingLeft(0)
}
return st
})
}
func (e *TableElement) setBorders(ctx RenderContext) {
rules := ctx.options.Styles.Table
border := lipgloss.NormalBorder()
if rules.RowSeparator != nil && rules.ColumnSeparator != nil {
border = lipgloss.Border{
Top: *rules.RowSeparator,
Bottom: *rules.RowSeparator,
Left: *rules.ColumnSeparator,
Right: *rules.ColumnSeparator,
Middle: *rules.CenterSeparator,
}
}
ctx.table.lipgloss.Border(border)
ctx.table.lipgloss.BorderTop(false)
ctx.table.lipgloss.BorderLeft(false)
ctx.table.lipgloss.BorderRight(false)
ctx.table.lipgloss.BorderBottom(false)
}
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
if _, err := ow.WriteString(ctx.table.lipgloss.String()); err != nil {
return err
}
renderText(ow, ctx.options.ColorProfile, ctx.blockStack.With(rules.StylePrimitive), rules.Suffix)
renderText(ow, ctx.options.ColorProfile, ctx.blockStack.Current().Style.StylePrimitive, rules.BlockSuffix)
ctx.table.lipgloss = nil
return nil
}
func (e *TableRowElement) Finish(_ io.Writer, ctx RenderContext) error {
if ctx.table.lipgloss == nil {
return nil
}
ctx.table.lipgloss.Row(ctx.table.row...)
ctx.table.row = []string{}
return nil
}
func (e *TableHeadElement) Finish(_ io.Writer, ctx RenderContext) error {
if ctx.table.lipgloss == nil {
return nil
}
ctx.table.lipgloss.Headers(ctx.table.header...)
ctx.table.header = []string{}
return nil
}
func (e *TableCellElement) Render(_ io.Writer, ctx RenderContext) error {
var b bytes.Buffer
style := ctx.options.Styles.Table.StylePrimitive
for _, child := range e.Children {
if r, ok := child.(StyleOverriderElementRenderer); ok {
if err := r.StyleOverrideRender(&b, ctx, style); err != nil {
return err
}
} else {
var bb bytes.Buffer
if err := child.Render(&bb, ctx); err != nil {
return err
}
el := &BaseElement{
Token: bb.String(),
Style: style,
}
if err := el.Render(&b, ctx); err != nil {
return err
}
}
}
if e.Head {
ctx.table.header = append(ctx.table.header, b.String())
} else {
ctx.table.row = append(ctx.table.row, b.String())
}
return nil
}
|