chalk / wrap-ansi

Wordwrap a string with ANSI escape codes
MIT License
120 stars 25 forks source link

Fix space issues at the end of input string #21

Closed SamVerschueren closed 7 years ago

SamVerschueren commented 7 years ago

This PR fixes the jumpy spaces in log-update. Let me explain what's going on.

Suppose we have a input string like this

'123456789\n '

The string is cut into separate lines

['123456789', ' ']

Let's wrap at 5. The first word is split on spaces, which it doesn't have so we can then cut at the 5th character.

['12345\n6789', ...]

The next word (empty space) is now taken being processed. The word is split at a space character, and thus that line now has 2 words.

['', '']

Because of this line, this word will now end up as 2 spaces, and thus has one space to many.

['12345\n6789', ' ']

I fixed this by bailing out early.

sindresorhus commented 7 years ago

Good detective work!