strings: use more comprehensive ansi escape code regex (#173362)

* strings: use more comprehensive ansi escape code regex

The ansi escape remover only recognized a few codes, and only a
subset of their syntax. Be more comprehensive, and add tests.

Fixes #153648

* address pr comment
This commit is contained in:
Connor Peet
2023-02-03 12:37:20 -08:00
committed by GitHub
parent 06c5eb5ead
commit dc6fd239e6
3 changed files with 128 additions and 16 deletions

View File

@@ -21,11 +21,8 @@ interface ServerReadyAction {
killOnServerStop?: boolean;
}
// Escape codes
// http://en.wikipedia.org/wiki/ANSI_escape_code
const EL = /\x1B\x5B[12]?K/g; // Erase in line
const COLOR_START = /\x1b\[\d+m/g; // Color
const COLOR_END = /\x1b\[0?m/g; // Color
// Escape codes, compiled from https://invisible-island.net/xterm/ctlseqs/ctlseqs.html#h3-Functions-using-CSI-_-ordered-by-the-final-character_s_
const CSI_SEQUENCE = /(:?\x1b\[|\x9B)[=?>!]?[\d;:]*["$#'* ]?[a-zA-Z@^`{}|~]/g;
/**
* Froms vs/base/common/strings.ts in core
@@ -33,9 +30,7 @@ const COLOR_END = /\x1b\[0?m/g; // Color
*/
function removeAnsiEscapeCodes(str: string): string {
if (str) {
str = str.replace(EL, '');
str = str.replace(COLOR_START, '');
str = str.replace(COLOR_END, '');
str = str.replace(CSI_SEQUENCE, '');
}
return str;