1
1
// Copyright (c) Microsoft Corporation. All rights reserved.
2
2
// Licensed under the MIT license. See LICENSE file in the project root for full license information.
3
3
4
- using System . Globalization ;
4
+ using Microsoft . DotNet . Cli . Commands . Test . Terminal ;
5
5
6
- namespace Microsoft . DotNet . Cli . Commands . Test . Terminal ;
6
+ namespace Microsoft . Testing . Platform . OutputDevice . Terminal ;
7
7
8
8
/// <summary>
9
9
/// Non-ANSI terminal that writes text using the standard Console.Foreground color capabilities to stay compatible with
10
10
/// standard Windows command line, and other command lines that are not capable of ANSI, or when output is redirected.
11
11
/// </summary>
12
- internal sealed class NonAnsiTerminal : ITerminal
12
+ internal sealed class NonAnsiTerminal : SimpleTerminal
13
13
{
14
- private readonly IConsole _console ;
15
14
private readonly ConsoleColor _defaultForegroundColor ;
16
- private readonly StringBuilder _stringBuilder = new ( ) ;
17
- private bool _isBatching ;
15
+ private bool ? _colorNotSupported ;
18
16
19
17
public NonAnsiTerminal ( IConsole console )
20
- {
21
- _console = console ;
22
- _defaultForegroundColor = _console . GetForegroundColor ( ) ;
23
- }
24
-
25
- public int Width => _console . IsOutputRedirected ? int . MaxValue : _console . BufferWidth ;
18
+ : base ( console )
19
+ => _defaultForegroundColor = IsForegroundColorNotSupported ( ) ? ConsoleColor . Black : console . GetForegroundColor ( ) ;
26
20
27
- public int Height => _console . IsOutputRedirected ? int . MaxValue : _console . BufferHeight ;
28
-
29
- public void Append ( char value )
21
+ public override void SetColor ( TerminalColor color )
30
22
{
31
- if ( _isBatching )
32
- {
33
- _stringBuilder . Append ( value ) ;
34
- }
35
- else
23
+ if ( IsForegroundColorNotSupported ( ) )
36
24
{
37
- _console . Write ( value ) ;
25
+ return ;
38
26
}
39
- }
40
27
41
- public void Append ( string value )
42
- {
43
- if ( _isBatching )
44
- {
45
- _stringBuilder . Append ( value ) ;
46
- }
47
- else
48
- {
49
- _console . Write ( value ) ;
50
- }
51
- }
52
-
53
- public void AppendLine ( )
54
- {
55
- if ( _isBatching )
56
- {
57
- _stringBuilder . AppendLine ( ) ;
58
- }
59
- else
60
- {
61
- _console . WriteLine ( ) ;
62
- }
28
+ Console . SetForegroundColor ( ToConsoleColor ( color ) ) ;
63
29
}
64
30
65
- public void AppendLine ( string value )
31
+ public override void ResetColor ( )
66
32
{
67
- if ( _isBatching )
68
- {
69
- _stringBuilder . AppendLine ( value ) ;
70
- }
71
- else
33
+ if ( IsForegroundColorNotSupported ( ) )
72
34
{
73
- _console . WriteLine ( value ) ;
35
+ return ;
74
36
}
75
- }
76
-
77
- public void AppendLink ( string path , int ? lineNumber )
78
- {
79
- Append ( path ) ;
80
- if ( lineNumber . HasValue )
81
- {
82
- Append ( $ ":{ lineNumber } ") ;
83
- }
84
- }
85
-
86
- public void SetColor ( TerminalColor color )
87
- {
88
- if ( _isBatching )
89
- {
90
- _console . Write ( _stringBuilder . ToString ( ) ) ;
91
- _stringBuilder . Clear ( ) ;
92
- }
93
-
94
- _console . SetForegroundColor ( ToConsoleColor ( color ) ) ;
95
- }
96
-
97
- public void ResetColor ( )
98
- {
99
- if ( _isBatching )
100
- {
101
- _console . Write ( _stringBuilder . ToString ( ) ) ;
102
- _stringBuilder . Clear ( ) ;
103
- }
104
-
105
- _console . SetForegroundColor ( _defaultForegroundColor ) ;
106
- }
107
-
108
- public void ShowCursor ( )
109
- {
110
- // nop
111
- }
112
37
113
- public void HideCursor ( )
114
- {
115
- // nop
38
+ Console . SetForegroundColor ( _defaultForegroundColor ) ;
116
39
}
117
40
118
- public void StartUpdate ( )
41
+ private bool IsForegroundColorNotSupported ( )
119
42
{
120
- if ( _isBatching )
121
- {
122
- throw new InvalidOperationException ( CliCommandStrings . ConsoleIsAlreadyInBatchingMode ) ;
123
- }
43
+ _colorNotSupported ??= RuntimeInformation . IsOSPlatform ( OSPlatform . Create ( "ANDROID" ) ) ||
44
+ RuntimeInformation . IsOSPlatform ( OSPlatform . Create ( "IOS" ) ) ||
45
+ RuntimeInformation . IsOSPlatform ( OSPlatform . Create ( "TVOS" ) ) ||
46
+ RuntimeInformation . IsOSPlatform ( OSPlatform . Create ( "WASI" ) ) ||
47
+ RuntimeInformation . IsOSPlatform ( OSPlatform . Create ( "BROWSER" ) ) ;
124
48
125
- _stringBuilder . Clear ( ) ;
126
- _isBatching = true ;
127
- }
128
-
129
- public void StopUpdate ( )
130
- {
131
- _console . Write ( _stringBuilder . ToString ( ) ) ;
132
- _isBatching = false ;
49
+ return _colorNotSupported . Value ;
133
50
}
134
51
135
52
private ConsoleColor ToConsoleColor ( TerminalColor color ) => color switch
@@ -153,111 +70,4 @@ public void StopUpdate()
153
70
TerminalColor . White => ConsoleColor . White ,
154
71
_ => _defaultForegroundColor ,
155
72
} ;
156
-
157
- public void EraseProgress ( )
158
- {
159
- // nop
160
- }
161
-
162
- public void RenderProgress ( TestProgressState ? [ ] progress )
163
- {
164
- int count = 0 ;
165
- foreach ( TestProgressState ? p in progress )
166
- {
167
- if ( p == null )
168
- {
169
- continue ;
170
- }
171
-
172
- count ++ ;
173
-
174
- string durationString = HumanReadableDurationFormatter . Render ( p . Stopwatch . Elapsed ) ;
175
-
176
- int passed = p . PassedTests ;
177
- int failed = p . FailedTests ;
178
- int skipped = p . SkippedTests ;
179
- int retried = p . RetriedFailedTests ;
180
-
181
- // Use just ascii here, so we don't put too many restrictions on fonts needing to
182
- // properly show unicode, or logs being saved in particular encoding.
183
- Append ( '[' ) ;
184
- SetColor ( TerminalColor . DarkGreen ) ;
185
- Append ( '+' ) ;
186
- Append ( passed . ToString ( CultureInfo . CurrentCulture ) ) ;
187
- ResetColor ( ) ;
188
-
189
- Append ( '/' ) ;
190
-
191
- SetColor ( TerminalColor . DarkRed ) ;
192
- Append ( 'x' ) ;
193
- Append ( failed . ToString ( CultureInfo . CurrentCulture ) ) ;
194
- ResetColor ( ) ;
195
-
196
- Append ( '/' ) ;
197
-
198
- SetColor ( TerminalColor . DarkYellow ) ;
199
- Append ( '?' ) ;
200
- Append ( skipped . ToString ( CultureInfo . CurrentCulture ) ) ;
201
- ResetColor ( ) ;
202
-
203
- if ( retried > 0 )
204
- {
205
- Append ( '/' ) ;
206
- SetColor ( TerminalColor . Gray ) ;
207
- Append ( 'r' ) ;
208
- Append ( retried . ToString ( CultureInfo . CurrentCulture ) ) ;
209
- ResetColor ( ) ;
210
- }
211
-
212
- Append ( ']' ) ;
213
-
214
- Append ( ' ' ) ;
215
- Append ( p . AssemblyName ) ;
216
-
217
- if ( p . TargetFramework != null || p . Architecture != null )
218
- {
219
- Append ( " (" ) ;
220
- if ( p . TargetFramework != null )
221
- {
222
- Append ( p . TargetFramework ) ;
223
- Append ( '|' ) ;
224
- }
225
-
226
- if ( p . Architecture != null )
227
- {
228
- Append ( p . Architecture ) ;
229
- }
230
-
231
- Append ( ')' ) ;
232
- }
233
-
234
- TestDetailState ? activeTest = p . TestNodeResultsState ? . GetRunningTasks ( 1 ) . FirstOrDefault ( ) ;
235
- if ( ! string . IsNullOrWhiteSpace ( activeTest ? . Text ) )
236
- {
237
- Append ( " - " ) ;
238
- Append ( activeTest . Text ) ;
239
- Append ( ' ' ) ;
240
- }
241
-
242
- Append ( durationString ) ;
243
-
244
- AppendLine ( ) ;
245
- }
246
-
247
- // Do not render empty lines when there is nothing to show.
248
- if ( count > 0 )
249
- {
250
- AppendLine ( ) ;
251
- }
252
- }
253
-
254
- public void StartBusyIndicator ( )
255
- {
256
- // nop
257
- }
258
-
259
- public void StopBusyIndicator ( )
260
- {
261
- // nop
262
- }
263
73
}
0 commit comments