Skip to content
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
2 changes: 1 addition & 1 deletion ansi/heading.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ import (
"fmt"
"io"

"github.com/muesli/reflow/wordwrap"
"github.com/charmbracelet/glamour/internal/wordwrap"
)

// A HeadingElement is used to render headings.
Expand Down
2 changes: 1 addition & 1 deletion ansi/paragraph.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ import (
"io"
"strings"

"github.com/muesli/reflow/wordwrap"
"github.com/charmbracelet/glamour/internal/wordwrap"
)

// A ParagraphElement is used to render individual paragraphs.
Expand Down
18 changes: 18 additions & 0 deletions glamour_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -325,3 +325,21 @@ func TestWithChromaFormatterCustom(t *testing.T) {

golden.RequireEqual(t, []byte(b))
}

func TestWithWordWrap(t *testing.T) {
longLine := "Of course. Based on my exploration, this project is a command-line application written in Go. It acts as..."

r, err := NewTermRenderer(
WithWordWrap(88),
)
if err != nil {
t.Fatal(err)
}

out, err := r.Render(longLine)
if err != nil {
t.Fatal(err)
}

golden.RequireEqual(t, []byte(out))
}
168 changes: 168 additions & 0 deletions internal/wordwrap/wordwrap.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,168 @@
package wordwrap

import (
"bytes"
"strings"
"unicode"

"github.com/muesli/reflow/ansi"
)

var (
defaultBreakpoints = []rune{'-'}
defaultNewline = []rune{'\n'}
)

// WordWrap contains settings and state for customisable text reflowing with
// support for ANSI escape sequences. This means you can style your terminal
// output without affecting the word wrapping algorithm.
type WordWrap struct {
Limit int
Breakpoints []rune
Newline []rune
KeepNewlines bool

buf bytes.Buffer
space bytes.Buffer
word ansi.Buffer

lineLen int
ansi bool
}

// NewWriter returns a new instance of a word-wrapping writer, initialized with
// default settings.
func NewWriter(limit int) *WordWrap {
return &WordWrap{
Limit: limit,
Breakpoints: defaultBreakpoints,
Newline: defaultNewline,
KeepNewlines: true,
}
}

// Bytes is shorthand for declaring a new default WordWrap instance,
// used to immediately word-wrap a byte slice.
func Bytes(b []byte, limit int) []byte {
f := NewWriter(limit)
_, _ = f.Write(b)
_ = f.Close()

return f.Bytes()
}

// String is shorthand for declaring a new default WordWrap instance,
// used to immediately word-wrap a string.
func String(s string, limit int) string {
return string(Bytes([]byte(s), limit))
}

func (w *WordWrap) addSpace() {
w.lineLen += w.space.Len()
_, _ = w.buf.Write(w.space.Bytes())
w.space.Reset()
}

func (w *WordWrap) addWord() {
if w.word.Len() > 0 {
w.addSpace()
w.lineLen += w.word.PrintableRuneWidth()
_, _ = w.buf.Write(w.word.Bytes())
w.word.Reset()
}
}

func (w *WordWrap) addNewLine() {
_, _ = w.buf.WriteRune('\n')
w.lineLen = 0
w.space.Reset()
}

func inGroup(a []rune, c rune) bool {
for _, v := range a {
if v == c {
return true
}
}
return false
}

// Write is used to write more content to the word-wrap buffer.
func (w *WordWrap) Write(b []byte) (int, error) {
if w.Limit == 0 {
return w.buf.Write(b)
}

s := string(b)
if !w.KeepNewlines {
s = strings.Replace(strings.TrimSpace(s), "\n", " ", -1)
}

for _, c := range s {
if c == '\x1B' {
// ANSI escape sequence
_, _ = w.word.WriteRune(c)
w.ansi = true
} else if w.ansi {
_, _ = w.word.WriteRune(c)
if (c >= 0x40 && c <= 0x5a) || (c >= 0x61 && c <= 0x7a) {
// ANSI sequence terminated
w.ansi = false
}
} else if inGroup(w.Newline, c) {
// end of current line
// see if we can add the content of the space buffer to the current line
if w.word.Len() == 0 {
if w.lineLen+w.space.Len() > w.Limit {
w.lineLen = 0
} else {
// preserve whitespace
_, _ = w.buf.Write(w.space.Bytes())
}
w.space.Reset()
}

w.addWord()
w.addNewLine()
} else if unicode.IsSpace(c) {
// end of current word
w.addWord()
_, _ = w.space.WriteRune(c)
} else if inGroup(w.Breakpoints, c) {
// valid breakpoint
w.addSpace()
w.addWord()
_, _ = w.buf.WriteRune(c)
w.lineLen++
Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This single line was the fix, a PR is open here: muesli/reflow#77

Not sure how you feel about vendoring the wordwrap logic but the muesli/reflow repo seemed to not be active and waiting for a PR merged there may not be fun.

} else {
// any other character
_, _ = w.word.WriteRune(c)

// add a line break if the current word would exceed the line's
// character limit
if w.lineLen+w.space.Len()+w.word.PrintableRuneWidth() > w.Limit &&
w.word.PrintableRuneWidth() < w.Limit {
w.addNewLine()
}
}
}

return len(b), nil
}

