Skip to content

Commit

Permalink
wip: headings
Browse files Browse the repository at this point in the history
  • Loading branch information
bashbunni committed Oct 13, 2023
1 parent c43239b commit 7442825
Showing 1 changed file with 39 additions and 29 deletions.
68 changes: 39 additions & 29 deletions ansi/heading.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@ package ansi

import (
"bytes"
"fmt"
"io"
)

Expand All @@ -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()
Expand Down

0 comments on commit 7442825

Please sign in to comment.