Skip to content

Commit c8cd570

Browse files
authored
CLI Utils: file-scoped namespaces and house-keeping (#47430)
2 parents bac2213 + af3ddd7 commit c8cd570

File tree

93 files changed

+3422
-3536
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

93 files changed

+3422
-3536
lines changed

exclusion.dic

+4
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,12 @@
1+
ansi
12
awaiter
3+
commandline
24
crossgen
35
csharp
46
cshtml
57
deserializer
68
msbuild
9+
muxer
710
nuget
811
nupkg
912
nuspec
@@ -14,6 +17,7 @@ tfm
1417
tfms
1518
url
1619
urls
20+
uuid
1721
xamarin
1822
xunit
1923
yaml

src/Cli/Microsoft.DotNet.Cli.Utils/AnsiConsole.cs

+119-120
Original file line numberDiff line numberDiff line change
@@ -1,156 +1,155 @@
11
// Licensed to the .NET Foundation under one or more agreements.
22
// The .NET Foundation licenses this file to you under the MIT license.
33

4-
namespace Microsoft.DotNet.Cli.Utils
4+
namespace Microsoft.DotNet.Cli.Utils;
5+
6+
public class AnsiConsole
57
{
6-
public class AnsiConsole
7-
{
8-
private const int Light = 0x08;
9-
private readonly bool _ansiEnabled;
8+
private const int Light = 0x08;
9+
private readonly bool _ansiEnabled;
1010

11-
private AnsiConsole(TextWriter writer)
12-
{
13-
Writer = writer;
11+
private AnsiConsole(TextWriter writer)
12+
{
13+
Writer = writer;
1414

15-
OriginalForegroundColor = Console.ForegroundColor;
16-
_boldRecursion = ((int)OriginalForegroundColor & Light) != 0 ? 1 : 0;
15+
OriginalForegroundColor = Console.ForegroundColor;
16+
_boldRecursion = ((int)OriginalForegroundColor & Light) != 0 ? 1 : 0;
1717

18-
_ansiEnabled = string.IsNullOrWhiteSpace(Environment.GetEnvironmentVariable("NO_COLOR"));
19-
}
18+
_ansiEnabled = string.IsNullOrWhiteSpace(Environment.GetEnvironmentVariable("NO_COLOR"));
19+
}
2020

21-
private int _boldRecursion;
21+
private int _boldRecursion;
2222

23-
public static AnsiConsole GetOutput()
24-
{
25-
return new AnsiConsole(Console.Out);
26-
}
23+
public static AnsiConsole GetOutput()
24+
{
25+
return new AnsiConsole(Console.Out);
26+
}
2727

28-
public static AnsiConsole GetError()
29-
{
30-
return new AnsiConsole(Console.Error);
31-
}
28+
public static AnsiConsole GetError()
29+
{
30+
return new AnsiConsole(Console.Error);
31+
}
3232

33-
public TextWriter Writer { get; }
33+
public TextWriter Writer { get; }
3434

35-
public ConsoleColor OriginalForegroundColor { get; }
35+
public ConsoleColor OriginalForegroundColor { get; }
3636

37-
private void SetColor(ConsoleColor color)
37+
private void SetColor(ConsoleColor color)
38+
{
39+
if (!_ansiEnabled)
3840
{
39-
if (!_ansiEnabled)
40-
{
41-
return;
42-
}
43-
44-
int c = (int)color;
45-
46-
Console.ForegroundColor =
47-
c < 0 ? color : // unknown, just use it
48-
_boldRecursion > 0 ? (ConsoleColor)(c | Light) : // ensure color is light
49-
(ConsoleColor)(c & ~Light); // ensure color is dark
41+
return;
5042
}
5143

52-
private void SetBold(bool bold)
53-
{
54-
if (!_ansiEnabled)
55-
{
56-
return;
57-
}
44+
int c = (int)color;
5845

59-
_boldRecursion += bold ? 1 : -1;
60-
if (_boldRecursion > 1 || (_boldRecursion == 1 && !bold))
61-
{
62-
return;
63-
}
46+
Console.ForegroundColor =
47+
c < 0 ? color : // unknown, just use it
48+
_boldRecursion > 0 ? (ConsoleColor)(c | Light) : // ensure color is light
49+
(ConsoleColor)(c & ~Light); // ensure color is dark
50+
}
6451

65-
// switches on _boldRecursion to handle boldness
66-
SetColor(Console.ForegroundColor);
52+
private void SetBold(bool bold)
53+
{
54+
if (!_ansiEnabled)
55+
{
56+
return;
6757
}
6858

69-
public void WriteLine(string message)
59+
_boldRecursion += bold ? 1 : -1;
60+
if (_boldRecursion > 1 || (_boldRecursion == 1 && !bold))
7061
{
71-
Write(message);
72-
Writer.WriteLine();
62+
return;
7363
}
7464

65+
// switches on _boldRecursion to handle boldness
66+
SetColor(Console.ForegroundColor);
67+
}
7568

76-
public void Write(string message)
69+
public void WriteLine(string message)
70+
{
71+
Write(message);
72+
Writer.WriteLine();
73+
}
74+
75+
76+
public void Write(string message)
77+
{
78+
var escapeScan = 0;
79+
for (; ; )
7780
{
78-
var escapeScan = 0;
79-
for (; ; )
81+
var escapeIndex = message.IndexOf("\x1b[", escapeScan, StringComparison.Ordinal);
82+
if (escapeIndex == -1)
8083
{
81-
var escapeIndex = message.IndexOf("\x1b[", escapeScan, StringComparison.Ordinal);
82-
if (escapeIndex == -1)
84+
var text = message.Substring(escapeScan);
85+
Writer.Write(text);
86+
break;
87+
}
88+
else
89+
{
90+
var startIndex = escapeIndex + 2;
91+
var endIndex = startIndex;
92+
while (endIndex != message.Length &&
93+
message[endIndex] >= 0x20 &&
94+
message[endIndex] <= 0x3f)
8395
{
84-
var text = message.Substring(escapeScan);
85-
Writer.Write(text);
86-
break;
96+
endIndex += 1;
8797
}
88-
else
98+
99+
var text = message.Substring(escapeScan, escapeIndex - escapeScan);
100+
Writer.Write(text);
101+
if (endIndex == message.Length)
89102
{
90-
var startIndex = escapeIndex + 2;
91-
var endIndex = startIndex;
92-
while (endIndex != message.Length &&
93-
message[endIndex] >= 0x20 &&
94-
message[endIndex] <= 0x3f)
95-
{
96-
endIndex += 1;
97-
}
98-
99-
var text = message.Substring(escapeScan, escapeIndex - escapeScan);
100-
Writer.Write(text);
101-
if (endIndex == message.Length)
102-
{
103-
break;
104-
}
103+
break;
104+
}
105105

106-
switch (message[endIndex])
107-
{
108-
case 'm':
109-
int value;
110-
if (int.TryParse(message.Substring(startIndex, endIndex - startIndex), out value))
106+
switch (message[endIndex])
107+
{
108+
case 'm':
109+
int value;
110+
if (int.TryParse(message.Substring(startIndex, endIndex - startIndex), out value))
111+
{
112+
switch (value)
111113
{
112-
switch (value)
113-
{
114-
case 1:
115-
SetBold(true);
116-
break;
117-
case 22:
118-
SetBold(false);
119-
break;
120-
case 30:
121-
SetColor(ConsoleColor.Black);
122-
break;
123-
case 31:
124-
SetColor(ConsoleColor.Red);
125-
break;
126-
case 32:
127-
SetColor(ConsoleColor.Green);
128-
break;
129-
case 33:
130-
SetColor(ConsoleColor.Yellow);
131-
break;
132-
case 34:
133-
SetColor(ConsoleColor.Blue);
134-
break;
135-
case 35:
136-
SetColor(ConsoleColor.Magenta);
137-
break;
138-
case 36:
139-
SetColor(ConsoleColor.Cyan);
140-
break;
141-
case 37:
142-
SetColor(ConsoleColor.Gray);
143-
break;
144-
case 39:
145-
Console.ForegroundColor = OriginalForegroundColor;
146-
break;
147-
}
114+
case 1:
115+
SetBold(true);
116+
break;
117+
case 22:
118+
SetBold(false);
119+
break;
120+
case 30:
121+
SetColor(ConsoleColor.Black);
122+
break;
123+
case 31:
124+
SetColor(ConsoleColor.Red);
125+
break;
126+
case 32:
127+
SetColor(ConsoleColor.Green);
128+
break;
129+
case 33:
130+
SetColor(ConsoleColor.Yellow);
131+
break;
132+
case 34:
133+
SetColor(ConsoleColor.Blue);
134+
break;
135+
case 35:
136+
SetColor(ConsoleColor.Magenta);
137+
break;
138+
case 36:
139+
SetColor(ConsoleColor.Cyan);
140+
break;
141+
case 37:
142+
SetColor(ConsoleColor.Gray);
143+
break;
144+
case 39:
145+
Console.ForegroundColor = OriginalForegroundColor;
146+
break;
148147
}
149-
break;
150-
}
151-
152-
escapeScan = endIndex + 1;
148+
}
149+
break;
153150
}
151+
152+
escapeScan = endIndex + 1;
154153
}
155154
}
156155
}

src/Cli/Microsoft.DotNet.Cli.Utils/ArgumentEscaper.cs

+6-13
Original file line numberDiff line numberDiff line change
@@ -179,21 +179,14 @@ private static string EscapeArgForCmd(string argument)
179179
return sb.ToString();
180180
}
181181

182-
internal static bool ShouldSurroundWithQuotes(string argument)
183-
{
182+
internal static bool ShouldSurroundWithQuotes(string argument) =>
184183
// Only quote if whitespace exists in the string
185-
return ArgumentContainsWhitespace(argument);
186-
}
184+
ArgumentContainsWhitespace(argument);
187185

188-
internal static bool IsSurroundedWithQuotes(string argument)
189-
{
190-
return argument.StartsWith("\"", StringComparison.Ordinal) &&
191-
argument.EndsWith("\"", StringComparison.Ordinal);
192-
}
186+
internal static bool IsSurroundedWithQuotes(string argument) =>
187+
argument.StartsWith("\"", StringComparison.Ordinal) && argument.EndsWith("\"", StringComparison.Ordinal);
193188

194-
internal static bool ArgumentContainsWhitespace(string argument)
195-
{
196-
return argument.Contains(" ") || argument.Contains("\t") || argument.Contains("\n");
197-
}
189+
internal static bool ArgumentContainsWhitespace(string argument) =>
190+
argument.Contains(" ") || argument.Contains("\t") || argument.Contains("\n");
198191
}
199192
}

src/Cli/Microsoft.DotNet.Cli.Utils/BlockingMemoryStream.cs

+1-1
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@ namespace Microsoft.DotNet.Cli.Utils
1010
/// </summary>
1111
public sealed class BlockingMemoryStream : Stream
1212
{
13-
private readonly BlockingCollection<byte[]> _buffers = new();
13+
private readonly BlockingCollection<byte[]> _buffers = [];
1414
private ArraySegment<byte> _remaining;
1515

1616
public override void Write(byte[] buffer, int offset, int count)

0 commit comments

Comments
 (0)