Skip to content

Commit

Permalink
fix: serverReadyAction with ANSI colors (microsoft#170315)
Browse files Browse the repository at this point in the history
  • Loading branch information
connor4312 authored Dec 30, 2022
1 parent 267b98d commit 00b145c
Showing 1 changed file with 23 additions and 2 deletions.
25 changes: 23 additions & 2 deletions extensions/debug-server-ready/src/extension.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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;

Expand Down Expand Up @@ -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;
}
}
Expand Down

0 comments on commit 00b145c

Please sign in to comment.