Skip to content

Commit

Permalink
fix: render right margin for block stack elements (#334)
Browse files Browse the repository at this point in the history
* Fix spacing for block stack elements

* update golden files

* multiple margin by 2 to account for margin-right

Co-authored-by: Maximilian Moehl <[email protected]>

---------

Co-authored-by: Maximilian Moehl <[email protected]>
  • Loading branch information
jahvon and maxmoehl authored Nov 25, 2024
1 parent a7ad15f commit 9cedaca
Show file tree
Hide file tree
Showing 30 changed files with 294 additions and 301 deletions.
4 changes: 2 additions & 2 deletions ansi/blockstack.go
Original file line number Diff line number Diff line change
Expand Up @@ -59,10 +59,10 @@ func (s BlockStack) Margin() uint {

// Width returns the available rendering width.
func (s BlockStack) Width(ctx RenderContext) uint {
if s.Indent()*s.Margin() > uint(ctx.options.WordWrap) {
if s.Indent()+s.Margin()*2 > uint(ctx.options.WordWrap) {

Check failure on line 62 in ansi/blockstack.go

View workflow job for this annotation

GitHub Actions / lint-soft

G115: integer overflow conversion int -> uint (gosec)
return 0
}
return uint(ctx.options.WordWrap) - s.Indent()*s.Margin()
return uint(ctx.options.WordWrap) - s.Indent() - s.Margin()*2

Check failure on line 65 in ansi/blockstack.go

View workflow job for this annotation

GitHub Actions / lint-soft

G115: integer overflow conversion int -> uint (gosec)
}

// Parent returns the current BlockElement's parent.
Expand Down
19 changes: 3 additions & 16 deletions ansi/heading.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,6 @@ import (
"bytes"
"io"

"github.com/muesli/reflow/indent"
"github.com/muesli/reflow/wordwrap"
)

Expand Down Expand Up @@ -60,28 +59,16 @@ func (e *HeadingElement) Render(w io.Writer, ctx RenderContext) error {
func (e *HeadingElement) Finish(w io.Writer, ctx RenderContext) error {
bs := ctx.blockStack
rules := bs.Current().Style
mw := NewMarginWriter(ctx, w, rules)

var indentation uint
var margin uint
if rules.Indent != nil {
indentation = *rules.Indent
}
if rules.Margin != nil {
margin = *rules.Margin
}

iw := indent.NewWriterPipe(w, indentation+margin, func(wr io.Writer) {
renderText(w, ctx.options.ColorProfile, bs.Parent().Style.StylePrimitive, " ")
})

flow := wordwrap.NewWriter(int(bs.Width(ctx) - indentation - margin*2))
flow := wordwrap.NewWriter(int(bs.Width(ctx)))

Check failure on line 64 in ansi/heading.go

View workflow job for this annotation

GitHub Actions / lint-soft

G115: integer overflow conversion uint -> int (gosec)
_, err := flow.Write(bs.Current().Block.Bytes())
if err != nil {
return err
}
flow.Close()

_, err = iw.Write(flow.Bytes())
_, err = mw.Write(flow.Bytes())
if err != nil {
return err
}
Expand Down
2 changes: 1 addition & 1 deletion ansi/testdata/TestRenderer/block_quote.golden

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion ansi/testdata/TestRenderer/list.golden

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Loading

0 comments on commit 9cedaca

Please sign in to comment.