Skip to content

Commit

Permalink
Merge pull request #269 from kindermannhubert/color-parsing
Browse files Browse the repository at this point in the history
AnsiColor toStringing + parsing (v4.1.1).
  • Loading branch information
kindermannhubert authored Sep 30, 2023
2 parents 45126a1 + c666ab8 commit 4245c19
Show file tree
Hide file tree
Showing 4 changed files with 79 additions and 8 deletions.
41 changes: 40 additions & 1 deletion src/PrettyPrompt/Highlighting/AnsiColor.cs
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,11 @@

using System;
using System.Collections.Generic;
using System.Globalization;
using System.Linq;
using System.Reflection;
using PrettyPrompt.Consoles;
using static PrettyPrompt.Highlighting.FormattedString.TextElementsEnumerator;

namespace PrettyPrompt.Highlighting;

Expand Down Expand Up @@ -38,7 +42,14 @@ namespace PrettyPrompt.Highlighting;
public static readonly AnsiColor BrightCyan = new("96", "106", nameof(BrightCyan));
public static readonly AnsiColor BrightWhite = new("97", "107", nameof(BrightWhite));

public static AnsiColor Rgb(byte r, byte g, byte b) => new($"38;2;{r};{g};{b}", $"48;2;{r};{g};{b}", "RGB color");
private static readonly Dictionary<string, AnsiColor> ansiColorNames =
typeof(AnsiColor)
.GetFields(BindingFlags.Static | BindingFlags.Public)
.Where(f => f.FieldType == typeof(AnsiColor))
.ToDictionary(f => f.Name, f => (AnsiColor)f.GetValue(null)!, StringComparer.OrdinalIgnoreCase);

public static AnsiColor Rgb(byte r, byte g, byte b)
=> new($"38;2;{r};{g};{b}", $"48;2;{r};{g};{b}", $"#{r:X2}{g:X2}{b:X2}");

public AnsiColor(string foregroundCode, string backgroundCode, string friendlyName)
{
Expand All @@ -59,6 +70,34 @@ public AnsiColor(string foregroundCode, string backgroundCode, string friendlyNa

public override string ToString() => friendlyName;

public static bool TryParse(string input, out AnsiColor result)
{
if (PromptConfiguration.HasUserOptedOutFromColor)
{
result = White;
return true;
}

var span = input.AsSpan();
if (input.StartsWith('#') && span.Length == 7 &&
byte.TryParse(span.Slice(1, 2), NumberStyles.AllowHexSpecifier, null, out byte r) &&
byte.TryParse(span.Slice(3, 2), NumberStyles.AllowHexSpecifier, null, out byte g) &&
byte.TryParse(span.Slice(5, 2), NumberStyles.AllowHexSpecifier, null, out byte b))
{
result = Rgb(r, g, b);
return true;
}

if (ansiColorNames.TryGetValue(input, out var color))
{
result = color;
return true;
}

result = default;
return false;
}

public enum Type
{
Foreground,
Expand Down
2 changes: 1 addition & 1 deletion src/PrettyPrompt/PrettyPrompt.csproj
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
<Project Sdk="Microsoft.NET.Sdk">

<PropertyGroup>
<Version>4.1.0</Version>
<Version>4.1.1</Version>

<PackageId>PrettyPrompt</PackageId>
<PackageTags>repl readline console cli</PackageTags>
Expand Down
32 changes: 32 additions & 0 deletions tests/PrettyPrompt.Tests/AnsiColorTests.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
using PrettyPrompt.Highlighting;
using Xunit;

namespace PrettyPrompt.Tests;

public class AnsiColorTests
{
[Theory]
[InlineData(0, 0, 0, "#000000")]
[InlineData(13, 183, 227, "#0DB7E3")]
[InlineData(255, 255, 255, "#FFFFFF")]
public void RgbToStringAndParse(byte r, byte g, byte b, string text)
{
Assert.Equal(text, AnsiColor.Rgb(r, g, b).ToString());

Assert.True(AnsiColor.TryParse(text, out var parsedColor));
Assert.Equal(text, parsedColor.ToString());
}

[Fact]
public void WllKnownColorToStringAndParse()
{
Assert.True(AnsiColor.TryParse("Black", out var parsedColor));
Assert.Equal(AnsiColor.Black.ToString(), parsedColor.ToString());

Assert.True(AnsiColor.TryParse("white", out parsedColor));
Assert.Equal(AnsiColor.White.ToString(), parsedColor.ToString());

Assert.True(AnsiColor.TryParse("GREEN", out parsedColor));
Assert.Equal(AnsiColor.Green.ToString(), parsedColor.ToString());
}
}
12 changes: 6 additions & 6 deletions tests/PrettyPrompt.Tests/PrettyPrompt.Tests.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -8,18 +8,18 @@
</PropertyGroup>

<ItemGroup>
<PackageReference Include="Microsoft.NET.Test.Sdk" Version="17.4.1" />
<PackageReference Include="NSubstitute" Version="4.4.0" />
<PackageReference Include="xunit" Version="2.4.2" />
<PackageReference Include="xunit.runner.visualstudio" Version="2.4.5">
<PackageReference Include="Microsoft.NET.Test.Sdk" Version="17.7.2" />
<PackageReference Include="NSubstitute" Version="5.1.0" />
<PackageReference Include="xunit" Version="2.5.1" />
<PackageReference Include="xunit.runner.visualstudio" Version="2.5.1">
<PrivateAssets>all</PrivateAssets>
<IncludeAssets>runtime; build; native; contentfiles; analyzers; buildtransitive</IncludeAssets>
</PackageReference>
<PackageReference Include="coverlet.collector" Version="3.2.0">
<PackageReference Include="coverlet.collector" Version="6.0.0">
<PrivateAssets>all</PrivateAssets>
<IncludeAssets>runtime; build; native; contentfiles; analyzers; buildtransitive</IncludeAssets>
</PackageReference>
<PackageReference Include="coverlet.msbuild" Version="3.2.0">
<PackageReference Include="coverlet.msbuild" Version="6.0.0">
<PrivateAssets>all</PrivateAssets>
<IncludeAssets>runtime; build; native; contentfiles; analyzers; buildtransitive</IncludeAssets>
</PackageReference>
Expand Down

0 comments on commit 4245c19

Please # to comment.