diff --git a/src/PluginInterface.cpp b/src/PluginInterface.cpp
index c7e33b1..2ab1a51 100644
--- a/src/PluginInterface.cpp
+++ b/src/PluginInterface.cpp
@@ -14,6 +14,8 @@ namespace Echoion::Plugin
// TeamSpeak rejects over-long text messages (long replies such as
// search results or the queue silently never arrive). Split into
// UTF-8-safe chunks on line boundaries so every line is delivered.
+ // A … block is kept whole in its own message —
+ // splitting it across messages would break the collapsible.
constexpr std::size_t kMaxMessageBytes = 900;
std::vector chunkMessage(const std::string& message,
@@ -26,6 +28,7 @@ namespace Echoion::Plugin
}
std::string current;
+ int detailsDepth = 0;
auto flush = [&] {
if (!current.empty()) {
chunks.push_back(current);
@@ -41,26 +44,45 @@ namespace Echoion::Plugin
: message.substr(pos, nl - pos + 1);
pos = (nl == std::string::npos) ? message.size() : nl + 1;
- // Hard-split a single over-long line on a UTF-8 boundary.
- while (line.size() > limit) {
- std::size_t cut = limit;
- while (cut > 0 &&
- (static_cast(line[cut]) & 0xC0) ==
- 0x80) {
- --cut;
+ const bool opens =
+ line.find("") != std::string::npos;
+ const bool closes =
+ line.find(" ") != std::string::npos;
+
+ if (detailsDepth == 0 && !current.empty()) {
+ // Start each top-level block in a fresh message,
+ // and otherwise split when adding the line would overflow.
+ if (opens || current.size() + line.size() > limit) {
+ flush();
}
- if (cut == 0) {
- cut = limit; // pathological line; avoid an infinite loop
- }
- flush();
- chunks.push_back(line.substr(0, cut));
- line.erase(0, cut);
}
- if (current.size() + line.size() > limit) {
- flush();
+ // Hard-split an over-long line on a UTF-8 boundary, but never
+ // inside a block (it stays whole even if oversized).
+ if (detailsDepth == 0 && !opens) {
+ while (line.size() > limit) {
+ std::size_t cut = limit;
+ while (cut > 0 && (static_cast(
+ line[cut]) &
+ 0xC0) == 0x80) {
+ --cut;
+ }
+ if (cut == 0) {
+ cut = limit; // pathological line; avoid infinite loop
+ }
+ flush();
+ chunks.push_back(line.substr(0, cut));
+ line.erase(0, cut);
+ }
}
+
current += line;
+ if (opens) {
+ ++detailsDepth;
+ }
+ if (closes && detailsDepth > 0) {
+ --detailsDepth;
+ }
}
flush();
return chunks;