Skip to content

Commit

Permalink
Remove ansi escape characters (#1789)
Browse files Browse the repository at this point in the history
  • Loading branch information
ravipal authored Mar 25, 2020
1 parent 2cfbf6a commit 2ad26b9
Showing 1 changed file with 12 additions and 0 deletions.
12 changes: 12 additions & 0 deletions src/utils/azureUtils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -152,6 +152,7 @@ export async function streamLogs(node: AzureRegistryTreeItem, run: AcrModels.Run
if (available > start) {
text = await getBlobToText(blobInfo, blob, start);
let utf8encoded = (new Buffer(text, 'ascii')).toString('utf8');
utf8encoded = removeAnsiEscapeSequences(utf8encoded);
start += text.length;
ext.outputChannel.append(utf8encoded);
}
Expand All @@ -163,6 +164,17 @@ export async function streamLogs(node: AzureRegistryTreeItem, run: AcrModels.Run
);
}

function removeAnsiEscapeSequences(text: string): string {
// (^.*\x1B\[\d*;?\d*H) - Esc[Line;ColumnH represents the cursor position. Remove any string before setting the cursore position.
// (\x1B\[[^A-Za-z]*[A-Za-z]) - Remove any control sequence for example "Esc[40m". This is the only regex required for linux container, the others are used by windows container log.
// (\x1B=) - "Esc=" enters the alternate keypad mode. So remove this, such sequences are present in windows container.
// (\x1B]0;.*\x07) - "Esc]0;string/x07" operating system command.
// (\xEF\xBB\xBF) - Removes the Byte Order Mark (BOM) of UTF-8.
// eslint-disable-next-line no-control-regex
const removeAnsiEscapeSequenceRegExp = new RegExp(/^.*\x1b\[\d*;?\d*H|\x1b\[[^A-Za-z]*[A-Za-z]|\x1b=|\x1b]0;.*\x07|\xEF\xBB\xBF/g);
return text.replace(removeAnsiEscapeSequenceRegExp, '');
}

// Promisify getBlobToText for readability and error handling purposes
export async function getBlobToText(blobInfo: IBlobInfo, blob: BlobService, rangeStart: number): Promise<string> {
return new Promise<string>(
Expand Down

0 comments on commit 2ad26b9

Please sign in to comment.