chalk / wrap-ansi

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

Inconsistant styling for strings with background and foreground. #43

Open Brittany-Reid opened 4 years ago

Brittany-Reid commented 4 years ago

Something similar to slice-ansi: https://github.com/chalk/slice-ansi/issues/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!

Brittany-Reid commented 4 years ago

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