diff --git a/ansi/heading.go b/ansi/heading.go index 9ee5d34a..c4147bb5 100644 --- a/ansi/heading.go +++ b/ansi/heading.go @@ -2,7 +2,6 @@ package ansi import ( "bytes" - "fmt" "io" ) @@ -23,48 +22,59 @@ const ( func (e *HeadingElement) Render(w io.Writer, ctx RenderContext) error { bs := ctx.blockStack - rules := ctx.options.Styles.Heading + be := BlockElement{ + Block: &bytes.Buffer{}, + Style: bs.Current().Style, + } + bs.Push(be) + + // renderText(w, bs.Parent().Style.StylePrimitive.Style(), rules.BlockPrefix) + // renderText(bs.Current().Block, bs.Current().Style.StylePrimitive.Style(), rules.Prefix) + return nil +} + +func (e *HeadingElement) Finish(w io.Writer, ctx RenderContext) error { + bs := ctx.blockStack + block := bs.Current().Style + heading := ctx.options.Styles.Heading + subheading := ctx.options.Styles.Heading switch e.Level { case h1: - rules = ctx.options.Styles.H1 + subheading = ctx.options.Styles.H1 case h2: - rules = ctx.options.Styles.H2 + subheading = ctx.options.Styles.H2 case h3: - rules = ctx.options.Styles.H3 + subheading = ctx.options.Styles.H3 case h4: - rules = ctx.options.Styles.H4 + subheading = ctx.options.Styles.H4 case h5: - rules = ctx.options.Styles.H5 + subheading = ctx.options.Styles.H5 case h6: - rules = ctx.options.Styles.H6 + subheading = ctx.options.Styles.H6 } - if !e.First { - renderText(w, bs.Current().Style.StylePrimitive.Style(), "\n") - } + blockStyle := block.Style().Margin(0) + headingStyle := heading.Style().Inherit(blockStyle) + style := subheading.Style().Inherit(headingStyle) - be := BlockElement{ - Block: &bytes.Buffer{}, - Style: bs.Current().Style, + if !e.First { + // renderText(w, bs.Current().Style.StylePrimitive.Style(), "\n") + w.Write([]byte("\n")) } - bs.Push(be) - - renderText(w, bs.Parent().Style.StylePrimitive.Style(), rules.BlockPrefix) - renderText(bs.Current().Block, bs.Current().Style.StylePrimitive.Style(), rules.Prefix) - return nil -} - -func (e *HeadingElement) Finish(w io.Writer, ctx RenderContext) error { - bs := ctx.blockStack - rules := bs.Current().Style + w.Write([]byte( + style.Render( + subheading.BlockPrefix + + subheading.Prefix + + bs.Current().Block.String() + + subheading.Suffix + + subheading.BlockSuffix))) // TODO set/handle width - val := (rules.Style().Render(bs.Current().Block.String())) - fmt.Print(val) - w.Write([]byte(val)) - renderText(w, bs.Current().Style.StylePrimitive.Style(), rules.Suffix) - renderText(w, bs.Parent().Style.StylePrimitive.Style(), rules.BlockSuffix) + // val := (headingStyle.Render(bs.Current().Block.String())) + // w.Write([]byte(val)) + // renderText(w, bs.Current().Style.StylePrimitive.Style(), rules.Suffix) + // renderText(w, bs.Parent().Style.StylePrimitive.Style(), rules.BlockSuffix) bs.Current().Block.Reset() bs.Pop()