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)
This commit is contained in:
Michael Carlberg 2016-11-30 22:17:52 +01:00
parent be8805be1e
commit d34263d850
4 changed files with 48 additions and 12 deletions

View File

@ -32,6 +32,8 @@ class builder {
void node(string str, bool add_space = false); void node(string str, bool add_space = false);
void node(string str, int font_index, 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(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 offset(int pixels = 0);
void space(int width = DEFAULT_SPACING); void space(int width = DEFAULT_SPACING);
void remove_trailing_space(int width = DEFAULT_SPACING); void remove_trailing_space(int width = DEFAULT_SPACING);

View File

@ -102,7 +102,7 @@ void builder::node(string str, bool add_space) {
s.erase(0, 5); s.erase(0, 5);
} else if ((n = s.find("%{T")) == 0 && (m = s.find('}')) != string::npos) { } 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); s.erase(n, m + 1);
} else if ((n = s.find("%{U-}")) == 0) { } 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 * 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 * Insert tag to alter the current font index
*/ */
void builder::font(int index) { void builder::font(int index) {
if (index == 0) {
return;
}
m_fontindex = index; m_fontindex = index;
tag_open(syntaxtag::T, to_string(index)); tag_open(syntaxtag::T, to_string(index));
} }
@ -356,7 +383,7 @@ void builder::color(string color) {
void builder::color_alpha(string alpha) { void builder::color_alpha(string alpha) {
string val{foreground_hex()}; string val{foreground_hex()};
if (alpha.find('#') == std::string::npos) { if (alpha.find('#') == string::npos) {
alpha = "#" + alpha; alpha = "#" + alpha;
} }

View File

@ -52,6 +52,19 @@ namespace drawtypes {
if (!label->m_overline.empty()) { if (!label->m_overline.empty()) {
m_overline = label->m_overline; 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) { void label::copy_undefined(const label_t& label) {

View File

@ -58,9 +58,7 @@ namespace drawtypes {
output = string_util::replace_all(output, "%indicator%", m_builder->flush()); output = string_util::replace_all(output, "%indicator%", m_builder->flush());
// Output empty icons // Output empty icons
while (empty_width--) { m_builder->node_repeat(m_empty, empty_width);
m_builder->node(m_empty);
}
output = string_util::replace_all(output, "%empty%", m_builder->flush()); output = string_util::replace_all(output, "%empty%", m_builder->flush());
return output; return output;
@ -68,23 +66,19 @@ namespace drawtypes {
void progressbar::fill(unsigned int perc, unsigned int fill_width) { void progressbar::fill(unsigned int perc, unsigned int fill_width) {
if (m_colors.empty()) { if (m_colors.empty()) {
for (size_t i = 0; i < fill_width; i++) { m_builder->node_repeat(m_fill, fill_width);
m_builder->node(m_fill);
}
} else if (m_gradient) { } else if (m_gradient) {
size_t color = 0; size_t color = 0;
for (size_t i = 0; i < fill_width; i++) { for (size_t i = 0; i < fill_width; i++) {
if (i % m_colorstep == 0 && color < m_colors.size()) { if (i % m_colorstep == 0 && color < m_colors.size()) {
m_fill->m_foreground = m_colors[color++]; m_fill->m_foreground = m_colors[color++];
} }
m_builder->node(m_fill); m_builder->node(m_fill->get());
} }
} else { } else {
size_t color = math_util::percentage_to_value<size_t>(perc, m_colors.size() - 1); size_t color = math_util::percentage_to_value<size_t>(perc, m_colors.size() - 1);
m_fill->m_foreground = m_colors[color]; m_fill->m_foreground = m_colors[color];
for (size_t i = 0; i < fill_width; i++) { m_builder->node_repeat(m_fill, fill_width);
m_builder->node(m_fill);
}
} }
} }