reflow icon indicating copy to clipboard operation
reflow copied to clipboard

Extra newlines when combining unconditional and standard word wrapping

Open meowgorithm opened this issue 3 years ago • 5 comments

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

meowgorithm avatar Sep 22 '21 19:09 meowgorithm

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

bashbunni avatar Sep 14 '23 23:09 bashbunni

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.

janderland avatar Sep 17 '23 19:09 janderland

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.

muesli avatar Sep 18 '23 13:09 muesli

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 avatar Sep 18 '23 19:09 janderland

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.

janderland avatar Nov 05 '23 02:11 janderland