using LibMatrix.EventTypes.Spec; namespace LibMatrix.Helpers; public class MessageBuilder(string msgType = "m.text", string format = "org.matrix.custom.html") { private RoomMessageEventContent Content { get; set; } = new() { MessageType = msgType, Format = format }; public RoomMessageEventContent Build() => Content; public MessageBuilder WithBody(string body) { Content.Body += body; Content.FormattedBody += body; return this; } public MessageBuilder WithHtmlTag(string tag, string body, Dictionary? attributes = null) { Content.Body += body; Content.FormattedBody += $"<{tag}"; if (attributes != null) foreach (var (key, value) in attributes) Content.FormattedBody += $" {key}=\"{value}\""; Content.FormattedBody += $">{body}"; return this; } public MessageBuilder WithHtmlTag(string tag, Action bodyBuilder, Dictionary? attributes = null) { Content.FormattedBody += $"<{tag}"; if (attributes != null) foreach (var (key, value) in attributes) Content.FormattedBody += $" {key}=\"{value}\""; Content.FormattedBody += ">"; bodyBuilder(this); Content.FormattedBody += $""; return this; } public MessageBuilder WithColoredBody(string color, string body) { Content.Body += body; Content.FormattedBody += $"{body}"; return this; } public MessageBuilder WithColoredBody(string color, Action bodyBuilder) { Content.FormattedBody += $""; bodyBuilder(this); Content.FormattedBody += ""; return this; } public MessageBuilder WithCustomEmoji(string mxcUri, string name) { Content.Body += $"{{{name}}}"; Content.FormattedBody += $"\"{name}\""; return this; } public MessageBuilder WithRainbowString(string text, byte skip = 1, int offset = 0, double lengthFactor = 255.0, bool useLength = true) { if (useLength) { lengthFactor = text.Length; } // HslaColorInterpolator interpolator = new((0, 255, 128, 255), (255, 255, 128, 255)); // // RainbowEnumerator enumerator = new(skip, offset, lengthFactor); // for (int i = 0; i < text.Length; i++) { // // var (r, g, b) = enumerator.Next(); // // var (r,g,b,a) = interpolator.Interpolate((i+offset) * skip, lengthFactor).ToRgba(); // // Console.WriteLine($"RBA: {r} {g} {b} {a}"); // // Content.FormattedBody += $"{text[i]}"; // } return this; } public MessageBuilder WithCodeBlock(string code, string language = "plaintext") { Content.Body += code; Content.FormattedBody += $"
{code}
"; return this; } public MessageBuilder WithCollapsibleSection(string title, string body) { Content.Body += body; Content.FormattedBody += $"
{title}{body}
"; return this; } public MessageBuilder WithCollapsibleSection(string title, Action bodyBuilder) { Content.FormattedBody += $"
{title}"; bodyBuilder(this); Content.FormattedBody += "
"; return this; } public MessageBuilder WithTable(Action tableBuilder) { var tb = new TableBuilder(this); this.WithHtmlTag("table", msb => tableBuilder(tb)); return this; } public class TableBuilder(MessageBuilder msb) { public TableBuilder WithTitle(string title, int colspan) { msb.Content.Body += title + "\n"; msb.Content.FormattedBody += $"{title}"; return this; } public TableBuilder WithRow(Action rowBuilder) { var rb = new RowBuilder(msb); msb.WithHtmlTag("tr", msb => rowBuilder(rb)).WithBody("\n"); return this; } public class RowBuilder(MessageBuilder msb) { public RowBuilder WithCell(string content, Dictionary? attributes = null) { msb.Content.Body += content + "\n"; msb.Content.FormattedBody += $"{content}\t"; return this; } } } }