chalk/wrap-ansi

Inconsistant styling for strings with background and foreground.

Opened this issue · 1 comments

Something similar to slice-ansi: chalk/slice-ansi#22

My use case is wrapping strings to terminal width, and only showing some lines. The following example works as expected:

var string = chalk.bgGreen("test");
var wrapped = wrapAnsi(string, 2, {hard: true, trim: false, wordWrap: false});
var lines = wrapped.split("\n");
console.log(lines[0])
console.log(lines)

You can see that the background is properly ended for each line, this is the behavior I expect when I try background and foreground:

[ '\u001b[42mte\u001b[49m', '\u001b[42mst\u001b[49m' ]

When you add a foreground however, the background never stops.

var string = chalk.bgGreen.black('test');
var wrapped = wrapAnsi(string, 2, {hard: true, trim: false, wordWrap: false});
var lines = wrapped.split("\n");
console.log(lines[0])
console.log(lines)

Result:

image

[
  '\u001b[42m\u001b[30mte\u001b[39m',
  '\u001b[30mst\u001b[39m\u001b[49m'
]

You can see that the inner ansi style is applied and ended per line, but that the outer style is applied only once. I would expect the behavior to be consistent with the previous example.

Thanks!

My workaround for now is to just add the reset code \u001b[0m to the end of the line.