Skip to content

Commit

Permalink
fix: blocks and word wrap
Browse files Browse the repository at this point in the history
  • Loading branch information
caarlos0 committed Jul 18, 2024
1 parent 63dca24 commit a4e220d
Show file tree
Hide file tree
Showing 35 changed files with 324 additions and 329 deletions.
12 changes: 8 additions & 4 deletions ansi/blockelement.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ import (
"bytes"
"io"

"github.com/muesli/reflow/wordwrap"
"github.com/charmbracelet/x/ansi"
)

// BlockElement provides a render buffer for children of a block element.
Expand All @@ -30,10 +30,14 @@ func (e *BlockElement) Finish(w io.Writer, ctx RenderContext) error {
bs := ctx.blockStack

if e.Margin {
s := ansi.Wordwrap(
bs.Current().Block.String(),
int(bs.Width(ctx)),
" ,.;-+|",
)

mw := NewMarginWriter(ctx, w, bs.Current().Style)
_, err := mw.Write(
wordwrap.Bytes(bs.Current().Block.Bytes(), int(bs.Width(ctx))))
if err != nil {
if _, err := io.WriteString(mw, s); err != nil {
return err
}

Expand Down
1 change: 1 addition & 0 deletions ansi/codeblock.go
Original file line number Diff line number Diff line change
Expand Up @@ -128,6 +128,7 @@ func (e *CodeBlockElement) Render(w io.Writer, ctx RenderContext) error {

if len(theme) > 0 {
renderText(iw, ctx.options.ColorProfile, bs.Current().Style.StylePrimitive, rules.BlockPrefix)

err := quick.Highlight(iw, e.Code, e.Language, "terminal256", theme)
if err != nil {
return err
Expand Down
3 changes: 1 addition & 2 deletions ansi/testdata/TestRenderer/block_quote.golden
Original file line number Diff line number Diff line change
@@ -1,3 +1,2 @@

=> First line of quote                                                            
=> Second line                                                                    
=> First line of quote Second line                                                
2 changes: 1 addition & 1 deletion ansi/testdata/TestRenderer/list.golden
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@

• First Item                                                                    
    • Nested List Item                                                          
    • Nested List Item                                                            
• Second Item                                                                   
Loading

0 comments on commit a4e220d

Please sign in to comment.