From 0346a965a7218b241011c75045e385c032401a7e Mon Sep 17 00:00:00 2001
From: patrick96
Date: Sun, 6 May 2018 23:59:53 +0200
Subject: [PATCH] refactor(builder): No exception when maxlen < 3
The check of the maxlen and ellipsis condition was also moved to the
label creation, this way get_label_text doesn't need to care about the
restrictions placed on maxlen and ellipsis
---
include/drawtypes/label.hpp | 14 +++++++++++++-
src/components/builder.cpp | 10 ++--------
src/drawtypes/label.cpp | 15 +++++++++++++--
tests/unit_tests/components/builder.cpp | 12 ------------
4 files changed, 28 insertions(+), 23 deletions(-)
diff --git a/include/drawtypes/label.hpp b/include/drawtypes/label.hpp
index d2ec7bb4..3b88c40a 100644
--- a/include/drawtypes/label.hpp
+++ b/include/drawtypes/label.hpp
@@ -1,5 +1,7 @@
#pragma once
+#include
+
#include "common.hpp"
#include "components/config.hpp"
#include "components/types.hpp"
@@ -37,6 +39,14 @@ namespace drawtypes {
int m_font{0};
side_values m_padding{0U,0U};
side_values m_margin{0U,0U};
+
+ /*
+ * If m_ellipsis is true, m_maxlen MUST be larger or equal to the length of
+ * the ellipsis (3), everything else is a programming error
+ *
+ * load_label should take care of this, but be aware, if you are creating
+ * labels in a different way.
+ */
size_t m_maxlen{0_z};
bool m_ellipsis{true};
@@ -55,7 +65,9 @@ namespace drawtypes {
, m_ellipsis(ellipsis)
, m_text(text)
, m_tokenized(m_text)
- , m_tokens(forward>(tokens)) {}
+ , m_tokens(forward>(tokens)) {
+ assert(!m_ellipsis || (m_maxlen == 0 || m_maxlen >= 3));
+ }
string get() const;
operator bool();
diff --git a/src/components/builder.cpp b/src/components/builder.cpp
index 6e2a7e73..f37cb46b 100644
--- a/src/components/builder.cpp
+++ b/src/components/builder.cpp
@@ -557,14 +557,8 @@ string builder::get_label_text(const label_t& label) {
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)");
- }
-
+ if (maxlen > 0 && string_util::char_len(text) > maxlen) {
+ if (label->m_ellipsis) {
text = string_util::utf8_truncate(std::move(text), maxlen - 3) + "...";
}
else {
diff --git a/src/drawtypes/label.cpp b/src/drawtypes/label.cpp
index a907f7c3..63616d31 100644
--- a/src/drawtypes/label.cpp
+++ b/src/drawtypes/label.cpp
@@ -221,6 +221,17 @@ namespace drawtypes {
}
}
+ size_t maxlen = conf.get(section, name + "-maxlen", 0_z);
+ bool ellipsis = conf.get(section, name + "-ellipsis", true);
+
+ if(ellipsis && maxlen > 0 && maxlen < 3) {
+ logger::make().err(sstream() << "Label " << section << "." << name
+ << " has maxlen " << maxlen
+ << ", which is smaller than length of ellipsis (3), disabling ellipsis...");
+
+ ellipsis = false;
+ }
+
// clang-format off
return factory_util::shared