fix: serverReadyAction with ANSI colors (#170315)

Fixes #159391
This commit is contained in:
Connor Peet 2022-12-30 15:24:02 -08:00 committed by GitHub
parent 267b98d271
commit 00b145c091
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23

View file

@ -21,6 +21,26 @@ 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
/**
* Froms vs/base/common/strings.ts in core
* @see https://github.com/microsoft/vscode/blob/22a2a0e833175c32a2005b977d7fbd355582e416/src/vs/base/common/strings.ts#L736
*/
function removeAnsiEscapeCodes(str: string): string {
if (str) {
str = str.replace(EL, '');
str = str.replace(COLOR_START, '');
str = str.replace(COLOR_END, '');
}
return str;
}
class Trigger {
private _fired = false;
@ -77,16 +97,17 @@ class ServerReadyDetector extends vscode.Disposable {
// first find the detector with a matching pid
const pid = await e.terminal.processId;
const str = removeAnsiEscapeCodes(e.data);
for (const [, detector] of this.detectors) {
if (detector.shellPid === pid) {
detector.detectPattern(e.data);
detector.detectPattern(str);
return;
}
}
// if none found, try all detectors until one matches
for (const [, detector] of this.detectors) {
if (detector.detectPattern(e.data)) {
if (detector.detectPattern(str)) {
return;
}
}