// Close will finish the word-wrap operation. Always call it before trying to
// retrieve the final result.
func (w *WordWrap) Close() error {
w.addWord()
return nil
}

// Bytes returns the word-wrapped result as a byte slice.
func (w *WordWrap) Bytes() []byte {
return w.buf.Bytes()
}

// String returns the word-wrapped result as a string.
func (w *WordWrap) String() string {
return w.buf.String()
}
162 changes: 162 additions & 0 deletions internal/wordwrap/wordwrap_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,162 @@
package wordwrap

import (
"testing"
)

func TestWordWrap(t *testing.T) {
tt := []struct {
Input string
Expected string
Limit int
KeepNewlines bool
}{
// No-op, should pass through, including trailing whitespace:
{
"foobar\n ",
"foobar\n ",
0,
true,
},
// Nothing to wrap here, should pass through:
{
"foo",
"foo",
4,
true,
},
// A single word that is too long passes through.
// We do not break long words:
{
"foobarfoo",
"foobarfoo",
4,
true,
},
// Lines are broken at whitespace:
{
"foo bar foo",
"foo\nbar\nfoo",
4,
true,
},
// A hyphen is a valid breakpoint:
{
"foo-foobar",
"foo-\nfoobar",
4,
true,
},
// Space buffer needs to be emptied before breakpoints:
{
"foo --bar",
"foo --bar",
9,
true,
},
// Lines are broken at whitespace, even if words
// are too long. We do not break words:
{
"foo bars foobars",
"foo\nbars\nfoobars",
4,
true,
},
// A word that would run beyond the limit is wrapped:
{
"foo bar",
"foo\nbar",
5,
true,
},
// Whitespace that trails a line and fits the width
// passes through, as does whitespace prefixing an
// explicit line break. A tab counts as one character:
{
"foo\nb\t a\n bar",
"foo\nb\t a\n bar",
4,
true,
},
// Trailing whitespace is removed if it doesn't fit the width.
// Runs of whitespace on which a line is broken are removed:
{
"foo \nb ar ",
"foo\nb\nar",
4,
true,
},
// An explicit line break at the end of the input is preserved:
{
"foo bar foo\n",
"foo\nbar\nfoo\n",
4,
true,
},
// Explicit break are always preserved:
{
"\nfoo bar\n\n\nfoo\n",
"\nfoo\nbar\n\n\nfoo\n",
4,
true,
},
// Unless we ask them to be ignored:
{
"\nfoo bar\n\n\nfoo\n",
"foo\nbar\nfoo",
4,
false,
},
// Complete example:
{
" This is a list: \n\n\t* foo\n\t* bar\n\n\n\t* foo \nbar ",
" This\nis a\nlist: \n\n\t* foo\n\t* bar\n\n\n\t* foo\nbar",
6,
true,
},
// ANSI sequence codes don't affect length calculation:
{
"\x1B[38;2;249;38;114mfoo\x1B[0m\x1B[38;2;248;248;242m \x1B[0m\x1B[38;2;230;219;116mbar\x1B[0m",
"\x1B[38;2;249;38;114mfoo\x1B[0m\x1B[38;2;248;248;242m \x1B[0m\x1B[38;2;230;219;116mbar\x1B[0m",
7,
true,
},
// ANSI control codes don't get wrapped:
{
"\x1B[38;2;249;38;114m(\x1B[0m\x1B[38;2;248;248;242mjust another test\x1B[38;2;249;38;114m)\x1B[0m",
"\x1B[38;2;249;38;114m(\x1B[0m\x1B[38;2;248;248;242mjust\nanother\ntest\x1B[38;2;249;38;114m)\x1B[0m",
3,
true,
},
// A long incorrectly formatted string:
{
"wqrs zxbvnpm ft y cvbmnkl-pqrs tyymlnbkjhg qafggsa mk pb",
"wqrs zxbvnpm ft y cvbmnkl-pqrs tyymlnbkjhg qafggsa\nmk pb",
52,
false,
},
}

for i, tc := range tt {
f := NewWriter(tc.Limit)
f.KeepNewlines = tc.KeepNewlines

_, err := f.Write([]byte(tc.Input))
if err != nil {
t.Error(err)
}
f.Close()

if f.String() != tc.Expected {
t.Errorf("Test %d, expected:\n\n`%s`\n\nActual Output:\n\n`%s`", i, tc.Expected, f.String())
}
}
}

func TestWordWrapString(t *testing.T) {
actual := String("foo bar", 3)
expected := "foo\nbar"
if actual != expected {
t.Errorf("expected:\n\n`%s`\n\nActual Output:\n\n`%s`", expected, actual)
}
}
2 changes: 2 additions & 0 deletions testdata/TestWithWordWrap.golden

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