From d34263d8501684c5dd1a2b9e9f6235f805846198 Mon Sep 17 00:00:00 2001 From: Michael Carlberg Date: Wed, 30 Nov 2016 22:17:52 +0100 Subject: [PATCH] fix(progressbar): Generate less data Build the format sub-strings (%fill%, %indicator%, %empty%) before adding it to the builder to avoid having it generate alot of duplicate content (tags, etc) --- include/components/builder.hpp | 2 ++ src/components/builder.cpp | 31 +++++++++++++++++++++++++++++-- src/drawtypes/label.cpp | 13 +++++++++++++ src/drawtypes/progressbar.cpp | 14 ++++---------- 4 files changed, 48 insertions(+), 12 deletions(-) diff --git a/include/components/builder.hpp b/include/components/builder.hpp index 965d6666..fce6d359 100644 --- a/include/components/builder.hpp +++ b/include/components/builder.hpp @@ -32,6 +32,8 @@ class builder { void node(string str, bool add_space = false); void node(string str, int font_index, bool add_space = false); void node(const label_t& label, bool add_space = false); + void node_repeat(string str, size_t n, bool add_space = false); + void node_repeat(const label_t& label, size_t n, bool add_space = false); void offset(int pixels = 0); void space(int width = DEFAULT_SPACING); void remove_trailing_space(int width = DEFAULT_SPACING); diff --git a/src/components/builder.cpp b/src/components/builder.cpp index 3af97d78..4050d4c7 100644 --- a/src/components/builder.cpp +++ b/src/components/builder.cpp @@ -102,7 +102,7 @@ void builder::node(string str, bool add_space) { s.erase(0, 5); } else if ((n = s.find("%{T")) == 0 && (m = s.find('}')) != string::npos) { - font(std::atoi(s.substr(n + 3, m - 3).c_str())); + font(atoi(s.substr(n + 3, m - 3).c_str())); s.erase(n, m + 1); } else if ((n = s.find("%{U-}")) == 0) { @@ -251,6 +251,30 @@ void builder::node(const label_t& label, bool add_space) { } } +/** + * Repeat text string n times + */ +void builder::node_repeat(string str, size_t n, bool add_space) { + string text; + while (n--) { + text += str; + } + node(text, add_space); +} + +/** + * Repeat label contents n times + */ +void builder::node_repeat(const label_t& label, size_t n, bool add_space) { + string text; + while (n--) { + text += label->get(); + } + label_t tmp{new label_t::element_type{text}}; + tmp->replace_defined_values(label); + node(tmp, add_space); +} + /** * Insert tag that will offset the contents by given pixels */ @@ -296,6 +320,9 @@ void builder::remove_trailing_space(int width) { * Insert tag to alter the current font index */ void builder::font(int index) { + if (index == 0) { + return; + } m_fontindex = index; tag_open(syntaxtag::T, to_string(index)); } @@ -356,7 +383,7 @@ void builder::color(string color) { void builder::color_alpha(string alpha) { string val{foreground_hex()}; - if (alpha.find('#') == std::string::npos) { + if (alpha.find('#') == string::npos) { alpha = "#" + alpha; } diff --git a/src/drawtypes/label.cpp b/src/drawtypes/label.cpp index 2982e461..b5edf264 100644 --- a/src/drawtypes/label.cpp +++ b/src/drawtypes/label.cpp @@ -52,6 +52,19 @@ namespace drawtypes { if (!label->m_overline.empty()) { m_overline = label->m_overline; } + if (label->m_font != 0) { + m_font = label->m_font; + } + if (label->m_padding != 0) { + m_padding = label->m_padding; + } + if (label->m_margin != 0) { + m_margin = label->m_margin; + } + if (label->m_maxlen != 0) { + m_maxlen = label->m_maxlen; + m_ellipsis = label->m_ellipsis; + } } void label::copy_undefined(const label_t& label) { diff --git a/src/drawtypes/progressbar.cpp b/src/drawtypes/progressbar.cpp index 54aab378..45b430b1 100644 --- a/src/drawtypes/progressbar.cpp +++ b/src/drawtypes/progressbar.cpp @@ -58,9 +58,7 @@ namespace drawtypes { output = string_util::replace_all(output, "%indicator%", m_builder->flush()); // Output empty icons - while (empty_width--) { - m_builder->node(m_empty); - } + m_builder->node_repeat(m_empty, empty_width); output = string_util::replace_all(output, "%empty%", m_builder->flush()); return output; @@ -68,23 +66,19 @@ namespace drawtypes { void progressbar::fill(unsigned int perc, unsigned int fill_width) { if (m_colors.empty()) { - for (size_t i = 0; i < fill_width; i++) { - m_builder->node(m_fill); - } + m_builder->node_repeat(m_fill, fill_width); } else if (m_gradient) { size_t color = 0; for (size_t i = 0; i < fill_width; i++) { if (i % m_colorstep == 0 && color < m_colors.size()) { m_fill->m_foreground = m_colors[color++]; } - m_builder->node(m_fill); + m_builder->node(m_fill->get()); } } else { size_t color = math_util::percentage_to_value(perc, m_colors.size() - 1); m_fill->m_foreground = m_colors[color]; - for (size_t i = 0; i < fill_width; i++) { - m_builder->node(m_fill); - } + m_builder->node_repeat(m_fill, fill_width); } }