about summary refs log tree commit diff
path: root/LibMatrix/Helpers
diff options
context:
space:
mode:
authorEmma [it/its]@Rory& <root@rory.gay>2024-01-11 07:31:09 +0100
committerEmma [it/its]@Rory& <root@rory.gay>2024-01-11 07:31:09 +0100
commit8dadf547033d71480fd7756809992c0f32549f59 (patch)
treea23631cc840047e3ad9e32dda9c043511af1a583 /LibMatrix/Helpers
parentTry-create factory for RemoteHomeserver, more reliable RemoteHomeserver creation (diff)
downloadLibMatrix-8dadf547033d71480fd7756809992c0f32549f59.tar.xz
Cleanup, more message formatters, messagebuilder start
Diffstat (limited to 'LibMatrix/Helpers')
-rw-r--r--LibMatrix/Helpers/MessageBuilder.cs40
-rw-r--r--LibMatrix/Helpers/MessageFormatter.cs24
2 files changed, 62 insertions, 2 deletions
diff --git a/LibMatrix/Helpers/MessageBuilder.cs b/LibMatrix/Helpers/MessageBuilder.cs
new file mode 100644
index 0000000..7715462
--- /dev/null
+++ b/LibMatrix/Helpers/MessageBuilder.cs
@@ -0,0 +1,40 @@
+using ArcaneLibs;
+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 WithColoredBody(string color, string body) {
+        Content.Body += body;
+        Content.FormattedBody += $"<font color=\"{color}\">{body}</font>";
+        return this;
+    }
+    
+    public MessageBuilder WithColoredBody(string color, Action<MessageBuilder> bodyBuilder) {
+        Content.FormattedBody += $"<font color=\"{color}\">";
+        bodyBuilder(this);
+        Content.FormattedBody += "</font>";
+        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;
+        }
+        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>";
+        }
+
+        return this;
+    }
+    
+}
\ No newline at end of file
diff --git a/LibMatrix/Helpers/MessageFormatter.cs b/LibMatrix/Helpers/MessageFormatter.cs
index b2dda61..b7c6975 100644
--- a/LibMatrix/Helpers/MessageFormatter.cs
+++ b/LibMatrix/Helpers/MessageFormatter.cs
@@ -13,7 +13,7 @@ public static class MessageFormatter {
 
     public static RoomMessageEventContent FormatException(string error, Exception e) {
         return new RoomMessageEventContent(body: $"{error}: {e.Message}", messageType: "m.text") {
-            FormattedBody = $"<font color=\"#EE4444\">{error}: <pre>{e.Message}</pre></font>",
+            FormattedBody = $"<font color=\"#EE4444\">{error}: <pre><code>{e.Message}</code></pre></font>",
             Format = "org.matrix.custom.html"
         };
     }
@@ -27,7 +27,7 @@ public static class MessageFormatter {
 
     public static RoomMessageEventContent FormatSuccessJson(string text, object data) {
         return new RoomMessageEventContent(body: text, messageType: "m.text") {
-            FormattedBody = $"<font color=\"#00FF00\">{text}: <pre>{data.ToJson(ignoreNull: true)}</pre></font>",
+            FormattedBody = $"<font color=\"#00FF00\">{text}: <pre><code>{data.ToJson(ignoreNull: true)}</code></pre></font>",
             Format = "org.matrix.custom.html"
         };
     }
@@ -53,4 +53,24 @@ public static class MessageFormatter {
             Format = "org.matrix.custom.html"
         };
     }
+    
+    public static RoomMessageEventContent FormatWarningJson(string warning, object data) {
+        return new RoomMessageEventContent(body: warning, messageType: "m.text") {
+            FormattedBody = $"<font color=\"#FFFF00\">{warning}: <pre><code>{data.ToJson(ignoreNull: true)}</code></pre></font>",
+            Format = "org.matrix.custom.html"
+        };
+    }
+    
+    public static RoomMessageEventContent Concat(this RoomMessageEventContent a, RoomMessageEventContent b) {
+        return new RoomMessageEventContent(body: $"{a.Body}{b.Body}", messageType: a.MessageType) {
+            FormattedBody = $"{a.FormattedBody}{b.FormattedBody}",
+            Format = a.Format
+        };
+    }
+    public static RoomMessageEventContent ConcatLine(this RoomMessageEventContent a, RoomMessageEventContent b) {
+        return new RoomMessageEventContent(body: $"{a.Body}\n{b.Body}", messageType: "m.text") {
+            FormattedBody = $"{a.FormattedBody}<br/>{b.FormattedBody}",
+            Format = "org.matrix.custom.html"
+        };
+    }
 }