2020-12-28 23:50:12 +01:00
|
|
|
/**
|
|
|
|
|
* Copyright (c) Microsoft Corporation.
|
|
|
|
|
*
|
|
|
|
|
* Licensed under the Apache License, Version 2.0 (the "License");
|
|
|
|
|
* you may not use this file except in compliance with the License.
|
|
|
|
|
* You may obtain a copy of the License at
|
|
|
|
|
*
|
|
|
|
|
* http://www.apache.org/licenses/LICENSE-2.0
|
|
|
|
|
*
|
|
|
|
|
* Unless required by applicable law or agreed to in writing, software
|
|
|
|
|
* distributed under the License is distributed on an "AS IS" BASIS,
|
|
|
|
|
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
|
|
|
|
* See the License for the specific language governing permissions and
|
|
|
|
|
* limitations under the License.
|
|
|
|
|
*/
|
|
|
|
|
|
|
|
|
|
import * as fs from 'fs';
|
|
|
|
|
import * as querystring from 'querystring';
|
|
|
|
|
import { Writable } from 'stream';
|
2021-01-06 18:31:42 +01:00
|
|
|
import * as hljs from '../../third_party/highlightjs/highlightjs';
|
2021-01-25 04:21:19 +01:00
|
|
|
import { RecorderOutput } from './recorderSupplement';
|
2020-12-28 23:50:12 +01:00
|
|
|
|
2021-01-25 04:21:19 +01:00
|
|
|
export class OutputMultiplexer implements RecorderOutput {
|
|
|
|
|
private _outputs: RecorderOutput[]
|
|
|
|
|
constructor(outputs: RecorderOutput[]) {
|
2020-12-28 23:50:12 +01:00
|
|
|
this._outputs = outputs;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
printLn(text: string) {
|
|
|
|
|
for (const output of this._outputs)
|
|
|
|
|
output.printLn(text);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
popLn(text: string) {
|
|
|
|
|
for (const output of this._outputs)
|
|
|
|
|
output.popLn(text);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
flush() {
|
|
|
|
|
for (const output of this._outputs)
|
|
|
|
|
output.flush();
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2021-01-13 21:52:03 +01:00
|
|
|
export class BufferOutput {
|
|
|
|
|
lines: string[] = [];
|
2020-12-28 23:50:12 +01:00
|
|
|
|
|
|
|
|
printLn(text: string) {
|
2021-01-13 21:52:03 +01:00
|
|
|
this.lines.push(...text.trimEnd().split('\n'));
|
2020-12-28 23:50:12 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
popLn(text: string) {
|
2021-01-13 21:52:03 +01:00
|
|
|
this.lines.length -= text.trimEnd().split('\n').length;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
buffer(): string {
|
|
|
|
|
return this.lines.join('\n');
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2021-01-25 04:21:19 +01:00
|
|
|
export class FileOutput extends BufferOutput implements RecorderOutput {
|
2021-01-13 21:52:03 +01:00
|
|
|
private _fileName: string;
|
|
|
|
|
|
|
|
|
|
constructor(fileName: string) {
|
|
|
|
|
super();
|
|
|
|
|
this._fileName = fileName;
|
2020-12-28 23:50:12 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
flush() {
|
2021-01-13 21:52:03 +01:00
|
|
|
fs.writeFileSync(this._fileName, this.buffer());
|
2020-12-28 23:50:12 +01:00
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2021-01-25 04:21:19 +01:00
|
|
|
export class TerminalOutput implements RecorderOutput {
|
2020-12-28 23:50:12 +01:00
|
|
|
private _output: Writable
|
|
|
|
|
private _language: string;
|
|
|
|
|
|
2021-01-13 21:52:03 +01:00
|
|
|
static create(output: Writable, language: string) {
|
|
|
|
|
if (process.stdout.columns)
|
|
|
|
|
return new TerminalOutput(output, language);
|
|
|
|
|
return new FlushingTerminalOutput(output);
|
|
|
|
|
}
|
|
|
|
|
|
2020-12-28 23:50:12 +01:00
|
|
|
constructor(output: Writable, language: string) {
|
|
|
|
|
this._output = output;
|
|
|
|
|
this._language = language;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
private _highlight(text: string) {
|
2021-01-06 18:31:42 +01:00
|
|
|
let highlightedCode = hljs.highlight(this._language, text).value;
|
2020-12-28 23:50:12 +01:00
|
|
|
highlightedCode = querystring.unescape(highlightedCode);
|
|
|
|
|
highlightedCode = highlightedCode.replace(/<span class="hljs-keyword">/g, '\x1b[38;5;205m');
|
|
|
|
|
highlightedCode = highlightedCode.replace(/<span class="hljs-built_in">/g, '\x1b[38;5;220m');
|
|
|
|
|
highlightedCode = highlightedCode.replace(/<span class="hljs-literal">/g, '\x1b[38;5;159m');
|
|
|
|
|
highlightedCode = highlightedCode.replace(/<span class="hljs-title">/g, '');
|
|
|
|
|
highlightedCode = highlightedCode.replace(/<span class="hljs-number">/g, '\x1b[38;5;78m');
|
|
|
|
|
highlightedCode = highlightedCode.replace(/<span class="hljs-string">/g, '\x1b[38;5;130m');
|
|
|
|
|
highlightedCode = highlightedCode.replace(/<span class="hljs-comment">/g, '\x1b[38;5;23m');
|
|
|
|
|
highlightedCode = highlightedCode.replace(/<span class="hljs-subst">/g, '\x1b[38;5;242m');
|
|
|
|
|
highlightedCode = highlightedCode.replace(/<span class="hljs-function">/g, '');
|
|
|
|
|
highlightedCode = highlightedCode.replace(/<span class="hljs-params">/g, '');
|
|
|
|
|
highlightedCode = highlightedCode.replace(/<span class="hljs-attr">/g, '');
|
|
|
|
|
highlightedCode = highlightedCode.replace(/<\/span>/g, '\x1b[0m');
|
|
|
|
|
highlightedCode = highlightedCode.replace(/'/g, "'");
|
|
|
|
|
highlightedCode = highlightedCode.replace(/"/g, '"');
|
|
|
|
|
highlightedCode = highlightedCode.replace(/>/g, '>');
|
|
|
|
|
highlightedCode = highlightedCode.replace(/</g, '<');
|
|
|
|
|
highlightedCode = highlightedCode.replace(/&/g, '&');
|
|
|
|
|
return highlightedCode;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
printLn(text: string) {
|
|
|
|
|
// Split into lines for highlighter to not fail.
|
|
|
|
|
for (const line of text.split('\n'))
|
|
|
|
|
this._output.write(this._highlight(line) + '\n');
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
popLn(text: string) {
|
|
|
|
|
const terminalWidth = process.stdout.columns || 80;
|
|
|
|
|
for (const line of text.split('\n')) {
|
|
|
|
|
const terminalLines = ((line.length - 1) / terminalWidth | 0) + 1;
|
|
|
|
|
for (let i = 0; i < terminalLines; ++i)
|
|
|
|
|
this._output.write('\u001B[1A\u001B[2K');
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
flush() {}
|
|
|
|
|
}
|
2021-01-13 21:52:03 +01:00
|
|
|
|
2021-01-25 04:21:19 +01:00
|
|
|
export class FlushingTerminalOutput extends BufferOutput implements RecorderOutput {
|
2021-01-13 21:52:03 +01:00
|
|
|
private _output: Writable
|
|
|
|
|
|
|
|
|
|
constructor(output: Writable) {
|
|
|
|
|
super();
|
|
|
|
|
this._output = output;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
printLn(text: string) {
|
|
|
|
|
super.printLn(text);
|
|
|
|
|
this._output.write('-------------8<-------------\n');
|
|
|
|
|
this._output.write(this.buffer() + '\n');
|
|
|
|
|
this._output.write('-------------8<-------------\n');
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
flush() {
|
|
|
|
|
this._output.write(this.buffer() + '\n');
|
|
|
|
|
}
|
|
|
|
|
}
|