From 30ed5cc5ef63d2cfbb0956c512644e4293814fd3 Mon Sep 17 00:00:00 2001 From: Christian Muehlhaeuser Date: Thu, 12 Dec 2019 21:37:23 +0100 Subject: [PATCH] Move BlockElement into a separate file --- blockelement.go | 49 +++++++++++++++++++++++++++++++++++++++++++++++++ blockstack.go | 5 ----- 2 files changed, 49 insertions(+), 5 deletions(-) create mode 100644 blockelement.go diff --git a/blockelement.go b/blockelement.go new file mode 100644 index 0000000..9bfe6d5 --- /dev/null +++ b/blockelement.go @@ -0,0 +1,49 @@ +package gold + +import ( + "bytes" + "io" + + "github.com/muesli/reflow" +) + +type BlockElement struct { + Block *bytes.Buffer + Style ElementStyle + Margin bool +} + +func (e *BlockElement) Render(w io.Writer, ctx RenderContext) error { + bs := ctx.blockStack + bs.Push(*e) + + renderText(w, bs.Parent().Style, e.Style.Prefix) + renderText(w, bs.Current().Style, e.Style.StyledPrefix) + return nil +} + +func (e *BlockElement) Finish(w io.Writer, ctx RenderContext) error { + bs := ctx.blockStack + + if e.Margin { + mw := NewMarginWriter(ctx, w, bs.Current().Style) + _, err := mw.Write( + reflow.Bytes(bs.Current().Block.Bytes(), int(bs.Width(ctx)))) + if err != nil { + return err + } + mw.Write([]byte("\n")) + } else { + _, err := bs.Parent().Block.Write(bs.Current().Block.Bytes()) + if err != nil { + return err + } + } + + renderText(w, bs.Current().Style, e.Style.StyledSuffix) + renderText(w, bs.Parent().Style, e.Style.Suffix) + + bs.Current().Block.Reset() + bs.Pop() + return nil +} diff --git a/blockstack.go b/blockstack.go index 502763c..7d54201 100644 --- a/blockstack.go +++ b/blockstack.go @@ -4,11 +4,6 @@ import ( "bytes" ) -type BlockElement struct { - Block *bytes.Buffer - Style ElementStyle -} - type BlockStack []BlockElement func (s *BlockStack) Len() int {