11// Copyright (c) Microsoft Corporation. All rights reserved.
22// Licensed under the MIT license. See LICENSE file in the project root for full license information.
33
4- using System . Globalization ;
4+ using Microsoft . DotNet . Cli . Commands . Test . Terminal ;
55
6- namespace Microsoft . DotNet . Cli . Commands . Test . Terminal ;
6+ namespace Microsoft . Testing . Platform . OutputDevice . Terminal ;
77
88/// <summary>
99/// Non-ANSI terminal that writes text using the standard Console.Foreground color capabilities to stay compatible with
1010/// standard Windows command line, and other command lines that are not capable of ANSI, or when output is redirected.
1111/// </summary>
12- internal sealed class NonAnsiTerminal : ITerminal
12+ internal sealed class NonAnsiTerminal : SimpleTerminal
1313{
14- private readonly IConsole _console ;
1514 private readonly ConsoleColor _defaultForegroundColor ;
16- private readonly StringBuilder _stringBuilder = new ( ) ;
17- private bool _isBatching ;
15+ private bool ? _colorNotSupported ;
1816
1917 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 ( ) ;
2620
27- public int Height => _console . IsOutputRedirected ? int . MaxValue : _console . BufferHeight ;
28-
29- public void Append ( char value )
21+ public override void SetColor ( TerminalColor color )
3022 {
31- if ( _isBatching )
32- {
33- _stringBuilder . Append ( value ) ;
34- }
35- else
23+ if ( IsForegroundColorNotSupported ( ) )
3624 {
37- _console . Write ( value ) ;
25+ return ;
3826 }
39- }
4027
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 ) ) ;
6329 }
6430
65- public void AppendLine ( string value )
31+ public override void ResetColor ( )
6632 {
67- if ( _isBatching )
68- {
69- _stringBuilder . AppendLine ( value ) ;
70- }
71- else
33+ if ( IsForegroundColorNotSupported ( ) )
7234 {
73- _console . WriteLine ( value ) ;
35+ return ;
7436 }
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- }
11237
113- public void HideCursor ( )
114- {
115- // nop
38+ Console . SetForegroundColor ( _defaultForegroundColor ) ;
11639 }
11740
118- public void StartUpdate ( )
41+ private bool IsForegroundColorNotSupported ( )
11942 {
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" ) ) ;
12448
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 ;
13350 }
13451
13552 private ConsoleColor ToConsoleColor ( TerminalColor color ) => color switch
@@ -153,111 +70,4 @@ public void StopUpdate()
15370 TerminalColor . White => ConsoleColor . White ,
15471 _ => _defaultForegroundColor ,
15572 } ;
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- }
26373}
0 commit comments