Skip to content

Commit 8b168b4

Browse files
CopilotwaldekmastykarzCopilotgarrytrinder
authored
Add NO_COLOR, TERM=dumb, and --no-color support (#1556)
* Initial plan * Add NO_COLOR, TERM=dumb, and --no-color support Respect the NO_COLOR environment variable (https://no-color.org/), TERM=dumb detection, and --no-color CLI flag to disable ANSI color escape codes in output. Changes: - DevProxyCommand: Add NoColor static property and --no-color option - DevProxyConfigOptions: Register --no-color for early parsing - TextWriterExtensions: Skip ANSI codes when NoColor is true - ProxyStateController: Skip Console color APIs when NoColor is true Co-authored-by: waldekmastykarz <11164679+waldekmastykarz@users.noreply.github.com> * Update DevProxy/Logging/TextWriterExtensions.cs Co-authored-by: Copilot <175728472+Copilot@users.noreply.github.com> --------- Co-authored-by: copilot-swe-agent[bot] <198982749+Copilot@users.noreply.github.com> Co-authored-by: waldekmastykarz <11164679+waldekmastykarz@users.noreply.github.com> Co-authored-by: Waldek Mastykarz <waldek@mastykarz.nl> Co-authored-by: Copilot <175728472+Copilot@users.noreply.github.com> Co-authored-by: Garry Trinder <garry@trinder365.co.uk>
1 parent 7bb8491 commit 8b168b4

4 files changed

Lines changed: 51 additions & 4 deletions

File tree

DevProxy/Commands/DevProxyCommand.cs

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -43,6 +43,7 @@ sealed class DevProxyCommand : RootCommand
4343
internal const string OutputOptionName = "--output";
4444
internal const string DetachedOptionName = "--detach";
4545
internal const string InternalDaemonOptionName = "--_internal-daemon";
46+
internal const string NoColorOptionName = "--no-color";
4647

4748
private static readonly string[] globalOptions = ["--version"];
4849
private static readonly string[] helpOptions = ["--help", "-h", "/h", "-?", "/?"];
@@ -56,6 +57,7 @@ sealed class DevProxyCommand : RootCommand
5657
private static bool _isInternalDaemonResolved;
5758
private static bool _stdioLogFilePathResolved;
5859
private static bool _detachedLogFilePathResolved;
60+
private static bool _noColorResolved;
5961

6062
public static bool HasGlobalOptions
6163
{
@@ -214,6 +216,24 @@ public static string DetachedLogFilePath
214216
}
215217
}
216218

219+
public static bool NoColor
220+
{
221+
get
222+
{
223+
if (_noColorResolved)
224+
{
225+
return field;
226+
}
227+
228+
var args = Environment.GetCommandLineArgs();
229+
field = args.Contains(NoColorOptionName) ||
230+
Environment.GetEnvironmentVariable("NO_COLOR") is not null ||
231+
string.Equals(Environment.GetEnvironmentVariable("TERM"), "dumb", StringComparison.OrdinalIgnoreCase);
232+
_noColorResolved = true;
233+
return field;
234+
}
235+
}
236+
217237
public DevProxyCommand(
218238
IEnumerable<IPlugin> plugins,
219239
ISet<UrlToWatch> urlsToWatch,
@@ -522,6 +542,13 @@ private void ConfigureCommand()
522542
Hidden = true
523543
};
524544

545+
var noColorOption = new Option<bool>(NoColorOptionName)
546+
{
547+
Description = "Disable colored output",
548+
Arity = ArgumentArity.Zero,
549+
Recursive = true
550+
};
551+
525552
var options = new List<Option>
526553
{
527554
apiPortOption,
@@ -534,6 +561,7 @@ private void ConfigureCommand()
534561
internalDaemonOption,
535562
ipAddressOption,
536563
logLevelOption,
564+
noColorOption,
537565
noFirstRunOption,
538566
noWatchOption,
539567
outputOption,

DevProxy/Commands/DevProxyConfigOptions.cs

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -145,6 +145,11 @@ public DevProxyConfigOptions()
145145
Arity = ArgumentArity.Zero
146146
};
147147

148+
var noColorOption = new Option<bool>(DevProxyCommand.NoColorOptionName)
149+
{
150+
Arity = ArgumentArity.Zero
151+
};
152+
148153
var options = new List<Option>
149154
{
150155
apiPortOption,
@@ -154,7 +159,8 @@ public DevProxyConfigOptions()
154159
urlsToWatchOption,
155160
logLevelOption,
156161
outputOption,
157-
discoverOption
162+
discoverOption,
163+
noColorOption
158164
};
159165
this.AddOptions(options.OrderByName());
160166

DevProxy/Logging/TextWriterExtensions.cs

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22
// The .NET Foundation licenses this file to you under the MIT license.
33
// See the LICENSE file in the project root for more information.
44

5+
using DevProxy.Commands;
56
using DevProxy.Logging;
67

78
#pragma warning disable IDE0130
@@ -15,13 +16,18 @@ static class TextWriterExtensions
1516

1617
public static void ResetColor(this TextWriter writer)
1718
{
19+
if (Console.IsOutputRedirected || DevProxyCommand.NoColor)
20+
{
21+
return;
22+
}
23+
1824
writer.Write(_defaultForegroundColor);
1925
writer.Write(_defaultBackgroundColor);
2026
}
2127

2228
public static void WriteColoredMessage(this TextWriter textWriter, string message, ConsoleColor? background, ConsoleColor? foreground)
2329
{
24-
if (Console.IsOutputRedirected)
30+
if (Console.IsOutputRedirected || DevProxyCommand.NoColor)
2531
{
2632
textWriter.Write(message);
2733
return;

DevProxy/Proxy/ProxyStateController.cs

Lines changed: 9 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44

55
using DevProxy.Abstractions.Plugins;
66
using DevProxy.Abstractions.Proxy;
7+
using DevProxy.Commands;
78
using Titanium.Web.Proxy;
89

910
namespace DevProxy.Proxy;
@@ -95,9 +96,15 @@ private static void PrintRecordingIndicator(bool isRecording)
9596
{
9697
if (isRecording)
9798
{
98-
Console.ForegroundColor = ConsoleColor.Red;
99+
if (!DevProxyCommand.NoColor)
100+
{
101+
Console.ForegroundColor = ConsoleColor.Red;
102+
}
99103
Console.Error.Write("◉");
100-
Console.ResetColor();
104+
if (!DevProxyCommand.NoColor)
105+
{
106+
Console.ResetColor();
107+
}
101108
Console.Error.WriteLine(" Recording... ");
102109
}
103110
else

0 commit comments

Comments
 (0)