From bf2da30c7ae9d4c15a5e22f3ee0b1bae2ca66e46 Mon Sep 17 00:00:00 2001 From: "Emma [it/its]@Rory&" Date: Wed, 24 Jan 2024 02:28:54 +0100 Subject: MessageBuilder extensions --- LibMatrix/Helpers/MessageBuilder.cs | 50 ++++++++++++++++++++++++++++------ LibMatrix/LibMatrix.csproj | 4 +-- LibMatrix/Services/ServiceInstaller.cs | 2 +- 3 files changed, 45 insertions(+), 11 deletions(-) (limited to 'LibMatrix') diff --git a/LibMatrix/Helpers/MessageBuilder.cs b/LibMatrix/Helpers/MessageBuilder.cs index 7715462..250187a 100644 --- a/LibMatrix/Helpers/MessageBuilder.cs +++ b/LibMatrix/Helpers/MessageBuilder.cs @@ -11,6 +11,37 @@ public class MessageBuilder(string msgType = "m.text", string format = "org.matr 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}"; @@ -25,14 +56,17 @@ public class MessageBuilder(string msgType = "m.text", string format = "org.matr } public MessageBuilder WithRainbowString(string text, byte skip = 1, int offset = 0, double lengthFactor = 255.0, bool useLength = true) { - if (useLength) { - lengthFactor = text.Length; - } - RainbowEnumerator enumerator = new(skip, offset, lengthFactor); - for (int i = 0; i < text.Length; i++) { - var (r, g, b) = enumerator.Next(); - Content.FormattedBody += $"{text[i]}"; - } + // 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; } diff --git a/LibMatrix/LibMatrix.csproj b/LibMatrix/LibMatrix.csproj index a2ee327..16e43f5 100644 --- a/LibMatrix/LibMatrix.csproj +++ b/LibMatrix/LibMatrix.csproj @@ -21,8 +21,8 @@ + If you want to use a time-appropriate version of the library, recursively clone https://cgit.rory.gay/matrix/MatrixUtils.git + instead, since this will be locked by the MatrixUtils project, which contains both LibMatrix and ArcaneLibs as a submodule. --> diff --git a/LibMatrix/Services/ServiceInstaller.cs b/LibMatrix/Services/ServiceInstaller.cs index ad5bedc..358dc2a 100644 --- a/LibMatrix/Services/ServiceInstaller.cs +++ b/LibMatrix/Services/ServiceInstaller.cs @@ -7,7 +7,7 @@ public static class ServiceInstaller { public static IServiceCollection AddRoryLibMatrixServices(this IServiceCollection services, RoryLibMatrixConfiguration? config = null) { //Check required services // if (!services.Any(x => x.ServiceType == typeof(TieredStorageService))) - // throw new Exception("[MRUCore/DI] No TieredStorageService has been registered!"); + // throw new Exception("[RMUCore/DI] No TieredStorageService has been registered!"); //Add config services.AddSingleton(config ?? new RoryLibMatrixConfiguration()); -- cgit 1.4.1