chalk / wrap-ansi

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

Handle spaces inside background color escapes #29

Closed vadimdemedes closed 5 years ago

vadimdemedes commented 5 years ago

Fixes https://github.com/chalk/wrap-ansi/issues/27.

The original issue is about wrap-ansi stripping the beginning space, if string is wrapped into an ansi escape (usually colors). I think it's ok for chalk.green(' hello '), because spaces won't be colorized anyway. However, for chalk.bgGreen(' hello ') this results in:

Reason it was happening is because wrap-ansi splits input string by space here https://github.com/chalk/wrap-ansi/blob/master/index.js#L82.

const words = chalk.bgGreen(' hello ');
const parts = words.split(' ');
//=> ['opening escape', 'hello', 'closing escape']

This PR fixes this case by scanning result of split() and joining opening escape, content and closing escape (following is pseudo code).

const normalizedParts = normalizeParts(parts);
//=> ['opening escape hello closing escape']

This PR is still failing, because it doesn't yet handle chalk.bgGreen(' hello ' + chalk.red('world')) case. Any help is appreciated!

vadimdemedes commented 5 years ago

I asked the author of https://github.com/chalk/wrap-ansi/pull/30 to copy tests from this PR, so that I can close this one, since the other one fixes many issues at once and has way less code.

sindresorhus commented 5 years ago

@vadimdemedes Can this be closed now?

vadimdemedes commented 5 years ago

Oh yes, sorry, forgot!