Skip to content
This repository was archived by the owner on Nov 24, 2022. It is now read-only.
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
24 changes: 23 additions & 1 deletion TabletBot.Discord/Formatting.cs
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
using System;
using System.Linq;
using System.Text;
using Discord;

Expand All @@ -22,10 +23,31 @@ public static string CodeBlock(string text, string? lang = null) =>

public static void AppendCodeBlock(this StringBuilder stringBuilder, string[] lines, string? lang = null)
{
TrimBaseIndentation(lines);

stringBuilder.AppendLine(CODE_BLOCK + lang);
foreach (var line in lines)
stringBuilder.AppendLine(line);
stringBuilder.AppendLine(CODE_BLOCK);
}

public static void TrimBaseIndentation(string[] lines)
{
var baseIndentationLength = lines.Min(line => {
var indentation = CountIndentation(line);
return line.Length > indentation ? indentation : int.MaxValue;
});

for (int i = 0; i != lines.Length; i++)
lines[i] = lines[i].Substring(Math.Min(baseIndentationLength, lines[i].Length)).TrimEnd();
}

public static int CountIndentation(string line)
{
for (var i = 0; i < line.Length; i++)
if (!char.IsWhiteSpace(line[i]))
return i;
return int.MaxValue;
}
}
}
}