-
Notifications
You must be signed in to change notification settings - Fork 235
A bug in word wrapping (with a failing test case) #452
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
Open
markusylisiurunen
wants to merge
3
commits into
charmbracelet:master
Choose a base branch
from
markusylisiurunen:fix-word-wrap
base: master
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
Show all changes
3 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| 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++ | ||
| } 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() | ||
| } | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| 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) | ||
| } | ||
| } |
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
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/reflowrepo seemed to not be active and waiting for a PR merged there may not be fun.