From 7dc42f543f7e681fa302a4ddff53d86b88a7d83b Mon Sep 17 00:00:00 2001
From: patrick96
Date: Sun, 6 May 2018 21:59:40 +0200
Subject: [PATCH] fix(builder): Respect label-ellipsis option
This slightly changes the existing behavior of maxlen. Before with a
maxlen of 4 'abcde' used to yield 'abcd...' now it yields 'a...'
---
src/components/builder.cpp | 17 +++++++++++++++--
tests/unit_tests/components/builder.cpp | 1 +
2 files changed, 16 insertions(+), 2 deletions(-)
diff --git a/src/components/builder.cpp b/src/components/builder.cpp
index ae697b11..6e2a7e73 100644
--- a/src/components/builder.cpp
+++ b/src/components/builder.cpp
@@ -555,8 +555,21 @@ string builder::foreground_hex() {
string builder::get_label_text(const label_t& label) {
string text{label->get()};
- if (label->m_maxlen > 0 && string_util::char_len(text) > label->m_maxlen) {
- text = string_util::utf8_truncate(std::move(text), label->m_maxlen) + "...";
+ size_t maxlen = label->m_maxlen;
+
+ if (maxlen > 0 && string_util::char_len(text) > maxlen ) {
+ if(label->m_ellipsis) {
+ if(maxlen < 3) {
+ throw application_error(sstream()
+ << "Label has maxlen (" << maxlen
+ << ") that is smaller than size of ellipsis(3)");
+ }
+
+ text = string_util::utf8_truncate(std::move(text), maxlen - 3) + "...";
+ }
+ else {
+ text = string_util::utf8_truncate(std::move(text), maxlen);
+ }
}
return text;
diff --git a/tests/unit_tests/components/builder.cpp b/tests/unit_tests/components/builder.cpp
index bee20fc8..dac48b2a 100644
--- a/tests/unit_tests/components/builder.cpp
+++ b/tests/unit_tests/components/builder.cpp
@@ -44,6 +44,7 @@ class GetLabelTextTest :
vector>> get_label_text_list = {
{"...", make_tuple("abcd", true, 3)},
+ {"abc", make_tuple("abc", true, 3)},
{"abc", make_tuple("abcdefgh", false, 3)},
{"a...", make_tuple("abcdefgh", true, 4)},
{"abcd...", make_tuple("abcdefgh", true, 7)},