From fb6e874235466a59be66bbad685f4c58ae8bb1b7 Mon Sep 17 00:00:00 2001 From: infokiller Date: Sun, 1 Dec 2019 02:28:41 +0200 Subject: [PATCH] feat(label): Add minlen with alignment (#1546) * Add label minlen and alignment. Fix build * Update src/drawtypes/label.cpp Co-Authored-By: infokiller * Use existing alignment type. * Remove redundant max_len handling in label::get. * Fix shadowing. * Add label alignment tests. * Handle minlen/maxlen and alignment in same function. Also add a test for a test case brought up in the PR discussion. * Format files with clang-format * Move builder::get_label_text tests into label tests builder::get_label_text doesn't really do anything anymore * builder: remove get_label_text * label: Clean up label::get() * Fix comment style. * Set default label alignment to left. * Update src/drawtypes/label.cpp Co-Authored-By: Patrick Ziegler * Update include/drawtypes/label.hpp Co-Authored-By: Patrick Ziegler --- include/components/builder.hpp | 2 - include/drawtypes/label.hpp | 19 +++-- src/components/builder.cpp | 21 +---- src/drawtypes/label.cpp | 70 +++++++++++++-- tests/CMakeLists.txt | 2 +- tests/unit_tests/components/builder.cpp | 68 --------------- tests/unit_tests/drawtypes/label.cpp | 109 ++++++++++++++++++++++++ 7 files changed, 186 insertions(+), 105 deletions(-) delete mode 100644 tests/unit_tests/components/builder.cpp create mode 100644 tests/unit_tests/drawtypes/label.cpp diff --git a/include/components/builder.hpp b/include/components/builder.hpp index a14d55f0..38c7e69e 100644 --- a/include/components/builder.hpp +++ b/include/components/builder.hpp @@ -55,8 +55,6 @@ class builder { string background_hex(); string foreground_hex(); - string get_label_text(const label_t& label); - void tag_open(syntaxtag tag, const string& value); void tag_open(attribute attr); void tag_close(syntaxtag tag); diff --git a/include/drawtypes/label.hpp b/include/drawtypes/label.hpp index 5859b716..86b2173f 100644 --- a/include/drawtypes/label.hpp +++ b/include/drawtypes/label.hpp @@ -25,9 +25,10 @@ namespace drawtypes { string m_underline{}; string m_overline{}; int m_font{0}; - side_values m_padding{0U,0U}; - side_values m_margin{0U,0U}; + side_values m_padding{0U, 0U}; + side_values m_margin{0U, 0U}; + size_t m_minlen{0}; /* * 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 @@ -36,12 +37,14 @@ namespace drawtypes { * labels in a different way. */ size_t m_maxlen{0_z}; + alignment m_alignment{alignment::LEFT}; bool m_ellipsis{true}; 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 = {}) + string overline = ""s, int font = 0, struct side_values padding = {0U, 0U}, + struct side_values margin = {0U, 0U}, int minlen = 0, size_t maxlen = 0_z, + alignment label_alignment = alignment::LEFT, bool ellipsis = true, vector&& tokens = {}) : m_foreground(foreground) , m_background(background) , m_underline(underline) @@ -49,13 +52,15 @@ namespace drawtypes { , m_font(font) , m_padding(padding) , m_margin(margin) + , m_minlen(minlen) , m_maxlen(maxlen) + , m_alignment(label_alignment) , m_ellipsis(ellipsis) , m_text(text) , m_tokenized(m_text) , m_tokens(forward>(tokens)) { - assert(!m_ellipsis || (m_maxlen == 0 || m_maxlen >= 3)); - } + assert(!m_ellipsis || (m_maxlen == 0 || m_maxlen >= 3)); + } string get() const; operator bool(); @@ -76,6 +81,6 @@ namespace drawtypes { label_t load_label(const config& conf, const string& section, string name, bool required = true, string def = ""s); label_t load_optional_label(const config& conf, string section, string name, string def = ""s); -} +} // namespace drawtypes POLYBAR_NS_END diff --git a/src/components/builder.cpp b/src/components/builder.cpp index 8d220cc3..56e20d44 100644 --- a/src/components/builder.cpp +++ b/src/components/builder.cpp @@ -1,6 +1,7 @@ +#include "components/builder.hpp" + #include -#include "components/builder.hpp" #include "drawtypes/label.hpp" #include "utils/color.hpp" #include "utils/string.hpp" @@ -124,7 +125,7 @@ void builder::node(const label_t& label, bool add_space) { return; } - auto text = get_label_text(label); + auto text = label->get(); if (label->m_margin.left > 0) { space(label->m_margin.left); @@ -479,22 +480,6 @@ string builder::foreground_hex() { return m_foreground; } -string builder::get_label_text(const label_t& label) { - string text{label->get()}; - - size_t maxlen = label->m_maxlen; - - if (maxlen > 0 && string_util::char_len(text) > maxlen) { - if (label->m_ellipsis) { - text = string_util::utf8_truncate(std::move(text), maxlen - 3) + "..."; - } else { - text = string_util::utf8_truncate(std::move(text), maxlen); - } - } - - return text; -} - /** * Insert directive to change value of given tag */ diff --git a/src/drawtypes/label.cpp b/src/drawtypes/label.cpp index 1fee8725..983c6289 100644 --- a/src/drawtypes/label.cpp +++ b/src/drawtypes/label.cpp @@ -1,14 +1,49 @@ +#include "drawtypes/label.hpp" + +#include #include -#include "drawtypes/label.hpp" #include "utils/factory.hpp" #include "utils/string.hpp" POLYBAR_NS namespace drawtypes { + /** + * Gets the text from the label as it should be rendered + * + * Here tokens are replaced with values and minlen and maxlen properties are applied + */ string label::get() const { - return m_tokenized; + const size_t len = string_util::char_len(m_tokenized); + if (len >= m_minlen) { + string text = m_tokenized; + if (m_maxlen > 0 && len > m_maxlen) { + if (m_ellipsis) { + text = string_util::utf8_truncate(std::move(text), m_maxlen - 3) + "..."; + } else { + text = string_util::utf8_truncate(std::move(text), m_maxlen); + } + } + return text; + } + + const size_t num_fill_chars = m_minlen - len; + size_t right_fill_len = 0; + size_t left_fill_len = 0; + if (m_alignment == alignment::RIGHT) { + left_fill_len = num_fill_chars; + } else if (m_alignment == alignment::LEFT) { + right_fill_len = num_fill_chars; + } else { + right_fill_len = std::ceil(num_fill_chars / 2.0); + left_fill_len = right_fill_len; + // The text is positioned one character to the left if we can't perfectly center it + if (len + left_fill_len + right_fill_len > m_minlen) { + --left_fill_len; + } + } + return string(left_fill_len, ' ') + m_tokenized + string(right_fill_len, ' '); } label::operator bool() { @@ -22,7 +57,7 @@ namespace drawtypes { std::copy(m_tokens.begin(), m_tokens.end(), back_it); } return factory_util::shared