Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

CLI Utils: file-scoped namespaces and house-keeping #47430

Merged
merged 6 commits into from
Mar 11, 2025
Merged
Show file tree
Hide file tree
Changes from 5 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 4 additions & 0 deletions exclusion.dic
Original file line number Diff line number Diff line change
@@ -1,9 +1,12 @@
ansi
awaiter
commandline
crossgen
csharp
cshtml
deserializer
msbuild
muxer
nuget
nupkg
nuspec
Expand All @@ -14,6 +17,7 @@ tfm
tfms
url
urls
uuid
xamarin
xunit
yaml
239 changes: 119 additions & 120 deletions src/Cli/Microsoft.DotNet.Cli.Utils/AnsiConsole.cs
Original file line number Diff line number Diff line change
@@ -1,156 +1,155 @@
// Licensed to the .NET Foundation under one or more agreements.
// The .NET Foundation licenses this file to you under the MIT license.

namespace Microsoft.DotNet.Cli.Utils
namespace Microsoft.DotNet.Cli.Utils;

public class AnsiConsole
{
public class AnsiConsole
{
private const int Light = 0x08;
private readonly bool _ansiEnabled;
private const int Light = 0x08;
private readonly bool _ansiEnabled;

private AnsiConsole(TextWriter writer)
{
Writer = writer;
private AnsiConsole(TextWriter writer)
{
Writer = writer;

OriginalForegroundColor = Console.ForegroundColor;
_boldRecursion = ((int)OriginalForegroundColor & Light) != 0 ? 1 : 0;
OriginalForegroundColor = Console.ForegroundColor;
_boldRecursion = ((int)OriginalForegroundColor & Light) != 0 ? 1 : 0;

_ansiEnabled = string.IsNullOrWhiteSpace(Environment.GetEnvironmentVariable("NO_COLOR"));
}
_ansiEnabled = string.IsNullOrWhiteSpace(Environment.GetEnvironmentVariable("NO_COLOR"));
}

private int _boldRecursion;
private int _boldRecursion;

public static AnsiConsole GetOutput()
{
return new AnsiConsole(Console.Out);
}
public static AnsiConsole GetOutput()
{
return new AnsiConsole(Console.Out);
}

public static AnsiConsole GetError()
{
return new AnsiConsole(Console.Error);
}
public static AnsiConsole GetError()
{
return new AnsiConsole(Console.Error);
}

public TextWriter Writer { get; }
public TextWriter Writer { get; }

public ConsoleColor OriginalForegroundColor { get; }
public ConsoleColor OriginalForegroundColor { get; }

private void SetColor(ConsoleColor color)
private void SetColor(ConsoleColor color)
{
if (!_ansiEnabled)
{
if (!_ansiEnabled)
{
return;
}

int c = (int)color;

Console.ForegroundColor =
c < 0 ? color : // unknown, just use it
_boldRecursion > 0 ? (ConsoleColor)(c | Light) : // ensure color is light
(ConsoleColor)(c & ~Light); // ensure color is dark
return;
}

private void SetBold(bool bold)
{
if (!_ansiEnabled)
{
return;
}
int c = (int)color;

_boldRecursion += bold ? 1 : -1;
if (_boldRecursion > 1 || (_boldRecursion == 1 && !bold))
{
return;
}
Console.ForegroundColor =
c < 0 ? color : // unknown, just use it
_boldRecursion > 0 ? (ConsoleColor)(c | Light) : // ensure color is light
(ConsoleColor)(c & ~Light); // ensure color is dark
}

// switches on _boldRecursion to handle boldness
SetColor(Console.ForegroundColor);
private void SetBold(bool bold)
{
if (!_ansiEnabled)
{
return;
}

public void WriteLine(string message)
_boldRecursion += bold ? 1 : -1;
if (_boldRecursion > 1 || (_boldRecursion == 1 && !bold))
{
Write(message);
Writer.WriteLine();
return;
}

// switches on _boldRecursion to handle boldness
SetColor(Console.ForegroundColor);
}

public void Write(string message)
public void WriteLine(string message)
{
Write(message);
Writer.WriteLine();
}


public void Write(string message)
{
var escapeScan = 0;
for (; ; )
{
var escapeScan = 0;
for (; ; )
var escapeIndex = message.IndexOf("\x1b[", escapeScan, StringComparison.Ordinal);
if (escapeIndex == -1)
{
var escapeIndex = message.IndexOf("\x1b[", escapeScan, StringComparison.Ordinal);
if (escapeIndex == -1)
var text = message.Substring(escapeScan);
Writer.Write(text);
break;
}
else
{
var startIndex = escapeIndex + 2;
var endIndex = startIndex;
while (endIndex != message.Length &&
message[endIndex] >= 0x20 &&
message[endIndex] <= 0x3f)
{
var text = message.Substring(escapeScan);
Writer.Write(text);
break;
endIndex += 1;
}
else

var text = message.Substring(escapeScan, escapeIndex - escapeScan);
Writer.Write(text);
if (endIndex == message.Length)
{
var startIndex = escapeIndex + 2;
var endIndex = startIndex;
while (endIndex != message.Length &&
message[endIndex] >= 0x20 &&
message[endIndex] <= 0x3f)
{
endIndex += 1;
}

var text = message.Substring(escapeScan, escapeIndex - escapeScan);
Writer.Write(text);
if (endIndex == message.Length)
{
break;
}
break;
}

switch (message[endIndex])
{
case 'm':
int value;
if (int.TryParse(message.Substring(startIndex, endIndex - startIndex), out value))
switch (message[endIndex])
{
case 'm':
int value;
if (int.TryParse(message.Substring(startIndex, endIndex - startIndex), out value))
{
switch (value)
{
switch (value)
{
case 1:
SetBold(true);
break;
case 22:
SetBold(false);
break;
case 30:
SetColor(ConsoleColor.Black);
break;
case 31:
SetColor(ConsoleColor.Red);
break;
case 32:
SetColor(ConsoleColor.Green);
break;
case 33:
SetColor(ConsoleColor.Yellow);
break;
case 34:
SetColor(ConsoleColor.Blue);
break;
case 35:
SetColor(ConsoleColor.Magenta);
break;
case 36:
SetColor(ConsoleColor.Cyan);
break;
case 37:
SetColor(ConsoleColor.Gray);
break;
case 39:
Console.ForegroundColor = OriginalForegroundColor;
break;
}
case 1:
SetBold(true);
break;
case 22:
SetBold(false);
break;
case 30:
SetColor(ConsoleColor.Black);
break;
case 31:
SetColor(ConsoleColor.Red);
break;
case 32:
SetColor(ConsoleColor.Green);
break;
case 33:
SetColor(ConsoleColor.Yellow);
break;
case 34:
SetColor(ConsoleColor.Blue);
break;
case 35:
SetColor(ConsoleColor.Magenta);
break;
case 36:
SetColor(ConsoleColor.Cyan);
break;
case 37:
SetColor(ConsoleColor.Gray);
break;
case 39:
Console.ForegroundColor = OriginalForegroundColor;
break;
}
break;
}

escapeScan = endIndex + 1;
}
break;
}

escapeScan = endIndex + 1;
}
}
}
Expand Down
19 changes: 6 additions & 13 deletions src/Cli/Microsoft.DotNet.Cli.Utils/ArgumentEscaper.cs
Original file line number Diff line number Diff line change
Expand Up @@ -179,21 +179,14 @@ private static string EscapeArgForCmd(string argument)
return sb.ToString();
}

internal static bool ShouldSurroundWithQuotes(string argument)
{
internal static bool ShouldSurroundWithQuotes(string argument) =>
// Only quote if whitespace exists in the string
return ArgumentContainsWhitespace(argument);
}
ArgumentContainsWhitespace(argument);

internal static bool IsSurroundedWithQuotes(string argument)
{
return argument.StartsWith("\"", StringComparison.Ordinal) &&
argument.EndsWith("\"", StringComparison.Ordinal);
}
internal static bool IsSurroundedWithQuotes(string argument) =>
argument.StartsWith("\"", StringComparison.Ordinal) && argument.EndsWith("\"", StringComparison.Ordinal);

internal static bool ArgumentContainsWhitespace(string argument)
{
return argument.Contains(" ") || argument.Contains("\t") || argument.Contains("\n");
}
internal static bool ArgumentContainsWhitespace(string argument) =>
argument.Contains(" ") || argument.Contains("\t") || argument.Contains("\n");
}
}
2 changes: 1 addition & 1 deletion src/Cli/Microsoft.DotNet.Cli.Utils/BlockingMemoryStream.cs
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ namespace Microsoft.DotNet.Cli.Utils
/// </summary>
public sealed class BlockingMemoryStream : Stream
{
private readonly BlockingCollection<byte[]> _buffers = new();
private readonly BlockingCollection<byte[]> _buffers = [];
private ArraySegment<byte> _remaining;

public override void Write(byte[] buffer, int offset, int count)
Expand Down
Loading
Loading