From 4df725aab9fe7c7bee42be40789c265a162cb517 Mon Sep 17 00:00:00 2001 From: M1dgard Date: Tue, 29 May 2018 15:35:11 +0200 Subject: [PATCH] feat(builder) Add minlen The counterpart of maxlen, that forces a minimum length on a string. Pads right with spaces if a string is too short. --- include/drawtypes/label.hpp | 5 ++++- src/components/builder.cpp | 11 ++++++++++- src/drawtypes/label.cpp | 19 ++++++++++++++++++- 3 files changed, 32 insertions(+), 3 deletions(-) diff --git a/include/drawtypes/label.hpp b/include/drawtypes/label.hpp index 3b88c40a..bf127038 100644 --- a/include/drawtypes/label.hpp +++ b/include/drawtypes/label.hpp @@ -49,11 +49,12 @@ namespace drawtypes { */ size_t m_maxlen{0_z}; bool m_ellipsis{true}; + size_t m_minlen{0_z}; explicit label(string text, int font) : m_font(font), m_text(text), m_tokenized(m_text) {} explicit label(string text, string foreground = ""s, string background = ""s, string underline = ""s, string overline = ""s, int font = 0, struct side_values padding = {0U,0U}, struct side_values margin = {0U,0U}, - size_t maxlen = 0_z, bool ellipsis = true, vector&& tokens = {}) + size_t maxlen = 0_z, bool ellipsis = true, size_t minlen = 0_z, vector&& tokens = {}) : m_foreground(foreground) , m_background(background) , m_underline(underline) @@ -63,10 +64,12 @@ namespace drawtypes { , m_margin(margin) , m_maxlen(maxlen) , m_ellipsis(ellipsis) + , m_minlen(minlen) , m_text(text) , m_tokenized(m_text) , m_tokens(forward>(tokens)) { assert(!m_ellipsis || (m_maxlen == 0 || m_maxlen >= 3)); + assert(!m_ellipsis || m_minlen <= m_maxlen); } string get() const; diff --git a/src/components/builder.cpp b/src/components/builder.cpp index 0aa1f101..cb15d850 100644 --- a/src/components/builder.cpp +++ b/src/components/builder.cpp @@ -559,9 +559,12 @@ string builder::foreground_hex() { string builder::get_label_text(const label_t& label) { string text{label->get()}; + size_t len = string_util::char_len(text); size_t maxlen = label->m_maxlen; + size_t minlen = label->m_minlen; - if (maxlen > 0 && string_util::char_len(text) > maxlen) { + // Truncate if too long + if (maxlen > 0 && len > maxlen) { if (label->m_ellipsis) { text = string_util::utf8_truncate(std::move(text), maxlen - 3) + "..."; } @@ -570,6 +573,12 @@ string builder::get_label_text(const label_t& label) { } } + // Pad right if too short + if (len < minlen) { + std::string s = "123"; + text.insert(text.end(), minlen - len, ' '); + } + return text; } diff --git a/src/drawtypes/label.cpp b/src/drawtypes/label.cpp index 63616d31..a0f86726 100644 --- a/src/drawtypes/label.cpp +++ b/src/drawtypes/label.cpp @@ -22,7 +22,7 @@ namespace drawtypes { std::copy(m_tokens.begin(), m_tokens.end(), back_it); } return factory_util::shared