muesli / reflow

A collection of (ANSI-sequence aware) text reflow operations & algorithms
MIT License
643 stars 40 forks source link

Extra newlines when combining unconditional and standard word wrapping #43

Open meowgorithm opened 3 years ago

meowgorithm commented 3 years ago

When combining word-wrapping with unconditional wrapping as described in the README, extra linebreaks can sometimes be found in the output.

For example:

const str = "the quick brown foxxxxxxxxxxxxxxxx jumped over the lazy dog."
const limit = 16
wrapped := wrap.String(wordwrap.String(str, limit), limit)
fmt.Println(wrapped)

Outputs:

the quick brown
foxxxxxxxxxxxxxx
xx
jumped over the
lazy dog.

However, I'd expect it to be:

the quick brown
foxxxxxxxxxxxxxx
xx jumped over 
the lazy dog.

Playground Example

bashbunni commented 1 year ago

We could probably enhance wordwrap to include forceful newlines to break words at the given width. The wrapper is behaving as expected in this case. (New lines are preserved by default) https://github.com/bashbunni/reflow/blob/master/wrap/wrap.go#L36

janderland commented 1 year ago

I'm also running into this issue. I may be able to take a stab at implementing what @bashbunni is describing if she isn't doing so already.

muesli commented 1 year ago

I'm also running into this issue. I may be able to take a stab at implementing what @bashbunni is describing if she isn't doing so already.

Depending on what you actually need, you can also just initialize a new wrap.Writer and set PreserveNewlines to false.

janderland commented 1 year ago

I'm also running into this issue. I may be able to take a stab at implementing what @bashbunni is describing if she isn't doing so already.

Depending on what you actually need, you can also just initialize a new wrap.Writer and set PreserveNewlines to false.

By setting wrap.Wrap.PreserveNewlines to false, won't this cause wrap.Wrap to undo the work which wordwrap.Wrap did? wordwrap.Wrap is only adding newlines to the string. If we don't preserve them it will be as if wordwrap.Wrap never modified the original string, right?

janderland commented 11 months ago

OK, I took a stab at adding this feature the library but ran into some problems while trying to maintain the current functionality. To better wrap my head around the problem, I implemented the wrapping functionality I need in my own project. I'm still open to porting this functionality back into this project if it's something the author wants.