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"strings" 8 9"charm.land/lipgloss/v2" 10) 11 12// A ParagraphElement is used to render individual paragraphs. 13type ParagraphElement struct { 14First bool 15} 16 17// Render renders a ParagraphElement. 18func (e * ParagraphElement )Render (w io.Writer ,ctx RenderContext )error { 19bs := ctx .blockStack 20rules := ctx .options .Styles .Paragraph 21 22if ! e .First { 23_ ,_ = io .WriteString (w ,"\n" ) 24 } 25be := BlockElement { 26Block :& bytes.Buffer {}, 27Style :cascadeStyle (bs .Current ().Style ,rules ,false ), 28 } 29bs .Push (be ) 30 31_ ,_ = renderText (w ,bs .Parent ().Style .StylePrimitive ,rules .BlockPrefix ) 32_ ,_ = renderText (bs .Current ().Block ,bs .Current ().Style .StylePrimitive ,rules .Prefix ) 33return nil 34} 35 36// Finish finishes rendering a ParagraphElement. 37func (e * ParagraphElement )Finish (w io.Writer ,ctx RenderContext )error { 38bs := ctx .blockStack 39rules := bs .Current ().Style 40 41mw := NewMarginWriter (ctx ,w ,rules ) 42defer mw .Close ()//nolint:errcheck 43if len (strings .TrimSpace (bs .Current ().Block .String ()))> 0 { 44blk := bs .Current ().Block .String () 45if ! ctx .options .PreserveNewLines { 46blk = strings .ReplaceAll (blk ,"\n" ," " ) 47 } 48flow := lipgloss .Wrap (blk ,int (bs .Width (ctx )),"" ) 49 50_ ,err := io .WriteString (mw ,flow ) 51if err != nil { 52return fmt .Errorf ("glamour: error writing to writer: %w" ,err ) 53 } 54_ ,_ = io .WriteString (mw ,"\n" ) 55 } 56 57_ ,_ = renderText (w ,bs .Current ().Style .StylePrimitive ,rules .Suffix ) 58_ ,_ = renderText (w ,bs .Parent ().Style .StylePrimitive ,rules .BlockSuffix ) 59 60bs .Current ().Block .Reset () 61bs .Pop () 62return nil 63}