Skip to content

Commit

Permalink
Merge pull request #55 from benpollarduk/no_color_env_support
Browse files Browse the repository at this point in the history
Added NO_COLOR support
  • Loading branch information
benpollarduk authored Jan 20, 2024
2 parents 67317f8 + 475568d commit e1d2021
Show file tree
Hide file tree
Showing 3 changed files with 107 additions and 3 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1,68 @@
using System;
using BP.AdventureFramework.Rendering.Frames;
using Microsoft.VisualStudio.TestTools.UnitTesting;

namespace BP.AdventureFramework.Tests.Rendering.Frames
{
[TestClass]
public class GridTextFrame_Tests
{
[TestMethod]
public void GivenUnmodifiedEnvironment_WhenIsColorSupressed_ThenReturnFalse()
{
var result = GridTextFrame.IsColorSupressed();

Assert.IsFalse(result);
}

[TestMethod]
public void GivenNoColorEnvironmentVariableSetToEmptyString_WhenIsColorSupressed_ThenReturnFalse()
{
Environment.SetEnvironmentVariable(GridTextFrame.NO_COLOR, "");

var result = GridTextFrame.IsColorSupressed();

Assert.IsFalse(result);
}

[TestMethod]
public void GivenNoColorEnvironmentVariableSetTo0_WhenIsColorSupressed_ThenReturnFalse()
{
Environment.SetEnvironmentVariable(GridTextFrame.NO_COLOR, "0");

var result = GridTextFrame.IsColorSupressed();

Assert.IsFalse(result);
}

[TestMethod]
public void GivenNoColorEnvironmentVariableSetToFalse_WhenIsColorSupressed_ThenReturnFalse()
{
Environment.SetEnvironmentVariable(GridTextFrame.NO_COLOR, "False");

var result = GridTextFrame.IsColorSupressed();

Assert.IsFalse(result);
}

[TestMethod]
public void GivenNoColorEnvironmentVariableSetTo1_WhenIsColorSupressed_ThenReturnTrue()
{
Environment.SetEnvironmentVariable(GridTextFrame.NO_COLOR, "1");

var result = GridTextFrame.IsColorSupressed();

Assert.IsTrue(result);
}

[TestMethod]
public void GivenNoColorEnvironmentVariableSetToTrue_WhenIsColorSupressed_ThenReturnTrue()
{
Environment.SetEnvironmentVariable(GridTextFrame.NO_COLOR, "True");

var result = GridTextFrame.IsColorSupressed();

Assert.IsTrue(result);
}
}
}
40 changes: 38 additions & 2 deletions BP.AdventureFramework/Rendering/Frames/GridTextFrame.cs
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,15 @@ namespace BP.AdventureFramework.Rendering.Frames
/// </summary>
public sealed class GridTextFrame : IFrame
{
#region Constants

/// <summary>
/// Get the value for the NO_COLOR environment variable.
/// </summary>
internal const string NO_COLOR = "NO_COLOR";

#endregion

#region Fields

private readonly GridStringBuilder builder;
Expand Down Expand Up @@ -47,6 +56,29 @@ public GridTextFrame(GridStringBuilder builder, int cursorLeft, int cursorTop, R

#endregion

#region StaticMethods

/// <summary>
/// Determine if color is suppressed. If the NO_COLOR environment variable is present and set to anything other than '0' or 'false' this will return true.
/// </summary>
/// <returns>True if the NO_COLOR ebviroment variable is present and set to anything other than '0' or 'false', else false.</returns>
internal static bool IsColorSupressed()
{
var value = Environment.GetEnvironmentVariable(NO_COLOR)?.ToLower() ?? string.Empty;

switch (value)
{
case "":
case "0":
case "false":
return false;
default:
return true;
}
}

#endregion

#region Overrides of Object

/// <summary>
Expand Down Expand Up @@ -100,10 +132,12 @@ public override string ToString()
/// <param name="writer">The writer.</param>
public void Render(TextWriter writer)
{
var renderInColor = !IsColorSupressed();
var cursorVisible = Console.CursorVisible;
var startColor = Console.ForegroundColor;

Console.BackgroundColor = BackgroundColor.ToConsoleColor();
if (renderInColor)
Console.BackgroundColor = BackgroundColor.ToConsoleColor();

Console.CursorVisible = false;

Expand All @@ -115,7 +149,9 @@ public void Render(TextWriter writer)

if (c != 0)
{
Console.ForegroundColor = builder.GetCellColor(x, y).ToConsoleColor();
if (renderInColor)
Console.ForegroundColor = builder.GetCellColor(x, y).ToConsoleColor();

writer.Write(c);
}
else
Expand Down
2 changes: 1 addition & 1 deletion BP.AdventureFramework/Rendering/Frames/TextFrame.cs
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,7 @@ public TextFrame(string frameData, int cursorLeft, int cursorTop)
/// <returns>A string that represents the current object.</returns>
public override string ToString()
{
return this.frameData;
return frameData;
}

#endregion
Expand Down

0 comments on commit e1d2021

Please sign in to comment.