Fix terminal string chunker to ignore non-printable color seqs

Reviewed By: rafeca

Differential Revision: D6405127

fbshipit-source-id: 94fd12a04db43a790af34fdee36be9d7f8ecf59d
This commit is contained in:
Peter van der Zee 2017-11-23 09:55:28 -08:00 committed by Facebook Github Bot
parent b7ba5c7ee1
commit ccd8eae0b0
1 changed files with 7 additions and 1 deletions

View File

@ -39,9 +39,15 @@ function clearStringBackwards(stream: tty.WriteStream, str: string): void {
* Cut a string into an array of string of the specific maximum size. A newline
* ends a chunk immediately (it's not included in the "." RexExp operator), and
* is not included in the result.
* When counting we should ignore non-printable characters. In particular the
* ANSI escape sequences (regex: /\x1B\[([0-9]{1,2}(;[0-9]{1,2})?)?m/)
* (Not an exhaustive match, intended to match ANSI color escapes)
* https://en.wikipedia.org/wiki/ANSI_escape_code
*/
function chunkString(str: string, size: number): Array<string> {
return str.match(new RegExp(`.{1,${size}}`, 'g')) || [];
const ANSI_COLOR = '\x1B\\[([0-9]{1,2}(;[0-9]{1,2})?)?m';
const SKIP_ANSI = `(?:${ANSI_COLOR})*`;
return str.match(new RegExp(`(?:${SKIP_ANSI}.){1,${size}}`, 'g')) || [];
}
/**