okami.mom/glamour
git clone https://git.nkp.pet/okami.mom/glamour
c293649
main
1package ansi 2 3import ( 4"bytes" 5"fmt" 6"io" 7 8"charm.land/lipgloss/v2" 9) 10 11// BlockElement provides a render buffer for children of a block element. 12// After all children have been rendered into it, it applies indentation and 13// margins around them and writes everything to the parent rendering buffer. 14type BlockElement struct { 15Block * bytes.Buffer 16Style StyleBlock 17Margin bool 18Newline bool 19} 20 21// Render renders a BlockElement. 22func (e * BlockElement )Render (w io.Writer ,ctx RenderContext )error { 23bs := ctx .blockStack 24bs .Push (* e ) 25 26_ ,_ = renderText (w ,bs .Parent ().Style .StylePrimitive ,e .Style .BlockPrefix ) 27_ ,_ = renderText (bs .Current ().Block ,bs .Current ().Style .StylePrimitive ,e .Style .Prefix ) 28return nil 29} 30 31// Finish finishes rendering a BlockElement. 32func (e * BlockElement )Finish (w io.Writer ,ctx RenderContext )error { 33bs := ctx .blockStack 34 35if e .Margin {//nolint: nestif 36s := lipgloss .Wrap ( 37bs .Current ().Block .String (), 38int (bs .Width (ctx )), 39" ,.;-+|" , 40 ) 41 42mw := NewMarginWriter (ctx ,w ,bs .Current ().Style ) 43defer mw .Close ()//nolint:errcheck 44if _ ,err := io .WriteString (mw ,s );err != nil { 45return fmt .Errorf ("glamour: error writing to writer: %w" ,err ) 46 } 47 48if e .Newline { 49if _ ,err := io .WriteString (mw ,"\n" );err != nil { 50return fmt .Errorf ("glamour: error writing to writer: %w" ,err ) 51 } 52 } 53 }else { 54_ ,err := bs .Parent ().Block .Write (bs .Current ().Block .Bytes ()) 55if err != nil { 56return fmt .Errorf ("glamour: error writing to writer: %w" ,err ) 57 } 58 } 59 60_ ,_ = renderText (w ,bs .Current ().Style .StylePrimitive ,e .Style .Suffix ) 61_ ,_ = renderText (w ,bs .Parent ().Style .StylePrimitive ,e .Style .BlockSuffix ) 62 63bs .Current ().Block .Reset () 64bs .Pop () 65return nil 66}