From 5ded28e38475e265c8cd27742d079a42ad3d39e0 Mon Sep 17 00:00:00 2001 From: Stephan Williams Date: Wed, 26 Jul 2017 20:30:59 -0400 Subject: [PATCH 1/2] Truncate should use display width instead of # characters --- src/musikcube/cursespp/Text.cpp | 15 ++++++++++----- 1 file changed, 10 insertions(+), 5 deletions(-) diff --git a/src/musikcube/cursespp/Text.cpp b/src/musikcube/cursespp/Text.cpp index 1663d82f3..976d78b20 100755 --- a/src/musikcube/cursespp/Text.cpp +++ b/src/musikcube/cursespp/Text.cpp @@ -51,8 +51,10 @@ namespace cursespp { auto it = str.begin(); auto end = str.end(); - size_t c = 0; - while (c < len && it != str.end()) { + size_t cols = 0; + while (it != str.end()) { + auto prev = it; + try { utf8::next(it, end); } @@ -61,10 +63,13 @@ namespace cursespp { ++it; } - ++c; - } + size_t c = u8cols(std::string(prev, it)); + if (cols + c > len) { + return std::string(str.begin(), prev); + } - return std::string(str.begin(), it); + cols += c; + } } return str; From 989254d16514beadc23e62938d3552cc3a93e52f Mon Sep 17 00:00:00 2001 From: Stephan Williams Date: Thu, 27 Jul 2017 12:50:37 -0400 Subject: [PATCH 2/2] Rework Truncate, fix Ellipsize to handle variable len --- src/musikcube/cursespp/Text.cpp | 23 ++++++++++++++--------- 1 file changed, 14 insertions(+), 9 deletions(-) diff --git a/src/musikcube/cursespp/Text.cpp b/src/musikcube/cursespp/Text.cpp index 976d78b20..0f30c3fdc 100755 --- a/src/musikcube/cursespp/Text.cpp +++ b/src/musikcube/cursespp/Text.cpp @@ -48,12 +48,13 @@ namespace cursespp { /* not a simple substr anymore, gotta deal with multi-byte characters... */ if (u8cols(str) > len) { + auto prev = str.begin(); auto it = str.begin(); auto end = str.end(); size_t cols = 0; - while (it != str.end()) { - auto prev = it; + while (cols <= len && it != str.end()) { + prev = it; try { utf8::next(it, end); @@ -63,13 +64,10 @@ namespace cursespp { ++it; } - size_t c = u8cols(std::string(prev, it)); - if (cols + c > len) { - return std::string(str.begin(), prev); - } - - cols += c; + cols += u8cols(std::string(prev, it)); } + + return std::string(str.begin(), prev); } return str; @@ -77,7 +75,14 @@ namespace cursespp { std::string Ellipsize(const std::string& str, size_t len) { if (u8cols(str) > len) { - return Truncate(str, len - 2) + ".."; + std::string trunc = Truncate(str, len - 2); + + size_t tlen = u8cols(trunc); + for (size_t i = tlen; i < len; i++) { + trunc += "."; + } + + return trunc; } return str;