refactor(integrations): apply review findings (constant, util move, prose cleanup)

This commit is contained in:
2026-05-06 21:08:50 +02:00
parent e58376bf50
commit 2d768e4edb
7 changed files with 52 additions and 41 deletions
+33
View File
@@ -1,5 +1,6 @@
using System.Globalization;
using System.Text;
using Dalamud.Bindings.ImGui;
namespace HellionChat.Util;
@@ -29,4 +30,36 @@ internal static class StringUtil
// separator to '.' so a German locale doesn't render "1,5GB".
return (Math.Sign(byteCount) * num).ToString("0.#", CultureInfo.InvariantCulture) + suf[place];
}
// Returns the text unchanged when it already fits the width budget,
// otherwise the longest prefix plus a horizontal-ellipsis character that
// still fits. Used by the chat header Honorific title slot and reused by
// the chat-line truncation path in later cycles.
public static string TruncateToFitWidth(string text, float maxWidth)
{
if (ImGui.CalcTextSize(text).X <= maxWidth)
{
return text;
}
// Binary-search the longest prefix that fits with an ellipsis.
const string ellipsis = "…";
var lo = 0;
var hi = text.Length;
while (lo < hi)
{
var mid = (lo + hi + 1) / 2;
var candidate = text[..mid] + ellipsis;
if (ImGui.CalcTextSize(candidate).X <= maxWidth)
{
lo = mid;
}
else
{
hi = mid - 1;
}
}
return lo == 0 ? ellipsis : text[..lo] + ellipsis;
}
}