about summary refs log tree commit diff
path: root/LibMatrix/Helpers/MessageBuilder.cs
diff options
context:
space:
mode:
authorEmma [it/its]@Rory& <root@rory.gay>2024-01-24 02:28:54 +0100
committerEmma [it/its]@Rory& <root@rory.gay>2024-01-24 02:28:54 +0100
commitbf2da30c7ae9d4c15a5e22f3ee0b1bae2ca66e46 (patch)
treec375a53ef850e33d8721abb0f68b34eb10afed08 /LibMatrix/Helpers/MessageBuilder.cs
parentAbstract FederationClient from RemoteHomeserver (diff)
downloadLibMatrix-bf2da30c7ae9d4c15a5e22f3ee0b1bae2ca66e46.tar.xz
MessageBuilder extensions
Diffstat (limited to 'LibMatrix/Helpers/MessageBuilder.cs')
-rw-r--r--LibMatrix/Helpers/MessageBuilder.cs50
1 files changed, 42 insertions, 8 deletions
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<string, string>? attributes = null) {
+        Content.Body += body;
+        Content.FormattedBody += $"<{tag}";
+        if (attributes != null) {
+            foreach (var (key, value) in attributes) {
+                Content.FormattedBody += $" {key}=\"{value}\"";
+            }
+        }
+        Content.FormattedBody += $">{body}</{tag}>";
+        return this;
+    }
+    
+    public MessageBuilder WithHtmlTag(string tag, Action<MessageBuilder> bodyBuilder, Dictionary<string, string>? attributes = null) {
+        Content.FormattedBody += $"<{tag}";
+        if (attributes != null) {
+            foreach (var (key, value) in attributes) {
+                Content.FormattedBody += $" {key}=\"{value}\"";
+            }
+        }
+        Content.FormattedBody += ">";
+        bodyBuilder(this);
+        Content.FormattedBody += $"</{tag}>";
+        return this;
+    }
+    
     public MessageBuilder WithColoredBody(string color, string body) {
         Content.Body += body;
         Content.FormattedBody += $"<font color=\"{color}\">{body}</font>";
@@ -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 += $"<font color=\"#{r:X2}{g:X2}{b:X2}\">{text[i]}</font>";
-        }
+        // 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 += $"<font color=\"#{r:X2}{g:X2}{b:X2}\">{text[i]}</font>";
+        // }
 
         return this;
     }