Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: render right margin for block stack elements #334

Open
wants to merge 3 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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) {
return 0
}
return uint(ctx.options.WordWrap) - s.Indent()*s.Margin()
return uint(ctx.options.WordWrap) - s.Indent() - s.Margin()*2
}

// 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 @@
"bytes"
"io"

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

Expand Down Expand Up @@ -60,28 +59,16 @@
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
Loading