Skip to content
Merged
Show file tree
Hide file tree
Changes from all 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
37 changes: 37 additions & 0 deletions DevProxy.Tests/DevProxyCommandNoColorTests.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
// Licensed to the .NET Foundation under one or more agreements.
// The .NET Foundation licenses this file to you under the MIT license.
// See the LICENSE file in the project root for more information.

using DevProxy.Commands;

using Xunit;

namespace DevProxy.Tests;

public class DevProxyCommandNoColorTests
{
[Theory]
[InlineData(null, false)]
[InlineData("", false)]
[InlineData("1", true)]
[InlineData("true", true)]
public void ShouldDisableColor_RespectsNoColorEnv(string? noColor, bool expected)
{
Assert.Equal(expected, DevProxyCommand.ShouldDisableColor([], noColor, null));
}

[Fact]
public void ShouldDisableColor_NoColorOption_DisablesColor()
{
Assert.True(DevProxyCommand.ShouldDisableColor(["devproxy", "--no-color"], "", null));
}

[Theory]
[InlineData("dumb", true)]
[InlineData("DUMB", true)]
[InlineData("xterm-256color", false)]
public void ShouldDisableColor_RespectsTerm(string term, bool expected)
{
Assert.Equal(expected, DevProxyCommand.ShouldDisableColor([], null, term));
}
}
14 changes: 10 additions & 4 deletions DevProxy/Commands/DevProxyCommand.cs
Original file line number Diff line number Diff line change
Expand Up @@ -231,15 +231,21 @@ public static bool NoColor
return field;
}

var args = Environment.GetCommandLineArgs();
field = args.Contains(NoColorOptionName) ||
Environment.GetEnvironmentVariable("NO_COLOR") is not null ||
string.Equals(Environment.GetEnvironmentVariable("TERM"), "dumb", StringComparison.OrdinalIgnoreCase);
field = ShouldDisableColor(
Environment.GetCommandLineArgs(),
Environment.GetEnvironmentVariable("NO_COLOR"),
Environment.GetEnvironmentVariable("TERM"));
_noColorResolved = true;
return field;
}
}

// Per https://no-color.org, NO_COLOR disables color only when present and not empty.
internal static bool ShouldDisableColor(string[] args, string? noColorEnv, string? termEnv) =>
args.Contains(NoColorOptionName) ||
!string.IsNullOrEmpty(noColorEnv) ||
string.Equals(termEnv, "dumb", StringComparison.OrdinalIgnoreCase);

public DevProxyCommand(
IEnumerable<IPlugin> plugins,
ISet<UrlToWatch> urlsToWatch,
Expand Down
Loading