Skip to content

Add NO_COLOR support #69

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

Merged
merged 4 commits into from
Aug 23, 2020
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
76 changes: 76 additions & 0 deletions CommandLineParser.Tests/Usage/NoColorOutputTests.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,76 @@
using MatthiWare.CommandLine.Abstractions;
using MatthiWare.CommandLine.Abstractions.Usage;
using MatthiWare.CommandLine.Core.Attributes;
using MatthiWare.CommandLine.Core.Usage;
using Moq;
using System;
using System.Collections.Generic;
using System.Text;
using Xunit;

namespace MatthiWare.CommandLine.Tests.Usage
{
[Collection("Non-Parallel Collection")]
public class NoColorOutputTests
{
private readonly CommandLineParser<Options> parser;
private readonly IEnvironmentVariablesService variablesService;
private Action<ConsoleColor> consoleColorGetter;
private bool variableServiceResult;
private readonly UsagePrinter printer;

public NoColorOutputTests()
{
var envMock = new Mock<IEnvironmentVariablesService>();
envMock.SetupGet(env => env.NoColorRequested).Returns(() => variableServiceResult);

variablesService = envMock.Object;

var usageBuilderMock = new Mock<IUsageBuilder>();
usageBuilderMock.Setup(m => m.AddErrors(It.IsAny<IReadOnlyCollection<Exception>>())).Callback(() =>
{
consoleColorGetter(printer.m_currentConsoleColor);
});

parser = new CommandLineParser<Options>();

printer = new UsagePrinter(parser, usageBuilderMock.Object, variablesService);

parser.Printer = printer;
}

[Fact]
public void CheckUsageOutputRespectsNoColor()
{
ParseAndCheckNoColor(false);
ParseAndCheckNoColor(true);
}

private void ParseAndCheckNoColor(bool noColorOuput)
{
consoleColorGetter = noColorOuput ? (Action<ConsoleColor>)AssertNoColor : AssertColor;

variableServiceResult = noColorOuput;

parser.Parse(new string[] { "alpha" });
}

private void AssertNoColor(ConsoleColor color)
{
Assert.True(variablesService.NoColorRequested);
Assert.NotEqual(ConsoleColor.Red, color);
}

private void AssertColor(ConsoleColor color)
{
Assert.False(variablesService.NoColorRequested);
Assert.Equal(ConsoleColor.Red, color);
}

private class Options
{
[Required, Name("b")]
public bool MyBool { get; set; }
}
}
}
8 changes: 8 additions & 0 deletions CommandLineParser.Tests/XUnitExtensions.cs
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
using System;
using System.Linq.Expressions;
using MatthiWare.CommandLine.Abstractions.Parsing;
using Xunit;

namespace MatthiWare.CommandLine.Tests
{
Expand Down Expand Up @@ -31,4 +32,11 @@ public static bool AssertNoErrors<T>(this IParserResult<T> result, bool shouldTh
return false;
}
}

#pragma warning disable SA1402 // FileMayOnlyContainASingleType
[CollectionDefinition("Non-Parallel Collection", DisableParallelization = true)]
public class NonParallelCollection
{
}
#pragma warning restore SA1402 // FileMayOnlyContainASingleType
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
namespace MatthiWare.CommandLine.Abstractions.Usage
{
/// <summary>
/// Environment variables
/// </summary>
public interface IEnvironmentVariablesService
{
/// <summary>
/// Inidicates if NO_COLOR environment variable has been set
/// https://no-color.org/
/// </summary>
bool NoColorRequested { get; }
}
}
31 changes: 30 additions & 1 deletion CommandLineParser/CommandLineParser.xml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

14 changes: 14 additions & 0 deletions CommandLineParser/Core/Usage/EnvironmentVariableService.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
using MatthiWare.CommandLine.Abstractions.Usage;
using System;

namespace MatthiWare.CommandLine.Core.Usage
{
/// <inheritdoc/>
public class EnvironmentVariableService : IEnvironmentVariablesService
{
private const string NoColorId = "NO_COLOR";

/// <inheritdoc/>
public bool NoColorRequested => Environment.GetEnvironmentVariable(NoColorId) != null;
}
}
39 changes: 33 additions & 6 deletions CommandLineParser/Core/Usage/UsagePrinter.cs
Original file line number Diff line number Diff line change
Expand Up @@ -9,30 +9,57 @@ namespace MatthiWare.CommandLine.Core.Usage
/// <inheritdoc/>
public class UsagePrinter : IUsagePrinter
{
private readonly IEnvironmentVariablesService environmentVariablesService;

protected ICommandLineCommandContainer Container { get; }

/// <inheritdoc/>
public IUsageBuilder Builder { get; }

/// <inheritdoc/>
internal ConsoleColor m_currentConsoleColor = ConsoleColor.Gray;

/// <summary>
/// Creates a new CLI output usage printer
/// </summary>
/// <param name="container"></param>
/// <param name="builder"></param>
public UsagePrinter(ICommandLineCommandContainer container, IUsageBuilder builder)
: this(container, builder, new EnvironmentVariableService())
{ }

/// <summary>
/// Creates a new CLI output usage printer
/// </summary>
/// <param name="container"></param>
/// <param name="builder"></param>
/// <param name="environmentVariablesService"></param>
public UsagePrinter(ICommandLineCommandContainer container, IUsageBuilder builder, IEnvironmentVariablesService environmentVariablesService)
{
Container = container;
Builder = builder;
Container = container ?? throw new ArgumentNullException(nameof(container));
Builder = builder ?? throw new ArgumentNullException(nameof(builder));
this.environmentVariablesService = environmentVariablesService ?? throw new ArgumentNullException(nameof(environmentVariablesService));
}

/// <inheritdoc/>
public virtual void PrintErrors(IReadOnlyCollection<Exception> errors)
{
var previousColor = Console.ForegroundColor;
bool canOutputColor = !this.environmentVariablesService.NoColorRequested;

Console.ForegroundColor = ConsoleColor.Red;
if (canOutputColor)
{
m_currentConsoleColor = ConsoleColor.Red;
Console.ForegroundColor = ConsoleColor.Red;
}

Builder.AddErrors(errors);

Console.Error.WriteLine(Builder.Build());

Console.ForegroundColor = previousColor;
if (canOutputColor)
{
m_currentConsoleColor = ConsoleColor.Gray;
Console.ResetColor();
}

Console.WriteLine();
}
Expand Down