Skip to content

Commit b5090e3

Browse files
committed
feat: chunk large subcodexes on words
1 parent f327969 commit b5090e3

3 files changed

Lines changed: 60 additions & 1 deletion

File tree

Elementalist/DiscordUi/CodexUi.cs

Lines changed: 13 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,10 @@
11
using System.Data;
2+
using System.Text;
23
using System.Text.RegularExpressions;
34
using Elementalist.Models;
5+
using Elementalist.Shared;
46
using ElementalistBot.Infrastructure.DataAccess.Rules;
7+
using NetCord;
58
using NetCord.Rest;
69
using NetCord.Services.ApplicationCommands;
710

@@ -66,7 +69,16 @@ private static EmbedProperties CreateCodexRuleEmbed(CodexEntry rule)
6669

6770
foreach (var subCodex in rule.Subcodexes)
6871
{
69-
codexEmbed.AddFields(new EmbedFieldProperties().WithName(subCodex.Title).WithValue(subCodex.Content));
72+
var continued = string.Empty;
73+
foreach (var fieldContentChunk in subCodex.Content.ChunkStringOnWords(1024)) //discord's max field length
74+
{
75+
codexEmbed.AddFields(new EmbedFieldProperties()
76+
.WithName(subCodex.Title + continued)
77+
.WithValue(new string(fieldContentChunk))
78+
);
79+
80+
continued = " - continued";
81+
}
7082
}
7183

7284
return codexEmbed;
Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
using System.Text;
2+
3+
namespace Elementalist.Shared;
4+
5+
public static class StringChunker
6+
{
7+
public static IEnumerable<string> ChunkStringOnWords(this string value, int maxLength)
8+
{
9+
var words = value.Split([' ']);
10+
var sb = new StringBuilder();
11+
int currentLength = 0;
12+
13+
foreach (var word in words)
14+
{
15+
if (currentLength + word.Length < maxLength)
16+
{
17+
sb.Append(word).Append(' ');
18+
currentLength += word.Length + 1;
19+
continue;
20+
}
21+
22+
var wordChunk = sb.ToString().Trim();
23+
sb = new StringBuilder();
24+
sb.Append(word).Append(' ');
25+
currentLength = word.Length + 1;
26+
27+
yield return wordChunk;
28+
}
29+
}
30+
}
Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
using Elementalist.Shared;
2+
using Xunit;
3+
4+
namespace ElementalistTests.Shared;
5+
6+
public class StringChunkerTests
7+
{
8+
[Fact]
9+
public void ChunkStringOnWords()
10+
{
11+
var testSentence = "The quick brown fox jumped over the lazy dog.";
12+
var chunked = testSentence.ChunkStringOnWords(12);
13+
14+
Assert.True(chunked.All(chunk => chunk.Length <= 12));
15+
Assert.Equal("brown fox", chunked.ElementAt(1));
16+
}
17+
}

0 commit comments

Comments
 (0)