refactor: Cleanup handling of syntax tags

This commit is contained in:
Michael Carlberg
2016-11-24 19:24:47 +01:00
parent 199a825494
commit 84d58e7619
25 changed files with 863 additions and 716 deletions

View File

@ -6,41 +6,57 @@
POLYBAR_NS
void builder::set_lazy(bool mode) {
m_lazy = mode;
}
/**
* Flush contents of the builder and return built string
*
* This will also close any unclosed tags
*/
string builder::flush() {
while (m_counters[syntaxtag::A] > 0) cmd_close(true);
while (m_counters[syntaxtag::B] > 0) background_close(true);
while (m_counters[syntaxtag::F] > 0) color_close(true);
while (m_counters[syntaxtag::T] > 0) font_close(true);
while (m_counters[syntaxtag::Uo] > 0) overline_color_close(true);
while (m_counters[syntaxtag::Uu] > 0) underline_color_close(true);
while (m_counters[syntaxtag::U] > 0) line_color_close(true);
while (m_counters[syntaxtag::u] > 0) underline_close(true);
while (m_counters[syntaxtag::o] > 0) overline_close(true);
if (m_tags[syntaxtag::A])
cmd_close();
if (m_tags[syntaxtag::B])
background_close();
if (m_tags[syntaxtag::F])
color_close();
if (m_tags[syntaxtag::T])
font_close();
if (m_tags[syntaxtag::o])
overline_color_close();
if (m_tags[syntaxtag::u])
underline_color_close();
if ((m_attributes >> static_cast<uint8_t>(attribute::UNDERLINE)) & 1U)
underline_close();
if ((m_attributes >> static_cast<uint8_t>(attribute::OVERLINE)) & 1U)
overline_close();
string output = m_output.data();
// reset values
m_output.clear();
for (auto& counter : m_counters) counter.second = 0;
for (auto& counter : m_tags) counter.second = 0;
for (auto& value : m_colors) value.second = "";
m_output.clear();
m_fontindex = 1;
return string_util::replace_all(output, string{BUILDER_SPACE_TOKEN}, " ");
}
/**
* Insert raw text string
*/
void builder::append(string text) {
string str(text);
auto len = str.length();
size_t len{str.length()};
if (len > 2 && str[0] == '"' && str[len - 1] == '"')
m_output += str.substr(1, len - 2);
else
m_output += str;
}
/**
* Insert text node
*
* This will also parse raw syntax tags
*/
void builder::node(string str, bool add_space) {
string::size_type n, m;
string s(str);
@ -50,7 +66,7 @@ void builder::node(string str, bool add_space) {
break;
} else if ((n = s.find("%{F-}")) == 0) {
color_close(true);
color_close();
s.erase(0, 5);
} else if ((n = s.find("%{F#")) == 0 && (m = s.find("}")) != string::npos) {
@ -61,7 +77,7 @@ void builder::node(string str, bool add_space) {
s.erase(n, m + 1);
} else if ((n = s.find("%{B-}")) == 0) {
background_close(true);
background_close();
s.erase(0, 5);
} else if ((n = s.find("%{B#")) == 0 && (m = s.find("}")) != string::npos) {
@ -69,7 +85,7 @@ void builder::node(string str, bool add_space) {
s.erase(n, m + 1);
} else if ((n = s.find("%{T-}")) == 0) {
font_close(true);
font_close();
s.erase(0, 5);
} else if ((n = s.find("%{T")) == 0 && (m = s.find("}")) != string::npos) {
@ -77,23 +93,23 @@ void builder::node(string str, bool add_space) {
s.erase(n, m + 1);
} else if ((n = s.find("%{U-}")) == 0) {
line_color_close(true);
line_color_close();
s.erase(0, 5);
} else if ((n = s.find("%{Uu-}")) == 0) {
underline_color_close(true);
s.erase(0, 6);
} else if ((n = s.find("%{u-}")) == 0) {
underline_color_close();
s.erase(0, 5);
} else if ((n = s.find("%{Uo-}")) == 0) {
overline_color_close(true);
s.erase(0, 6);
} else if ((n = s.find("%{o-}")) == 0) {
overline_color_close();
s.erase(0, 5);
} else if ((n = s.find("%{Uu#")) == 0 && (m = s.find("}")) != string::npos) {
underline_color(s.substr(n + 4, m - 4));
} else if ((n = s.find("%{u#")) == 0 && (m = s.find("}")) != string::npos) {
underline_color(s.substr(n + 3, m - 3));
s.erase(n, m + 1);
} else if ((n = s.find("%{Uo#")) == 0 && (m = s.find("}")) != string::npos) {
overline_color(s.substr(n + 4, m - 4));
} else if ((n = s.find("%{o#")) == 0 && (m = s.find("}")) != string::npos) {
overline_color(s.substr(n + 3, m - 3));
s.erase(n, m + 1);
} else if ((n = s.find("%{U#")) == 0 && (m = s.find("}")) != string::npos) {
@ -109,15 +125,15 @@ void builder::node(string str, bool add_space) {
s.erase(0, 5);
} else if ((n = s.find("%{-u}")) == 0) {
underline_close(true);
underline_close();
s.erase(0, 5);
} else if ((n = s.find("%{-o}")) == 0) {
overline_close(true);
overline_close();
s.erase(0, 5);
} else if ((n = s.find("%{A}")) == 0) {
cmd_close(true);
cmd_close();
s.erase(0, 4);
} else if ((n = s.find("%{")) == 0 && (m = s.find("}")) != string::npos) {
@ -138,35 +154,36 @@ void builder::node(string str, bool add_space) {
space();
}
/**
* Insert text node with specific font index
*
* @see builder::node
*/
void builder::node(string str, int font_index, bool add_space) {
font(font_index);
node(str, add_space);
font_close();
}
// void builder::node(progressbar_t bar, float perc, bool add_space) {
// if (!bar)
// return;
// node(bar->get_output(math_util::cap<float>(0, 100, perc)), add_space);
// }
/**
* Insert tags for given label
*/
void builder::node(label_t label, bool add_space) {
if (!label || !*label)
return;
auto text = label->get();
string text{label->get()};
if (label->m_maxlen > 0 && text.length() > label->m_maxlen) {
text = text.substr(0, label->m_maxlen) + "...";
}
if ((label->m_overline.empty() && m_counters[syntaxtag::o] > 0) ||
(m_counters[syntaxtag::o] > 0 && label->m_margin > 0))
overline_close(true);
if ((label->m_underline.empty() && m_counters[syntaxtag::u] > 0) ||
(m_counters[syntaxtag::u] > 0 && label->m_margin > 0))
underline_close(true);
// if ((label->m_overline.empty() && m_tags[syntaxtag::o] > 0) || (m_tags[syntaxtag::o] > 0 && label->m_margin > 0))
// overline_close();
// if ((label->m_underline.empty() && m_tags[syntaxtag::u] > 0) || (m_tags[syntaxtag::u] > 0 && label->m_margin > 0))
// underline_close();
// TODO: Replace with margin-left
if (label->m_margin > 0)
space(label->m_margin);
@ -175,46 +192,48 @@ void builder::node(label_t label, bool add_space) {
if (!label->m_underline.empty())
underline(label->m_underline);
background(label->m_background);
color(label->m_foreground);
if (!label->m_background.empty())
background(label->m_background);
if (!label->m_foreground.empty())
color(label->m_foreground);
// TODO: Replace with padding-left
if (label->m_padding > 0)
space(label->m_padding);
node(text, label->m_font, add_space);
// TODO: Replace with padding-right
if (label->m_padding > 0)
space(label->m_padding);
color_close(m_lazy && label->m_margin > 0);
background_close(m_lazy && label->m_margin > 0);
if (!label->m_background.empty())
background_close();
if (!label->m_foreground.empty())
color_close();
if (!label->m_underline.empty() || (label->m_margin > 0 && m_counters[syntaxtag::u] > 0))
underline_close(m_lazy && label->m_margin > 0);
if (!label->m_overline.empty() || (label->m_margin > 0 && m_counters[syntaxtag::o] > 0))
overline_close(m_lazy && label->m_margin > 0);
if (!label->m_underline.empty() || (label->m_margin > 0 && m_tags[syntaxtag::u] > 0))
underline_close();
if (!label->m_overline.empty() || (label->m_margin > 0 && m_tags[syntaxtag::o] > 0))
overline_close();
// TODO: Replace with margin-right
if (label->m_margin > 0)
space(label->m_margin);
}
// void builder::node(ramp_t ramp, float perc, bool add_space) {
// if (!ramp)
// return;
// node(ramp->get_by_percentage(math_util::cap<float>(0, 100, perc)), add_space);
// }
// void builder::node(animation_t animation, bool add_space) {
// if (!animation)
// return;
// node(animation->get(), add_space);
// }
/**
* Insert tag that will offset the contents by given pixels
*/
void builder::offset(int pixels) {
if (pixels != 0)
tag_open('O', std::to_string(pixels));
if (pixels == 0)
return;
tag_open(syntaxtag::O, to_string(pixels));
}
/**
* Insert spaces
*/
void builder::space(int width) {
if (width == DEFAULT_SPACING)
width = m_bar.spacing;
@ -224,6 +243,9 @@ void builder::space(int width) {
append(str);
}
/**
* Remove trailing space
*/
void builder::remove_trailing_space(int width) {
if (width == DEFAULT_SPACING)
width = m_bar.spacing;
@ -235,87 +257,70 @@ void builder::remove_trailing_space(int width) {
m_output = m_output.substr(0, m_output.length() - spacing);
}
void builder::invert() {
tag_open('R', "");
}
/**
* Insert tag to alter the current font index
*/
void builder::font(int index) {
if (index <= 0 && m_counters[syntaxtag::T] > 0)
font_close(true);
if (index <= 0 || index == m_fontindex)
return;
if (m_lazy && m_counters[syntaxtag::T] > 0)
font_close(true);
m_counters[syntaxtag::T]++;
m_fontindex = index;
tag_open('T', std::to_string(index));
tag_open(syntaxtag::T, to_string(index));
}
void builder::font_close(bool force) {
if ((!force && m_lazy) || m_counters[syntaxtag::T] <= 0)
return;
m_counters[syntaxtag::T]--;
/**
* Insert tag to reset the font index
*/
void builder::font_close() {
m_fontindex = 1;
tag_close('T');
tag_close(syntaxtag::T);
}
/**
* Insert tag to alter the current background color
*/
void builder::background(string color) {
if (color.length() == 2 || (color.find("#") == 0 && color.length() == 3)) {
string bg{background_hex()};
color = "#" + color.substr(color.length() - 2);
auto bg = color_util::hex<uint16_t>(m_bar.background);
color += bg.substr(bg.length() - (bg.length() < 6 ? 3 : 6));
} else if (color.length() >= 7 && color == "#" + string(color.length() - 1, color[1])) {
color = color.substr(0, 4);
}
if (color.empty() && m_counters[syntaxtag::B] > 0)
background_close(true);
if (color.empty() || color == m_colors[syntaxtag::B])
return;
if (m_lazy && m_counters[syntaxtag::B] > 0)
background_close(true);
m_counters[syntaxtag::B]++;
color = color_util::simplify_hex(color);
m_colors[syntaxtag::B] = color;
tag_open('B', color);
tag_open(syntaxtag::B, color);
}
void builder::background_close(bool force) {
if ((!force && m_lazy) || m_counters[syntaxtag::B] <= 0)
return;
m_counters[syntaxtag::B]--;
/**
* Insert tag to reset the background color
*/
void builder::background_close() {
m_colors[syntaxtag::B] = "";
tag_close('B');
tag_close(syntaxtag::B);
}
void builder::color(string color_) {
auto color(color_);
/**
* Insert tag to alter the current foreground color
*/
void builder::color(string color) {
if (color.length() == 2 || (color.find("#") == 0 && color.length() == 3)) {
string fg{foreground_hex()};
color = "#" + color.substr(color.length() - 2);
auto fg = color_util::hex<uint16_t>(m_bar.foreground);
color += fg.substr(fg.length() - (fg.length() < 6 ? 3 : 6));
} else if (color.length() >= 7 && color == "#" + string(color.length() - 1, color[1])) {
color = color.substr(0, 4);
}
if (color.empty() && m_counters[syntaxtag::F] > 0)
color_close(true);
if (color.empty() || color == m_colors[syntaxtag::F])
return;
if (m_lazy && m_counters[syntaxtag::F] > 0)
color_close(true);
m_counters[syntaxtag::F]++;
color = color_util::simplify_hex(color);
m_colors[syntaxtag::F] = color;
tag_open('F', color);
tag_open(syntaxtag::F, color);
}
void builder::color_alpha(string alpha_) {
auto alpha(alpha_);
string val = color_util::hex<uint16_t>(m_bar.foreground);
/**
* Insert tag to alter the alpha value of the default foreground color
*/
void builder::color_alpha(string alpha) {
string val{foreground_hex()};
if (alpha.find("#") == std::string::npos) {
alpha = "#" + alpha;
}
@ -332,117 +337,103 @@ void builder::color_alpha(string alpha_) {
color((alpha.substr(0, 3) + val.substr(val.size() - 6)).substr(0, 9));
}
void builder::color_close(bool force) {
if ((!force && m_lazy) || m_counters[syntaxtag::F] <= 0)
return;
m_counters[syntaxtag::F]--;
/**
* Insert tag to reset the foreground color
*/
void builder::color_close() {
m_colors[syntaxtag::F] = "";
tag_close('F');
tag_close(syntaxtag::F);
}
/**
* Insert tag to alter the current overline/underline color
*/
void builder::line_color(string color) {
if (color.empty() && m_counters[syntaxtag::U] > 0)
line_color_close(true);
if (color.empty() || color == m_colors[syntaxtag::U])
return;
if (m_lazy && m_counters[syntaxtag::U] > 0)
line_color_close(true);
m_counters[syntaxtag::U]++;
m_colors[syntaxtag::U] = color;
tag_open('U', color);
overline_color(color);
underline_color(color);
}
void builder::line_color_close(bool force) {
if ((!force && m_lazy) || m_counters[syntaxtag::U] <= 0)
return;
m_counters[syntaxtag::U]--;
m_colors[syntaxtag::U] = "";
tag_close('U');
/**
* Close overline/underline color tag
*/
void builder::line_color_close() {
overline_color_close();
underline_color_close();
}
/**
* Insert tag to alter the current overline color
*/
void builder::overline_color(string color) {
if (color.empty() && m_counters[syntaxtag::Uo] > 0)
overline_color_close(true);
if (color.empty() || color == m_colors[syntaxtag::Uo])
return;
if (m_lazy && m_counters[syntaxtag::Uo] > 0)
overline_color_close(true);
m_counters[syntaxtag::Uo]++;
m_colors[syntaxtag::Uo] = color;
append("%{Uo" + color + "}");
color = color_util::simplify_hex(color);
m_colors[syntaxtag::o] = color;
tag_open(syntaxtag::o, color);
tag_open(attribute::OVERLINE);
}
void builder::overline_color_close(bool force) {
if ((!force && m_lazy) || m_counters[syntaxtag::Uo] <= 0)
return;
m_counters[syntaxtag::Uo]--;
m_colors[syntaxtag::Uo] = "";
append("%{Uu-}");
/**
* Close underline color tag
*/
void builder::overline_color_close() {
m_colors[syntaxtag::o] = "";
tag_close(syntaxtag::o);
}
/**
* Insert tag to alter the current underline color
*/
void builder::underline_color(string color) {
if (color.empty() && m_counters[syntaxtag::Uu] > 0)
underline_color_close(true);
if (color.empty() || color == m_colors[syntaxtag::Uu])
return;
if (m_lazy && m_counters[syntaxtag::Uu] > 0)
underline_color_close(true);
m_counters[syntaxtag::Uu]++;
m_colors[syntaxtag::Uu] = color;
append("%{Uu" + color + "}");
color = color_util::simplify_hex(color);
m_colors[syntaxtag::u] = color;
tag_open(syntaxtag::u, color);
tag_open(attribute::UNDERLINE);
}
void builder::underline_color_close(bool force) {
if ((!force && m_lazy) || m_counters[syntaxtag::Uu] <= 0)
return;
m_counters[syntaxtag::Uu]--;
m_colors[syntaxtag::Uu] = "";
append("%{Uu-}");
/**
* Close underline color tag
*/
void builder::underline_color_close() {
tag_close(syntaxtag::u);
m_colors[syntaxtag::u] = "";
}
/**
* Insert tag to enable the overline attribute
*/
void builder::overline(string color) {
if (!color.empty())
overline_color(color);
if (m_counters[syntaxtag::o] > 0)
return;
m_counters[syntaxtag::o]++;
append("%{+o}");
else
tag_open(attribute::OVERLINE);
}
void builder::overline_close(bool force) {
if ((!force && m_lazy) || m_counters[syntaxtag::o] <= 0)
return;
m_counters[syntaxtag::o]--;
append("%{-o}");
/**
* Close overline attribute tag
*/
void builder::overline_close() {
tag_close(attribute::OVERLINE);
}
/**
* Insert tag to enable the underline attribute
*/
void builder::underline(string color) {
if (!color.empty())
underline_color(color);
if (m_counters[syntaxtag::u] > 0)
return;
m_counters[syntaxtag::u]++;
append("%{+u}");
else
tag_open(attribute::UNDERLINE);
}
void builder::underline_close(bool force) {
if ((!force && m_lazy) || m_counters[syntaxtag::u] <= 0)
return;
m_counters[syntaxtag::u]--;
append("%{-u}");
/**
* Close underline attribute tag
*/
void builder::underline_close() {
tag_close(attribute::UNDERLINE);
}
/**
* Open command tag
*/
void builder::cmd(mousebtn index, string action, bool condition) {
int button = static_cast<int>(index);
@ -455,23 +446,148 @@ void builder::cmd(mousebtn index, string action, bool condition) {
action = string_util::replace_all(action, "{", "\\{");
action = string_util::replace_all(action, "%", "\x0025");
append("%{A" + std::to_string(button) + ":" + action + ":}");
m_counters[syntaxtag::A]++;
tag_open(syntaxtag::A, to_string(button) + ":" + action + ":");
}
void builder::cmd_close(bool force) {
if (m_counters[syntaxtag::A] > 0 || force)
append("%{A}");
if (m_counters[syntaxtag::A] > 0)
m_counters[syntaxtag::A]--;
/**
* Close command tag
*/
void builder::cmd_close() {
tag_close(syntaxtag::A);
}
void builder::tag_open(char tag, string value) {
append("%{" + string({tag}) + value + "}");
/**
* Get default background hex string
*/
string builder::background_hex() {
if (m_background.empty())
m_background = color_util::hex<uint16_t>(m_bar.background);
return m_background;
}
void builder::tag_close(char tag) {
append("%{" + string({tag}) + "-}");
/**
* Get default foreground hex string
*/
string builder::foreground_hex() {
if (m_foreground.empty())
m_foreground = color_util::hex<uint16_t>(m_bar.foreground);
return m_foreground;
}
/**
* Insert directive to change value of given tag
*/
void builder::tag_open(syntaxtag tag, string value) {
if (m_tags.find(tag) != m_tags.end())
m_tags[tag]++;
switch (tag) {
case syntaxtag::NONE:
break;
case syntaxtag::A:
append("%{A" + value + "}");
break;
case syntaxtag::F:
append("%{F" + value + "}");
break;
case syntaxtag::B:
append("%{B" + value + "}");
break;
case syntaxtag::T:
append("%{T" + value + "}");
break;
case syntaxtag::u:
append("%{u" + value + "}");
break;
case syntaxtag::o:
append("%{o" + value + "}");
break;
case syntaxtag::R:
append("%{R}");
break;
case syntaxtag::O:
append("%{O" + value + "}");
break;
}
}
/**
* Insert directive to use given attribute unless already set
*/
void builder::tag_open(attribute attr) {
if ((m_attributes >> static_cast<uint8_t>(attr)) & 1U)
return;
m_attributes |= 1U << static_cast<uint8_t>(attr);
switch (attr) {
case attribute::NONE:
break;
case attribute::UNDERLINE:
append("%{+u}");
break;
case attribute::OVERLINE:
append("%{+o}");
break;
}
}
/**
* Insert directive to reset given tag if it's open and closable
*/
void builder::tag_close(syntaxtag tag) {
if (!m_tags[tag] || m_tags.find(tag) == m_tags.end())
return;
m_tags[tag]--;
switch (tag) {
case syntaxtag::NONE:
break;
case syntaxtag::A:
append("%{A}");
break;
case syntaxtag::F:
append("%{F-}");
break;
case syntaxtag::B:
append("%{B-}");
break;
case syntaxtag::T:
append("%{T-}");
break;
case syntaxtag::u:
append("%{u-}");
break;
case syntaxtag::o:
append("%{o-}");
break;
case syntaxtag::R:
break;
case syntaxtag::O:
break;
}
}
/**
* Insert directive to remove given attribute if set
*/
void builder::tag_close(attribute attr) {
if (!((m_attributes >> static_cast<uint8_t>(attr)) & 1U))
return;
m_attributes &= ~(1U << static_cast<uint8_t>(attr));
switch (attr) {
case attribute::NONE:
break;
case attribute::UNDERLINE:
append("%{-u}");
break;
case attribute::OVERLINE:
append("%{-o}");
break;
}
}
POLYBAR_NS_END