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// A HeadingElement is used to render headings. 12type HeadingElement struct { 13Level int 14First bool 15} 16 17const ( 18h1 = iota + 1 19h2 20h3 21h4 22h5 23h6 24) 25 26// Render renders a HeadingElement. 27func (e * HeadingElement )Render (w io.Writer ,ctx RenderContext )error { 28bs := ctx .blockStack 29rules := ctx .options .Styles .Heading 30 31switch e .Level { 32case h1 : 33rules = cascadeStyles (rules ,ctx .options .Styles .H1 ) 34case h2 : 35rules = cascadeStyles (rules ,ctx .options .Styles .H2 ) 36case h3 : 37rules = cascadeStyles (rules ,ctx .options .Styles .H3 ) 38case h4 : 39rules = cascadeStyles (rules ,ctx .options .Styles .H4 ) 40case h5 : 41rules = cascadeStyles (rules ,ctx .options .Styles .H5 ) 42case h6 : 43rules = cascadeStyles (rules ,ctx .options .Styles .H6 ) 44 } 45 46if ! e .First { 47_ ,_ = renderText (w ,bs .Current ().Style .StylePrimitive ,"\n" ) 48 } 49 50be := BlockElement { 51Block :& bytes.Buffer {}, 52Style :cascadeStyle (bs .Current ().Style ,rules ,false ), 53 } 54bs .Push (be ) 55 56_ ,_ = renderText (w ,bs .Parent ().Style .StylePrimitive ,rules .BlockPrefix ) 57_ ,_ = renderText (bs .Current ().Block ,bs .Current ().Style .StylePrimitive ,rules .Prefix ) 58return nil 59} 60 61// Finish finishes rendering a HeadingElement. 62func (e * HeadingElement )Finish (w io.Writer ,ctx RenderContext )error { 63bs := ctx .blockStack 64rules := bs .Current ().Style 65mw := NewMarginWriter (ctx ,w ,rules ) 66defer mw .Close ()//nolint:errcheck 67 68flow := lipgloss .Wrap (bs .Current ().Block .String (),int (bs .Width (ctx )),"" ) 69_ ,err := io .WriteString (mw ,flow ) 70if err != nil { 71return fmt .Errorf ("glamour: error writing to writer: %w" ,err ) 72 } 73 74_ ,_ = renderText (w ,bs .Current ().Style .StylePrimitive ,rules .Suffix ) 75_ ,_ = renderText (w ,bs .Parent ().Style .StylePrimitive ,rules .BlockSuffix ) 76 77bs .Current ().Block .Reset () 78bs .Pop () 79return nil 80}