I am working with control sequences for text formatting and have stumbled upon some unexpected behavior. I have some text to output, in the middle of this text there is some smaller text block highlighted with a colored background. The original text may be pretty long, it may take a few lines, so the colored text block in the middle may appear on the multiple lines - starts on the one line and finishes on the other. Everything seems to work fine until it reaches the bottom of the terminal window, a lot of whitespace becomes colored:
Here is a script to reproduce:
# color.sh echo -e 'default \x1B[43m color \n color \x1B[49m default';
As you can see, I've added a newline character
\n in the text block, just to reproduce, but it my situation, when a text is pretty long and it does not fit into a single line, colored block gets split into multiple line, and as a result I have the same colored whitespace after the text.
# color-long.sh echo -e 'default default default default default default default default default \x1B[43m color color \x1B[49m default';
I get this on Ubuntu 14.04, but was able to reproduce the same behavior on Yosemite 10.10.
I wonder what is the reason of this behavior and how it can be fixed without using different utilities for output (instead of
echo), but maybe by using the same control sequences. I have control over the text, but not over the output process.
I've already tried to wrap sequences like
\001\x1B[43m\002, but it does not give any result, just adds extra
 to the text or outputs some unrecognized symbols.