forked from microsoft/rushstack
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathStringBufferTerminalProvider.ts
More file actions
103 lines (88 loc) · 2.76 KB
/
StringBufferTerminalProvider.ts
File metadata and controls
103 lines (88 loc) · 2.76 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
// Copyright (c) Microsoft Corporation. All rights reserved. Licensed under the MIT license.
// See LICENSE in the project root for license information.
import { ITerminalProvider, TerminalProviderSeverity } from './ITerminalProvider';
import { StringBuilder } from '../StringBuilder';
import { Text } from '../Text';
/**
* Terminal provider that stores written data in buffers separated by severity.
* This terminal provider is designed to be used when code that prints to a terminal
* is being unit tested.
*
* @beta
*/
export class StringBufferTerminalProvider implements ITerminalProvider {
private _standardBuffer: StringBuilder = new StringBuilder();
private _verboseBuffer: StringBuilder = new StringBuilder();
private _warningBuffer: StringBuilder = new StringBuilder();
private _errorBuffer: StringBuilder = new StringBuilder();
private _supportsColor: boolean;
public constructor(supportsColor: boolean = false) {
this._supportsColor = supportsColor;
}
/**
* {@inheritDoc ITerminalProvider.write}
*/
public write(data: string, severity: TerminalProviderSeverity): void {
switch (severity) {
case TerminalProviderSeverity.warning: {
this._warningBuffer.append(data);
break;
}
case TerminalProviderSeverity.error: {
this._errorBuffer.append(data);
break;
}
case TerminalProviderSeverity.verbose: {
this._verboseBuffer.append(data);
break;
}
case TerminalProviderSeverity.log:
default: {
this._standardBuffer.append(data);
break;
}
}
}
/**
* {@inheritDoc ITerminalProvider.eolCharacter}
*/
public get eolCharacter(): string {
return '[n]';
}
/**
* {@inheritDoc ITerminalProvider.supportsColor}
*/
public get supportsColor(): boolean {
return this._supportsColor;
}
/**
* Get everything that has been written at log-level severity.
*/
public getOutput(): string {
return this._normalizeOutput(this._standardBuffer.toString());
}
/**
* Get everything that has been written at verbose-level severity.
*/
public getVerbose(): string {
return this._normalizeOutput(this._verboseBuffer.toString());
}
/**
* Get everything that has been written at error-level severity.
*/
public getErrorOutput(): string {
return this._normalizeOutput(this._errorBuffer.toString());
}
/**
* Get everything that has been written at warning-level severity.
*/
public getWarningOutput(): string {
return this._normalizeOutput(this._warningBuffer.toString());
}
private _normalizeOutput(s: string): string { // tslint:disable-line:export-name
return Text.convertToLf(s)
.replace(/\u001b/g, '[x]')
.replace(/\n/g, '[-n-]')
.replace(/\r/g, '[-r-]');
}
}