Skip to content

Commit

Permalink
Feat: Underline
Browse files Browse the repository at this point in the history
  • Loading branch information
AnnaSasDev committed Jan 10, 2025
1 parent cfd19b4 commit 582c116
Show file tree
Hide file tree
Showing 18 changed files with 234 additions and 214 deletions.
18 changes: 9 additions & 9 deletions src/CodeOfChaos.Ansi.Generators/AnsiStringBuilderGenerator.cs
Original file line number Diff line number Diff line change
Expand Up @@ -36,21 +36,21 @@ public void Initialize(IncrementalGeneratorInitializationContext context) {
private static void GenerateCode(SourceProductionContext context, ImmutableArray<AdditionalText> files) {
IEnumerable<ColorEntry> colors = files.SelectMany(file => ParseColorFile(context, file)).ToArray();
var builder = new GeneratorStringBuilder();

#region Fore & Background
foreach (string section in new[] {"Foreground", "Background"}) {
foreach (string section in new[] { "Foreground", "Background", "Underline" }) {
context.AddSource($"Ansi{section}Builder.g.cs", builder
.AppendUsings("System")
.AppendAutoGenerated()
.AppendNamespace("CodeOfChaos.Ansi")
.AppendLine($"public partial class Ansi{section}Builder {{")
.ForEach(colors, (stringBuilder, entry) => stringBuilder
.ForEach(colors, itemFormatter: (stringBuilder, entry) => stringBuilder
.AppendBodyIndented($$"""
#region {{entry.Name}}
private static CodeOfChaos.Ansi.ByteVector3 _{{entry.Name}} = new({{entry.Colors}});
public string {{entry.Name}}(string text) => $"{CodeOfChaos.Ansi.AnsiCodes.Rgb{{section}}Color(_{{entry.Name}})}{text}{CodeOfChaos.Ansi.AnsiCodes.ResetGraphicsModes}";
public Ansi{{section}}Builder Append{{entry.Name}}(string text) => BuilderAction(() => {
Builder
.Append(CodeOfChaos.Ansi.AnsiCodes.Rgb{{section}}Color(_{{entry.Name}}))
Expand All @@ -70,21 +70,21 @@ private static void GenerateCode(SourceProductionContext context, ImmutableArray
action(this);
Builder.Append(CodeOfChaos.Ansi.AnsiCodes.ResetGraphicsModes);
});
public Ansi{{section}}Builder Append{{entry.Name}}Line(string text) => BuilderAction(() => {
Builder
.Append(CodeOfChaos.Ansi.AnsiCodes.Rgb{{section}}Color(_{{entry.Name}}))
.Append(text)
.AppendLine(CodeOfChaos.Ansi.AnsiCodes.ResetGraphicsModes);
});
public Ansi{{section}}Builder Append{{entry.Name}}Line(Func<string> action) => BuilderAction(() => {
Builder
.Append(CodeOfChaos.Ansi.AnsiCodes.Rgb{{section}}Color(_{{entry.Name}}))
.Append(action())
.AppendLine(CodeOfChaos.Ansi.AnsiCodes.ResetGraphicsModes);
});
public Ansi{{section}}Builder Append{{entry.Name}}Line(Action<Ansi{{section}}Builder> action) => BuilderAction(() => {
Builder.Append(CodeOfChaos.Ansi.AnsiCodes.Rgb{{section}}Color(_{{entry.Name}}));
action(this);
Expand All @@ -97,7 +97,7 @@ private static void GenerateCode(SourceProductionContext context, ImmutableArray
)
.AppendLine("}")
.ToStringAndClear()
);
);
}
#endregion
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -31,22 +31,22 @@
</PropertyGroup>

<ItemGroup>
<None Include="../../LICENSE" Pack="true" PackagePath="" Visible="false" />
<None Include="../../README.md" Pack="true" PackagePath="" />
<None Include="../../assets/icon.png" Pack="true" PackagePath="" Visible="false" />
<None Include="../../LICENSE" Pack="true" PackagePath="" Visible="false"/>
<None Include="../../README.md" Pack="true" PackagePath=""/>
<None Include="../../assets/icon.png" Pack="true" PackagePath="" Visible="false"/>
</ItemGroup>

<!-- Package dependencies -->
<ItemGroup>
<PackageReference Include="Microsoft.CodeAnalysis.Analyzers" Version="3.11.0" PrivateAssets="all" />
<PackageReference Include="Microsoft.CodeAnalysis.Common" Version="4.12.0" PrivateAssets="all" />
<PackageReference Include="Microsoft.CodeAnalysis.CSharp" Version="4.12.0" PrivateAssets="all" />
<PackageReference Include="CodeOfChaos.GeneratorTools" Version="1.2.0" Pack="true" GeneratePathProperty="true" PrivateAssets="all" />
<PackageReference Include="Microsoft.CodeAnalysis.Analyzers" Version="3.11.0" PrivateAssets="all"/>
<PackageReference Include="Microsoft.CodeAnalysis.Common" Version="4.12.0" PrivateAssets="all"/>
<PackageReference Include="Microsoft.CodeAnalysis.CSharp" Version="4.12.0" PrivateAssets="all"/>
<PackageReference Include="CodeOfChaos.GeneratorTools" Version="1.2.0" Pack="true" GeneratePathProperty="true" PrivateAssets="all"/>
</ItemGroup>

<ItemGroup>
<None Include="$(OutputPath)\$(AssemblyName).dll" Pack="true" PackagePath="analyzers/dotnet/cs" Visible="false" />
<None Include="$(PkgCodeOfChaos_GeneratorTools)\lib\netstandard2.0\*.dll" Pack="true" PackagePath="analyzers/dotnet/cs" Visible="false" />
<None Include="$(OutputPath)\$(AssemblyName).dll" Pack="true" PackagePath="analyzers/dotnet/cs" Visible="false"/>
<None Include="$(PkgCodeOfChaos_GeneratorTools)\lib\netstandard2.0\*.dll" Pack="true" PackagePath="analyzers/dotnet/cs" Visible="false"/>
</ItemGroup>

</Project>
25 changes: 13 additions & 12 deletions src/CodeOfChaos.Ansi.Generators/Xml/ColorEntryContainer.cs
Original file line number Diff line number Diff line change
Expand Up @@ -18,24 +18,25 @@ public class ColorEntryContainer {
}

public class XmlColorEntry {

private readonly Regex _regexCommaSeparated = new(@"^(\d+)[,.;:](\d+)[,.;:](\d+)$", RegexOptions.Compiled);
private readonly Regex _regexHexFormat = new(@"^#?([A-Fa-f0-9]{6})$", RegexOptions.Compiled);
[XmlAttribute("Name")]
public string Name { get; set; } = string.Empty;// Name attribute for the color

[XmlAttribute("Color")]
public string Color { get; set; } = string.Empty;// RGB values as a comma-separated string

private readonly Regex _regexCommaSeparated = new(@"^(\d+)[,.;:](\d+)[,.;:](\d+)$", RegexOptions.Compiled);
private readonly Regex _regexHexFormat = new(@"^#?([A-Fa-f0-9]{6})$", RegexOptions.Compiled);

public ColorEntry ToColorEntry() {
if (_regexCommaSeparated.Match(Color) is {Success : true } match) return new ColorEntry {
Name = Name,
Codes = [
int.Parse(match.Groups[1].Value),
int.Parse(match.Groups[2].Value),
int.Parse(match.Groups[3].Value)
]
};
if (_regexCommaSeparated.Match(Color) is { Success : true } match)
return new ColorEntry {
Name = Name,
Codes = [
int.Parse(match.Groups[1].Value),
int.Parse(match.Groups[2].Value),
int.Parse(match.Groups[3].Value)
]
};

if (_regexHexFormat.IsMatch(Color)) {
string hex = Color.TrimStart('#');
Expand All @@ -55,6 +56,6 @@ public class ColorEntry {
public string Name { get; set; } = string.Empty;// Mapped Name

public int[] Codes { get; set; } = [];// Mapped and split RGB values

public string Colors => string.Join(",", Codes);
}
Loading

0 comments on commit 582c116

Please sign in to comment.