From 532c5e75d31a9b18cd0d6677719bada3320c341a Mon Sep 17 00:00:00 2001 From: Augustin Cavalier Date: Mon, 16 Sep 2024 14:39:31 -0400 Subject: [PATCH] TextTable: Ignore ANSI escape codes when counting text width. Otherwise the launch_roster table will be misaligned. --- src/kits/shared/TextTable.cpp | 31 +++++++++++++++++++++++++++---- 1 file changed, 27 insertions(+), 4 deletions(-) diff --git a/src/kits/shared/TextTable.cpp b/src/kits/shared/TextTable.cpp index 2601205b3e..8baab597c1 100644 --- a/src/kits/shared/TextTable.cpp +++ b/src/kits/shared/TextTable.cpp @@ -7,6 +7,8 @@ #include #include +#include +#include #include @@ -65,18 +67,39 @@ struct TextTable::Column { fWidth = width; } - void UpdateNeededWidth(const BString& text) + static int32 TextWidth(const BString& text) { // TODO: Full-width character support. - int32 textWidth = text.CountChars(); + int32 textWidth = 0; + const char* string = text.String(), *stringEnd = text.String() + text.Length(); + while (string < stringEnd) { + uint32 charLen = UTF8NextCharLen(string, stringEnd - string); + if (charLen == 1 && string[0] == '\033') { + // ANSI escape code. + charLen++; + if (string[charLen - 1] == '[') { + // Keep going until we hit an end character. + while (!isalpha(string[charLen - 1]) && string[charLen - 1] != '\0') + charLen++; + } + } else { + textWidth++; + } + string += charLen; + } + return textWidth; + } + + void UpdateNeededWidth(const BString& text) + { + int32 textWidth = TextWidth(text); if (textWidth > fNeededWidth) fNeededWidth = textWidth; } BString Format(const BString& text) { - // TODO: Full-width character support. - int32 textWidth = text.CountChars(); + int32 textWidth = TextWidth(text); if (textWidth == fWidth) return text;