Skip to content

Commit 9cfb5f9

Browse files
committed
Feat: AnsiColorStore
1 parent 582c116 commit 9cfb5f9

File tree

1 file changed

+19
-10
lines changed

1 file changed

+19
-10
lines changed

src/CodeOfChaos.Ansi.Generators/AnsiStringBuilderGenerator.cs

Lines changed: 19 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -37,56 +37,65 @@ private static void GenerateCode(SourceProductionContext context, ImmutableArray
3737
IEnumerable<ColorEntry> colors = files.SelectMany(file => ParseColorFile(context, file)).ToArray();
3838
var builder = new GeneratorStringBuilder();
3939

40+
#region AnsiColorStore
41+
context.AddSource("AnsiColorStore.g.cs", builder
42+
.AppendAutoGenerated()
43+
.AppendNamespace("CodeOfChaos.Ansi")
44+
.AppendLine("public class AnsiColorStore {")
45+
.ForEach(colors, itemFormatter: (stringBuilder, entry) => stringBuilder
46+
.AppendLineIndented($"public static readonly CodeOfChaos.Ansi.ByteVector3 {entry.Name} = new({entry.Colors});")
47+
)
48+
.AppendLine("}")
49+
.ToStringAndClear());
50+
#endregion
51+
4052
#region Fore & Background
4153
foreach (string section in new[] { "Foreground", "Background", "Underline" }) {
4254
context.AddSource($"Ansi{section}Builder.g.cs", builder
43-
.AppendUsings("System")
4455
.AppendAutoGenerated()
4556
.AppendNamespace("CodeOfChaos.Ansi")
4657
.AppendLine($"public partial class Ansi{section}Builder {{")
4758
.ForEach(colors, itemFormatter: (stringBuilder, entry) => stringBuilder
4859
.AppendBodyIndented($$"""
4960
#region {{entry.Name}}
50-
private static CodeOfChaos.Ansi.ByteVector3 _{{entry.Name}} = new({{entry.Colors}});
51-
52-
public string {{entry.Name}}(string text) => $"{CodeOfChaos.Ansi.AnsiCodes.Rgb{{section}}Color(_{{entry.Name}})}{text}{CodeOfChaos.Ansi.AnsiCodes.ResetGraphicsModes}";
61+
public string {{entry.Name}}(string text) => $"{CodeOfChaos.Ansi.AnsiCodes.Rgb{{section}}Color(CodeOfChaos.Ansi.AnsiColorStore.{{entry.Name}})}{text}{CodeOfChaos.Ansi.AnsiCodes.ResetGraphicsModes}";
5362
5463
public Ansi{{section}}Builder Append{{entry.Name}}(string text) => BuilderAction(() => {
5564
Builder
56-
.Append(CodeOfChaos.Ansi.AnsiCodes.Rgb{{section}}Color(_{{entry.Name}}))
65+
.Append(CodeOfChaos.Ansi.AnsiCodes.Rgb{{section}}Color(CodeOfChaos.Ansi.AnsiColorStore.{{entry.Name}}))
5766
.Append(text)
5867
.Append(CodeOfChaos.Ansi.AnsiCodes.ResetGraphicsModes);
5968
});
6069
6170
public Ansi{{section}}Builder Append{{entry.Name}}(Func<string> action) => BuilderAction(() => {
6271
Builder
63-
.Append(CodeOfChaos.Ansi.AnsiCodes.Rgb{{section}}Color(_{{entry.Name}}))
72+
.Append(CodeOfChaos.Ansi.AnsiCodes.Rgb{{section}}Color(CodeOfChaos.Ansi.AnsiColorStore.{{entry.Name}}))
6473
.Append(action())
6574
.Append(CodeOfChaos.Ansi.AnsiCodes.ResetGraphicsModes);
6675
});
6776
6877
public Ansi{{section}}Builder Append{{entry.Name}}(Action<Ansi{{section}}Builder> action) => BuilderAction(() => {
69-
Builder.Append(CodeOfChaos.Ansi.AnsiCodes.Rgb{{section}}Color(_{{entry.Name}}));
78+
Builder.Append(CodeOfChaos.Ansi.AnsiCodes.Rgb{{section}}Color(CodeOfChaos.Ansi.AnsiColorStore.{{entry.Name}}));
7079
action(this);
7180
Builder.Append(CodeOfChaos.Ansi.AnsiCodes.ResetGraphicsModes);
7281
});
7382
7483
public Ansi{{section}}Builder Append{{entry.Name}}Line(string text) => BuilderAction(() => {
7584
Builder
76-
.Append(CodeOfChaos.Ansi.AnsiCodes.Rgb{{section}}Color(_{{entry.Name}}))
85+
.Append(CodeOfChaos.Ansi.AnsiCodes.Rgb{{section}}Color(CodeOfChaos.Ansi.AnsiColorStore.{{entry.Name}}))
7786
.Append(text)
7887
.AppendLine(CodeOfChaos.Ansi.AnsiCodes.ResetGraphicsModes);
7988
});
8089
8190
public Ansi{{section}}Builder Append{{entry.Name}}Line(Func<string> action) => BuilderAction(() => {
8291
Builder
83-
.Append(CodeOfChaos.Ansi.AnsiCodes.Rgb{{section}}Color(_{{entry.Name}}))
92+
.Append(CodeOfChaos.Ansi.AnsiCodes.Rgb{{section}}Color(CodeOfChaos.Ansi.AnsiColorStore.{{entry.Name}}))
8493
.Append(action())
8594
.AppendLine(CodeOfChaos.Ansi.AnsiCodes.ResetGraphicsModes);
8695
});
8796
8897
public Ansi{{section}}Builder Append{{entry.Name}}Line(Action<Ansi{{section}}Builder> action) => BuilderAction(() => {
89-
Builder.Append(CodeOfChaos.Ansi.AnsiCodes.Rgb{{section}}Color(_{{entry.Name}}));
98+
Builder.Append(CodeOfChaos.Ansi.AnsiCodes.Rgb{{section}}Color(CodeOfChaos.Ansi.AnsiColorStore.{{entry.Name}}));
9099
action(this);
91100
Builder.AppendLine(CodeOfChaos.Ansi.AnsiCodes.ResetGraphicsModes);
92101
});

0 commit comments

Comments
 (0)