Skip to content

Commit

Permalink
refactor: remove stylewriter from table
Browse files Browse the repository at this point in the history
  • Loading branch information
bashbunni committed Sep 28, 2023
1 parent 4730c03 commit a92f0f0
Showing 1 changed file with 14 additions and 30 deletions.
44 changes: 14 additions & 30 deletions ansi/table.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,10 +10,9 @@ import (

// A TableElement is used to render tables.
type TableElement struct {
lipgloss *table.Table
styleWriter *StyleWriter
headers []string
row []string
lipgloss *table.Table
headers []string
row []string
}

// A TableRowElement is used to render a single row in a table.
Expand All @@ -31,44 +30,32 @@ type TableCellElement struct {
func (e *TableElement) Render(w io.Writer, ctx RenderContext) error {
bs := ctx.blockStack

// TODO add indentation and margin for the table
// 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)
ctx.table.styleWriter = NewStyleWriter(ctx, w, style)

renderText(w, ctx.options.ColorProfile, bs.Current().Style.StylePrimitive, rules.BlockPrefix)
renderText(w, ctx.options.ColorProfile, style, rules.Prefix)
// ctx.table.writer = tablewriter.NewWriter(ctx.table.styleWriter)
ctx.table.lipgloss = table.New()
// TODO add indentation and margin for the table; parent should dictate this?
return nil
}

func (e *TableElement) Finish(w io.Writer, ctx RenderContext) error {
rules := ctx.options.Styles.Table

// TODO create style with custom separators
ctx.table.lipgloss.Border(lipgloss.NormalBorder())

// TODO remove styleWriter dep; not needed with lipgloss
ctx.table.styleWriter.Write([]byte(ctx.table.lipgloss.Render()))
// TODO is this hacky? what would be the better sol'n given that the writer we're receiving belongs to the ctx.BlockStack.Parent() and the original behaviour was using stylewriter to write to Current() block
ow := ctx.blockStack.Current().Block

ctx.table.lipgloss = nil
// TODO should prefix, suffix, and margins etc all be handled in the parent writer?
renderText(ow, ctx.options.ColorProfile, ctx.blockStack.With(rules.StylePrimitive), rules.Suffix)
renderText(ow, ctx.options.ColorProfile, ctx.blockStack.Current().Style.StylePrimitive, rules.BlockSuffix)
ow.Write([]byte(ctx.table.lipgloss.Render()))

renderText(ctx.table.styleWriter, ctx.options.ColorProfile, ctx.blockStack.With(rules.StylePrimitive), rules.Suffix)
renderText(ctx.table.styleWriter, ctx.options.ColorProfile, ctx.blockStack.Current().Style.StylePrimitive, rules.BlockSuffix)
return ctx.table.styleWriter.Close()
ctx.table.lipgloss = nil
return nil
}

func (e *TableRowElement) Finish(w io.Writer, ctx RenderContext) error {
Expand Down Expand Up @@ -106,13 +93,10 @@ func StringToAny(s []string) []any {
for i, str := range s {
out[i] = str
}
fmt.Print(s)

fmt.Print(out)
return out
}

// TODO apply individual cell styling here if desired
// TODO apply individual cell styling here if desired.
func (e *TableCellElement) Render(w io.Writer, ctx RenderContext) error {
if e.Head {
ctx.table.headers = append(ctx.table.headers, e.Text)
Expand Down

0 comments on commit a92f0f0

Please sign in to comment.