mirror of
https://github.com/Tyrrrz/DiscordChatExporter.git
synced 2026-03-10 00:43:03 +00:00
Split output file into multiple partitions (#116)
This commit is contained in:
@@ -1,4 +1,6 @@
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.Linq;
|
||||
|
||||
namespace DiscordChatExporter.Core.Models
|
||||
{
|
||||
@@ -31,5 +33,45 @@ namespace DiscordChatExporter.Core.Models
|
||||
|
||||
throw new ArgumentOutOfRangeException(nameof(format));
|
||||
}
|
||||
|
||||
public static IReadOnlyList<ChatLog> SplitIntoPartitions(this ChatLog chatLog, int partitionLimit)
|
||||
{
|
||||
// If chat log has fewer messages than the limit - just return chat log in a list
|
||||
if (chatLog.Messages.Count <= partitionLimit)
|
||||
return new[] {chatLog};
|
||||
|
||||
var result = new List<ChatLog>();
|
||||
|
||||
// Loop through messages
|
||||
var buffer = new List<Message>();
|
||||
foreach (var message in chatLog.Messages)
|
||||
{
|
||||
// Add message to buffer
|
||||
buffer.Add(message);
|
||||
|
||||
// If reached the limit - split and reset buffer
|
||||
if (buffer.Count >= partitionLimit)
|
||||
{
|
||||
// Add to result
|
||||
var chatLogPartition = new ChatLog(chatLog.Guild, chatLog.Channel, chatLog.From, chatLog.To, buffer,
|
||||
chatLog.Mentionables);
|
||||
result.Add(chatLogPartition);
|
||||
|
||||
// Reset the buffer instead of clearing to avoid mutations on existing references
|
||||
buffer = new List<Message>();
|
||||
}
|
||||
}
|
||||
|
||||
// Add what's remaining in buffer
|
||||
if (buffer.Any())
|
||||
{
|
||||
// Add to result
|
||||
var chatLogPartition = new ChatLog(chatLog.Guild, chatLog.Channel, chatLog.From, chatLog.To, buffer,
|
||||
chatLog.Mentionables);
|
||||
result.Add(chatLogPartition);
|
||||
}
|
||||
|
||||
return result;
|
||||
}
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user