2016-11-25 12:55:15 +00:00
|
|
|
#include <utility>
|
|
|
|
|
2016-11-02 19:22:45 +00:00
|
|
|
#include "components/builder.hpp"
|
2016-11-19 14:49:03 +00:00
|
|
|
|
|
|
|
#include "drawtypes/label.hpp"
|
2016-11-02 19:22:45 +00:00
|
|
|
#include "utils/math.hpp"
|
|
|
|
#include "utils/string.hpp"
|
|
|
|
|
2016-11-19 05:22:44 +00:00
|
|
|
POLYBAR_NS
|
2016-11-02 19:22:45 +00:00
|
|
|
|
2016-11-24 18:24:47 +00:00
|
|
|
/**
|
|
|
|
* Flush contents of the builder and return built string
|
|
|
|
*
|
|
|
|
* This will also close any unclosed tags
|
|
|
|
*/
|
2016-11-02 19:22:45 +00:00
|
|
|
string builder::flush() {
|
2016-11-25 12:55:15 +00:00
|
|
|
if (m_tags[syntaxtag::B]) {
|
2016-11-24 18:24:47 +00:00
|
|
|
background_close();
|
2016-11-25 12:55:15 +00:00
|
|
|
}
|
|
|
|
if (m_tags[syntaxtag::F]) {
|
2016-11-24 18:24:47 +00:00
|
|
|
color_close();
|
2016-11-25 12:55:15 +00:00
|
|
|
}
|
|
|
|
if (m_tags[syntaxtag::T]) {
|
2016-11-24 18:24:47 +00:00
|
|
|
font_close();
|
2016-11-25 12:55:15 +00:00
|
|
|
}
|
|
|
|
if (m_tags[syntaxtag::o]) {
|
2016-11-24 18:24:47 +00:00
|
|
|
overline_color_close();
|
2016-11-25 12:55:15 +00:00
|
|
|
}
|
|
|
|
if (m_tags[syntaxtag::u]) {
|
2016-11-24 18:24:47 +00:00
|
|
|
underline_color_close();
|
2016-11-25 12:55:15 +00:00
|
|
|
}
|
|
|
|
if ((m_attributes >> static_cast<uint8_t>(attribute::UNDERLINE)) & 1U) {
|
2016-11-24 18:24:47 +00:00
|
|
|
underline_close();
|
2016-11-25 12:55:15 +00:00
|
|
|
}
|
|
|
|
if ((m_attributes >> static_cast<uint8_t>(attribute::OVERLINE)) & 1U) {
|
2016-11-24 18:24:47 +00:00
|
|
|
overline_close();
|
2016-11-25 12:55:15 +00:00
|
|
|
}
|
2016-11-02 19:22:45 +00:00
|
|
|
|
2016-11-25 03:10:26 +00:00
|
|
|
while (m_tags[syntaxtag::A]) {
|
|
|
|
cmd_close();
|
|
|
|
}
|
|
|
|
|
2016-11-02 19:22:45 +00:00
|
|
|
string output = m_output.data();
|
|
|
|
|
|
|
|
// reset values
|
2016-11-25 03:10:26 +00:00
|
|
|
m_tags.clear();
|
|
|
|
m_colors.clear();
|
2016-11-24 18:24:47 +00:00
|
|
|
m_output.clear();
|
2016-11-02 19:22:45 +00:00
|
|
|
m_fontindex = 1;
|
|
|
|
|
|
|
|
return string_util::replace_all(output, string{BUILDER_SPACE_TOKEN}, " ");
|
|
|
|
}
|
|
|
|
|
2016-11-24 18:24:47 +00:00
|
|
|
/**
|
|
|
|
* Insert raw text string
|
|
|
|
*/
|
2016-11-02 19:22:45 +00:00
|
|
|
void builder::append(string text) {
|
2016-11-25 12:55:15 +00:00
|
|
|
string str(move(text));
|
2016-11-24 18:24:47 +00:00
|
|
|
size_t len{str.length()};
|
2016-11-25 12:55:15 +00:00
|
|
|
if (len > 2 && str[0] == '"' && str[len - 1] == '"') {
|
2016-11-02 19:22:45 +00:00
|
|
|
m_output += str.substr(1, len - 2);
|
2016-11-25 12:55:15 +00:00
|
|
|
} else {
|
2016-11-02 19:22:45 +00:00
|
|
|
m_output += str;
|
2016-11-25 12:55:15 +00:00
|
|
|
}
|
2016-11-02 19:22:45 +00:00
|
|
|
}
|
|
|
|
|
2016-11-24 18:24:47 +00:00
|
|
|
/**
|
|
|
|
* Insert text node
|
|
|
|
*
|
|
|
|
* This will also parse raw syntax tags
|
|
|
|
*/
|
2016-11-02 19:22:45 +00:00
|
|
|
void builder::node(string str, bool add_space) {
|
|
|
|
string::size_type n, m;
|
2016-11-25 12:55:15 +00:00
|
|
|
string s(move(str));
|
2016-11-02 19:22:45 +00:00
|
|
|
|
|
|
|
while (true) {
|
|
|
|
if (s.empty()) {
|
|
|
|
break;
|
|
|
|
|
|
|
|
} else if ((n = s.find("%{F-}")) == 0) {
|
2016-11-24 18:24:47 +00:00
|
|
|
color_close();
|
2016-11-02 19:22:45 +00:00
|
|
|
s.erase(0, 5);
|
|
|
|
|
2016-11-25 12:55:15 +00:00
|
|
|
} else if ((n = s.find("%{F#")) == 0 && (m = s.find('}')) != string::npos) {
|
|
|
|
if (m - n - 4 == 2) {
|
2016-11-02 19:22:45 +00:00
|
|
|
color_alpha(s.substr(n + 3, m - 3));
|
2016-11-25 12:55:15 +00:00
|
|
|
} else {
|
2016-11-02 19:22:45 +00:00
|
|
|
color(s.substr(n + 3, m - 3));
|
2016-11-25 12:55:15 +00:00
|
|
|
}
|
2016-11-02 19:22:45 +00:00
|
|
|
s.erase(n, m + 1);
|
|
|
|
|
|
|
|
} else if ((n = s.find("%{B-}")) == 0) {
|
2016-11-24 18:24:47 +00:00
|
|
|
background_close();
|
2016-11-02 19:22:45 +00:00
|
|
|
s.erase(0, 5);
|
|
|
|
|
2016-11-25 12:55:15 +00:00
|
|
|
} else if ((n = s.find("%{B#")) == 0 && (m = s.find('}')) != string::npos) {
|
2016-11-02 19:22:45 +00:00
|
|
|
background(s.substr(n + 3, m - 3));
|
|
|
|
s.erase(n, m + 1);
|
|
|
|
|
|
|
|
} else if ((n = s.find("%{T-}")) == 0) {
|
2016-11-24 18:24:47 +00:00
|
|
|
font_close();
|
2016-11-02 19:22:45 +00:00
|
|
|
s.erase(0, 5);
|
|
|
|
|
2016-11-25 12:55:15 +00:00
|
|
|
} else if ((n = s.find("%{T")) == 0 && (m = s.find('}')) != string::npos) {
|
2016-11-30 21:17:52 +00:00
|
|
|
font(atoi(s.substr(n + 3, m - 3).c_str()));
|
2016-11-02 19:22:45 +00:00
|
|
|
s.erase(n, m + 1);
|
|
|
|
|
|
|
|
} else if ((n = s.find("%{U-}")) == 0) {
|
2016-11-24 18:24:47 +00:00
|
|
|
line_color_close();
|
2016-11-02 19:22:45 +00:00
|
|
|
s.erase(0, 5);
|
|
|
|
|
2016-11-24 18:24:47 +00:00
|
|
|
} else if ((n = s.find("%{u-}")) == 0) {
|
|
|
|
underline_color_close();
|
|
|
|
s.erase(0, 5);
|
2016-11-21 15:14:40 +00:00
|
|
|
|
2016-11-24 18:24:47 +00:00
|
|
|
} else if ((n = s.find("%{o-}")) == 0) {
|
|
|
|
overline_color_close();
|
|
|
|
s.erase(0, 5);
|
2016-11-21 15:14:40 +00:00
|
|
|
|
2016-11-25 12:55:15 +00:00
|
|
|
} else if ((n = s.find("%{u#")) == 0 && (m = s.find('}')) != string::npos) {
|
2016-11-24 18:24:47 +00:00
|
|
|
underline_color(s.substr(n + 3, m - 3));
|
2016-11-21 15:14:40 +00:00
|
|
|
s.erase(n, m + 1);
|
|
|
|
|
2016-11-25 12:55:15 +00:00
|
|
|
} else if ((n = s.find("%{o#")) == 0 && (m = s.find('}')) != string::npos) {
|
2016-11-24 18:24:47 +00:00
|
|
|
overline_color(s.substr(n + 3, m - 3));
|
2016-11-21 15:14:40 +00:00
|
|
|
s.erase(n, m + 1);
|
|
|
|
|
2016-11-25 12:55:15 +00:00
|
|
|
} else if ((n = s.find("%{U#")) == 0 && (m = s.find('}')) != string::npos) {
|
2016-11-02 19:22:45 +00:00
|
|
|
line_color(s.substr(n + 3, m - 3));
|
|
|
|
s.erase(n, m + 1);
|
|
|
|
|
|
|
|
} else if ((n = s.find("%{+u}")) == 0) {
|
|
|
|
underline();
|
|
|
|
s.erase(0, 5);
|
|
|
|
|
|
|
|
} else if ((n = s.find("%{+o}")) == 0) {
|
|
|
|
overline();
|
|
|
|
s.erase(0, 5);
|
|
|
|
|
|
|
|
} else if ((n = s.find("%{-u}")) == 0) {
|
2016-11-24 18:24:47 +00:00
|
|
|
underline_close();
|
2016-11-02 19:22:45 +00:00
|
|
|
s.erase(0, 5);
|
|
|
|
|
|
|
|
} else if ((n = s.find("%{-o}")) == 0) {
|
2016-11-24 18:24:47 +00:00
|
|
|
overline_close();
|
2016-11-02 19:22:45 +00:00
|
|
|
s.erase(0, 5);
|
|
|
|
|
2016-11-25 12:55:15 +00:00
|
|
|
} else if ((n = s.find("%{")) == 0 && (m = s.find('}')) != string::npos) {
|
2016-11-02 19:22:45 +00:00
|
|
|
append(s.substr(n, m + 1));
|
|
|
|
s.erase(n, m + 1);
|
|
|
|
|
|
|
|
} else if ((n = s.find("%{")) > 0) {
|
|
|
|
append(s.substr(0, n));
|
|
|
|
s.erase(0, n);
|
|
|
|
|
2016-11-25 12:55:15 +00:00
|
|
|
} else {
|
2016-11-02 19:22:45 +00:00
|
|
|
break;
|
2016-11-25 12:55:15 +00:00
|
|
|
}
|
2016-11-02 19:22:45 +00:00
|
|
|
}
|
|
|
|
|
2016-11-25 12:55:15 +00:00
|
|
|
if (!s.empty()) {
|
2016-11-02 19:22:45 +00:00
|
|
|
append(s);
|
2016-11-25 12:55:15 +00:00
|
|
|
}
|
|
|
|
if (add_space) {
|
2016-11-02 19:22:45 +00:00
|
|
|
space();
|
2016-11-25 12:55:15 +00:00
|
|
|
}
|
2016-11-02 19:22:45 +00:00
|
|
|
}
|
|
|
|
|
2016-11-24 18:24:47 +00:00
|
|
|
/**
|
|
|
|
* Insert text node with specific font index
|
|
|
|
*
|
|
|
|
* @see builder::node
|
|
|
|
*/
|
2016-11-02 19:22:45 +00:00
|
|
|
void builder::node(string str, int font_index, bool add_space) {
|
|
|
|
font(font_index);
|
2016-11-25 12:55:15 +00:00
|
|
|
node(move(str), add_space);
|
2016-11-02 19:22:45 +00:00
|
|
|
font_close();
|
|
|
|
}
|
|
|
|
|
2016-11-24 18:24:47 +00:00
|
|
|
/**
|
|
|
|
* Insert tags for given label
|
|
|
|
*/
|
2016-11-25 12:55:15 +00:00
|
|
|
void builder::node(const label_t& label, bool add_space) {
|
|
|
|
if (!label || !*label) {
|
2016-11-02 19:22:45 +00:00
|
|
|
return;
|
2016-11-25 12:55:15 +00:00
|
|
|
}
|
2016-11-02 19:22:45 +00:00
|
|
|
|
2016-11-24 18:24:47 +00:00
|
|
|
string text{label->get()};
|
2016-11-02 19:22:45 +00:00
|
|
|
|
|
|
|
if (label->m_maxlen > 0 && text.length() > label->m_maxlen) {
|
|
|
|
text = text.substr(0, label->m_maxlen) + "...";
|
|
|
|
}
|
|
|
|
|
2016-11-24 18:24:47 +00:00
|
|
|
// 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();
|
2016-11-02 19:22:45 +00:00
|
|
|
|
2016-12-04 10:57:33 +00:00
|
|
|
if (label->m_margin.left > 0) {
|
|
|
|
space(label->m_margin.left);
|
2016-11-25 12:55:15 +00:00
|
|
|
}
|
2016-11-02 19:22:45 +00:00
|
|
|
|
2016-11-25 12:55:15 +00:00
|
|
|
if (!label->m_overline.empty()) {
|
2016-11-02 19:22:45 +00:00
|
|
|
overline(label->m_overline);
|
2016-11-25 12:55:15 +00:00
|
|
|
}
|
|
|
|
if (!label->m_underline.empty()) {
|
2016-11-02 19:22:45 +00:00
|
|
|
underline(label->m_underline);
|
2016-11-25 12:55:15 +00:00
|
|
|
}
|
2016-11-02 19:22:45 +00:00
|
|
|
|
2016-11-25 12:55:15 +00:00
|
|
|
if (!label->m_background.empty()) {
|
2016-11-24 18:24:47 +00:00
|
|
|
background(label->m_background);
|
2016-11-25 12:55:15 +00:00
|
|
|
}
|
|
|
|
if (!label->m_foreground.empty()) {
|
2016-11-24 18:24:47 +00:00
|
|
|
color(label->m_foreground);
|
2016-11-25 12:55:15 +00:00
|
|
|
}
|
2016-11-02 19:22:45 +00:00
|
|
|
|
2016-12-04 10:57:33 +00:00
|
|
|
if (label->m_padding.left > 0) {
|
|
|
|
space(label->m_padding.left);
|
2016-11-25 12:55:15 +00:00
|
|
|
}
|
2016-11-02 19:22:45 +00:00
|
|
|
|
|
|
|
node(text, label->m_font, add_space);
|
|
|
|
|
2016-12-04 10:57:33 +00:00
|
|
|
if (label->m_padding.right > 0) {
|
|
|
|
space(label->m_padding.right);
|
2016-11-25 12:55:15 +00:00
|
|
|
}
|
2016-11-02 19:22:45 +00:00
|
|
|
|
2016-11-25 12:55:15 +00:00
|
|
|
if (!label->m_background.empty()) {
|
2016-11-24 18:24:47 +00:00
|
|
|
background_close();
|
2016-11-25 12:55:15 +00:00
|
|
|
}
|
|
|
|
if (!label->m_foreground.empty()) {
|
2016-11-24 18:24:47 +00:00
|
|
|
color_close();
|
2016-11-25 12:55:15 +00:00
|
|
|
}
|
2016-11-02 19:22:45 +00:00
|
|
|
|
2016-12-04 10:57:33 +00:00
|
|
|
if (!label->m_underline.empty() || (label->m_margin.right > 0 && m_tags[syntaxtag::u] > 0)) {
|
2016-11-24 18:24:47 +00:00
|
|
|
underline_close();
|
2016-11-25 12:55:15 +00:00
|
|
|
}
|
2016-12-04 10:57:33 +00:00
|
|
|
if (!label->m_overline.empty() || (label->m_margin.right > 0 && m_tags[syntaxtag::o] > 0)) {
|
2016-11-24 18:24:47 +00:00
|
|
|
overline_close();
|
2016-11-25 12:55:15 +00:00
|
|
|
}
|
2016-11-02 19:22:45 +00:00
|
|
|
|
2016-12-04 10:57:33 +00:00
|
|
|
if (label->m_margin.right > 0) {
|
|
|
|
space(label->m_margin.right);
|
2016-11-25 12:55:15 +00:00
|
|
|
}
|
2016-11-02 19:22:45 +00:00
|
|
|
}
|
|
|
|
|
2016-11-30 21:17:52 +00:00
|
|
|
/**
|
|
|
|
* Repeat text string n times
|
|
|
|
*/
|
2016-12-04 03:11:47 +00:00
|
|
|
void builder::node_repeat(const string& str, size_t n, bool add_space) {
|
2016-11-30 21:17:52 +00:00
|
|
|
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);
|
|
|
|
}
|
|
|
|
|
2016-11-24 18:24:47 +00:00
|
|
|
/**
|
|
|
|
* Insert tag that will offset the contents by given pixels
|
|
|
|
*/
|
2016-11-02 19:22:45 +00:00
|
|
|
void builder::offset(int pixels) {
|
2016-11-25 12:55:15 +00:00
|
|
|
if (pixels == 0) {
|
2016-11-24 18:24:47 +00:00
|
|
|
return;
|
2016-11-25 12:55:15 +00:00
|
|
|
}
|
2016-11-24 18:24:47 +00:00
|
|
|
tag_open(syntaxtag::O, to_string(pixels));
|
2016-11-02 19:22:45 +00:00
|
|
|
}
|
|
|
|
|
2016-11-24 18:24:47 +00:00
|
|
|
/**
|
|
|
|
* Insert spaces
|
|
|
|
*/
|
2016-11-02 19:22:45 +00:00
|
|
|
void builder::space(int width) {
|
2016-11-25 12:55:15 +00:00
|
|
|
if (width == DEFAULT_SPACING) {
|
2016-11-02 19:22:45 +00:00
|
|
|
width = m_bar.spacing;
|
2016-11-25 12:55:15 +00:00
|
|
|
}
|
|
|
|
if (width <= 0) {
|
2016-11-02 19:22:45 +00:00
|
|
|
return;
|
2016-11-25 12:55:15 +00:00
|
|
|
}
|
2016-11-02 19:22:45 +00:00
|
|
|
string str(width, ' ');
|
|
|
|
append(str);
|
|
|
|
}
|
|
|
|
|
2016-11-24 18:24:47 +00:00
|
|
|
/**
|
|
|
|
* Remove trailing space
|
|
|
|
*/
|
2016-11-02 19:22:45 +00:00
|
|
|
void builder::remove_trailing_space(int width) {
|
2016-11-25 12:55:15 +00:00
|
|
|
if (width == DEFAULT_SPACING) {
|
2016-11-02 19:22:45 +00:00
|
|
|
width = m_bar.spacing;
|
2016-11-25 12:55:15 +00:00
|
|
|
}
|
|
|
|
if (width <= 0) {
|
2016-11-02 19:22:45 +00:00
|
|
|
return;
|
2016-11-25 12:55:15 +00:00
|
|
|
}
|
2016-11-02 19:22:45 +00:00
|
|
|
string::size_type spacing = width;
|
|
|
|
string str(spacing, ' ');
|
2016-11-25 12:55:15 +00:00
|
|
|
if (m_output.length() >= spacing && m_output.substr(m_output.length() - spacing) == str) {
|
2016-11-02 19:22:45 +00:00
|
|
|
m_output = m_output.substr(0, m_output.length() - spacing);
|
2016-11-25 12:55:15 +00:00
|
|
|
}
|
2016-11-02 19:22:45 +00:00
|
|
|
}
|
|
|
|
|
2016-11-24 18:24:47 +00:00
|
|
|
/**
|
|
|
|
* Insert tag to alter the current font index
|
|
|
|
*/
|
2016-11-02 19:22:45 +00:00
|
|
|
void builder::font(int index) {
|
2016-11-30 21:17:52 +00:00
|
|
|
if (index == 0) {
|
|
|
|
return;
|
|
|
|
}
|
2016-11-02 19:22:45 +00:00
|
|
|
m_fontindex = index;
|
2016-11-24 18:24:47 +00:00
|
|
|
tag_open(syntaxtag::T, to_string(index));
|
2016-11-02 19:22:45 +00:00
|
|
|
}
|
|
|
|
|
2016-11-24 18:24:47 +00:00
|
|
|
/**
|
|
|
|
* Insert tag to reset the font index
|
|
|
|
*/
|
|
|
|
void builder::font_close() {
|
2016-11-02 19:22:45 +00:00
|
|
|
m_fontindex = 1;
|
2016-11-24 18:24:47 +00:00
|
|
|
tag_close(syntaxtag::T);
|
2016-11-02 19:22:45 +00:00
|
|
|
}
|
|
|
|
|
2016-11-24 18:24:47 +00:00
|
|
|
/**
|
|
|
|
* Insert tag to alter the current background color
|
|
|
|
*/
|
2016-11-02 19:22:45 +00:00
|
|
|
void builder::background(string color) {
|
2016-11-25 12:55:15 +00:00
|
|
|
if (color.length() == 2 || (color.find('#') == 0 && color.length() == 3)) {
|
2016-11-24 18:24:47 +00:00
|
|
|
string bg{background_hex()};
|
2016-11-02 19:22:45 +00:00
|
|
|
color = "#" + color.substr(color.length() - 2);
|
|
|
|
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);
|
|
|
|
}
|
|
|
|
|
2016-11-24 18:24:47 +00:00
|
|
|
color = color_util::simplify_hex(color);
|
2016-11-02 19:22:45 +00:00
|
|
|
m_colors[syntaxtag::B] = color;
|
2016-11-24 18:24:47 +00:00
|
|
|
tag_open(syntaxtag::B, color);
|
2016-11-02 19:22:45 +00:00
|
|
|
}
|
|
|
|
|
2016-11-24 18:24:47 +00:00
|
|
|
/**
|
|
|
|
* Insert tag to reset the background color
|
|
|
|
*/
|
|
|
|
void builder::background_close() {
|
2016-11-02 19:22:45 +00:00
|
|
|
m_colors[syntaxtag::B] = "";
|
2016-11-24 18:24:47 +00:00
|
|
|
tag_close(syntaxtag::B);
|
2016-11-02 19:22:45 +00:00
|
|
|
}
|
|
|
|
|
2016-11-24 18:24:47 +00:00
|
|
|
/**
|
|
|
|
* Insert tag to alter the current foreground color
|
|
|
|
*/
|
|
|
|
void builder::color(string color) {
|
2016-11-25 12:55:15 +00:00
|
|
|
if (color.length() == 2 || (color.find('#') == 0 && color.length() == 3)) {
|
2016-11-24 18:24:47 +00:00
|
|
|
string fg{foreground_hex()};
|
2016-11-02 19:22:45 +00:00
|
|
|
color = "#" + color.substr(color.length() - 2);
|
|
|
|
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);
|
|
|
|
}
|
|
|
|
|
2016-11-24 18:24:47 +00:00
|
|
|
color = color_util::simplify_hex(color);
|
2016-11-02 19:22:45 +00:00
|
|
|
m_colors[syntaxtag::F] = color;
|
2016-11-24 18:24:47 +00:00
|
|
|
tag_open(syntaxtag::F, color);
|
2016-11-02 19:22:45 +00:00
|
|
|
}
|
|
|
|
|
2016-11-24 18:24:47 +00:00
|
|
|
/**
|
|
|
|
* Insert tag to alter the alpha value of the default foreground color
|
|
|
|
*/
|
|
|
|
void builder::color_alpha(string alpha) {
|
|
|
|
string val{foreground_hex()};
|
|
|
|
|
2016-11-30 21:17:52 +00:00
|
|
|
if (alpha.find('#') == string::npos) {
|
2016-11-02 19:22:45 +00:00
|
|
|
alpha = "#" + alpha;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (alpha.size() == 4) {
|
|
|
|
color(alpha);
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (val.size() < 6 && val.size() > 2) {
|
|
|
|
val.append(val.substr(val.size() - 3));
|
|
|
|
}
|
|
|
|
|
|
|
|
color((alpha.substr(0, 3) + val.substr(val.size() - 6)).substr(0, 9));
|
|
|
|
}
|
|
|
|
|
2016-11-24 18:24:47 +00:00
|
|
|
/**
|
|
|
|
* Insert tag to reset the foreground color
|
|
|
|
*/
|
|
|
|
void builder::color_close() {
|
2016-11-02 19:22:45 +00:00
|
|
|
m_colors[syntaxtag::F] = "";
|
2016-11-24 18:24:47 +00:00
|
|
|
tag_close(syntaxtag::F);
|
2016-11-02 19:22:45 +00:00
|
|
|
}
|
|
|
|
|
2016-11-24 18:24:47 +00:00
|
|
|
/**
|
|
|
|
* Insert tag to alter the current overline/underline color
|
|
|
|
*/
|
2016-11-25 12:55:15 +00:00
|
|
|
void builder::line_color(const string& color) {
|
2016-11-24 18:24:47 +00:00
|
|
|
overline_color(color);
|
|
|
|
underline_color(color);
|
2016-11-02 19:22:45 +00:00
|
|
|
}
|
|
|
|
|
2016-11-24 18:24:47 +00:00
|
|
|
/**
|
|
|
|
* Close overline/underline color tag
|
|
|
|
*/
|
|
|
|
void builder::line_color_close() {
|
|
|
|
overline_color_close();
|
|
|
|
underline_color_close();
|
2016-11-02 19:22:45 +00:00
|
|
|
}
|
|
|
|
|
2016-11-24 18:24:47 +00:00
|
|
|
/**
|
|
|
|
* Insert tag to alter the current overline color
|
|
|
|
*/
|
2016-11-21 15:14:40 +00:00
|
|
|
void builder::overline_color(string color) {
|
2016-11-24 18:24:47 +00:00
|
|
|
color = color_util::simplify_hex(color);
|
|
|
|
m_colors[syntaxtag::o] = color;
|
|
|
|
tag_open(syntaxtag::o, color);
|
|
|
|
tag_open(attribute::OVERLINE);
|
2016-11-21 15:14:40 +00:00
|
|
|
}
|
|
|
|
|
2016-11-24 18:24:47 +00:00
|
|
|
/**
|
|
|
|
* Close underline color tag
|
|
|
|
*/
|
|
|
|
void builder::overline_color_close() {
|
|
|
|
m_colors[syntaxtag::o] = "";
|
|
|
|
tag_close(syntaxtag::o);
|
2016-11-21 15:14:40 +00:00
|
|
|
}
|
|
|
|
|
2016-11-24 18:24:47 +00:00
|
|
|
/**
|
|
|
|
* Insert tag to alter the current underline color
|
|
|
|
*/
|
2016-11-21 15:14:40 +00:00
|
|
|
void builder::underline_color(string color) {
|
2016-11-24 18:24:47 +00:00
|
|
|
color = color_util::simplify_hex(color);
|
|
|
|
m_colors[syntaxtag::u] = color;
|
|
|
|
tag_open(syntaxtag::u, color);
|
|
|
|
tag_open(attribute::UNDERLINE);
|
2016-11-21 15:14:40 +00:00
|
|
|
}
|
|
|
|
|
2016-11-24 18:24:47 +00:00
|
|
|
/**
|
|
|
|
* Close underline color tag
|
|
|
|
*/
|
|
|
|
void builder::underline_color_close() {
|
|
|
|
tag_close(syntaxtag::u);
|
|
|
|
m_colors[syntaxtag::u] = "";
|
2016-11-21 15:14:40 +00:00
|
|
|
}
|
|
|
|
|
2016-11-24 18:24:47 +00:00
|
|
|
/**
|
|
|
|
* Insert tag to enable the overline attribute
|
|
|
|
*/
|
2016-11-25 12:55:15 +00:00
|
|
|
void builder::overline(const string& color) {
|
|
|
|
if (!color.empty()) {
|
2016-11-21 15:14:40 +00:00
|
|
|
overline_color(color);
|
2016-11-25 12:55:15 +00:00
|
|
|
} else {
|
2016-11-24 18:24:47 +00:00
|
|
|
tag_open(attribute::OVERLINE);
|
2016-11-25 12:55:15 +00:00
|
|
|
}
|
2016-11-02 19:22:45 +00:00
|
|
|
}
|
|
|
|
|
2016-11-24 18:24:47 +00:00
|
|
|
/**
|
|
|
|
* Close overline attribute tag
|
|
|
|
*/
|
|
|
|
void builder::overline_close() {
|
|
|
|
tag_close(attribute::OVERLINE);
|
2016-11-02 19:22:45 +00:00
|
|
|
}
|
|
|
|
|
2016-11-24 18:24:47 +00:00
|
|
|
/**
|
|
|
|
* Insert tag to enable the underline attribute
|
|
|
|
*/
|
2016-11-25 12:55:15 +00:00
|
|
|
void builder::underline(const string& color) {
|
|
|
|
if (!color.empty()) {
|
2016-11-21 15:14:40 +00:00
|
|
|
underline_color(color);
|
2016-11-25 12:55:15 +00:00
|
|
|
} else {
|
2016-11-24 18:24:47 +00:00
|
|
|
tag_open(attribute::UNDERLINE);
|
2016-11-25 12:55:15 +00:00
|
|
|
}
|
2016-11-02 19:22:45 +00:00
|
|
|
}
|
|
|
|
|
2016-11-24 18:24:47 +00:00
|
|
|
/**
|
|
|
|
* Close underline attribute tag
|
|
|
|
*/
|
|
|
|
void builder::underline_close() {
|
|
|
|
tag_close(attribute::UNDERLINE);
|
2016-11-02 19:22:45 +00:00
|
|
|
}
|
|
|
|
|
2016-11-24 18:24:47 +00:00
|
|
|
/**
|
|
|
|
* Open command tag
|
|
|
|
*/
|
2016-11-02 19:22:45 +00:00
|
|
|
void builder::cmd(mousebtn index, string action, bool condition) {
|
|
|
|
int button = static_cast<int>(index);
|
|
|
|
|
2016-11-25 12:55:15 +00:00
|
|
|
if (!condition || action.empty()) {
|
2016-11-02 19:22:45 +00:00
|
|
|
return;
|
2016-11-25 12:55:15 +00:00
|
|
|
}
|
2016-11-02 19:22:45 +00:00
|
|
|
|
|
|
|
action = string_util::replace_all(action, ":", "\\:");
|
|
|
|
|
2016-11-24 18:24:47 +00:00
|
|
|
tag_open(syntaxtag::A, to_string(button) + ":" + action + ":");
|
2016-11-02 19:22:45 +00:00
|
|
|
}
|
|
|
|
|
2016-11-24 18:24:47 +00:00
|
|
|
/**
|
|
|
|
* Close command tag
|
|
|
|
*/
|
|
|
|
void builder::cmd_close() {
|
|
|
|
tag_close(syntaxtag::A);
|
2016-11-02 19:22:45 +00:00
|
|
|
}
|
|
|
|
|
2016-11-24 18:24:47 +00:00
|
|
|
/**
|
|
|
|
* Get default background hex string
|
|
|
|
*/
|
|
|
|
string builder::background_hex() {
|
2016-11-25 12:55:15 +00:00
|
|
|
if (m_background.empty()) {
|
2016-11-24 18:24:47 +00:00
|
|
|
m_background = color_util::hex<uint16_t>(m_bar.background);
|
2016-11-25 12:55:15 +00:00
|
|
|
}
|
2016-11-24 18:24:47 +00:00
|
|
|
return m_background;
|
2016-11-02 19:22:45 +00:00
|
|
|
}
|
|
|
|
|
2016-11-24 18:24:47 +00:00
|
|
|
/**
|
|
|
|
* Get default foreground hex string
|
|
|
|
*/
|
|
|
|
string builder::foreground_hex() {
|
2016-11-25 12:55:15 +00:00
|
|
|
if (m_foreground.empty()) {
|
2016-11-24 18:24:47 +00:00
|
|
|
m_foreground = color_util::hex<uint16_t>(m_bar.foreground);
|
2016-11-25 12:55:15 +00:00
|
|
|
}
|
2016-11-24 18:24:47 +00:00
|
|
|
return m_foreground;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Insert directive to change value of given tag
|
|
|
|
*/
|
2016-11-25 12:55:15 +00:00
|
|
|
void builder::tag_open(syntaxtag tag, const string& value) {
|
|
|
|
if (m_tags.find(tag) == m_tags.end()) {
|
2016-11-25 03:10:26 +00:00
|
|
|
m_tags[tag] = 0;
|
2016-11-25 12:55:15 +00:00
|
|
|
}
|
2016-11-25 03:10:26 +00:00
|
|
|
|
|
|
|
m_tags[tag]++;
|
2016-11-24 18:24:47 +00:00
|
|
|
|
|
|
|
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) {
|
2016-11-25 12:55:15 +00:00
|
|
|
if ((m_attributes >> static_cast<uint8_t>(attr)) & 1U) {
|
2016-11-24 18:24:47 +00:00
|
|
|
return;
|
2016-11-25 12:55:15 +00:00
|
|
|
}
|
2016-11-24 18:24:47 +00:00
|
|
|
|
|
|
|
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) {
|
2016-11-25 12:55:15 +00:00
|
|
|
if (m_tags.find(tag) == m_tags.end() || !m_tags[tag]) {
|
2016-11-24 18:24:47 +00:00
|
|
|
return;
|
2016-11-25 12:55:15 +00:00
|
|
|
}
|
2016-11-24 18:24:47 +00:00
|
|
|
|
|
|
|
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) {
|
2016-11-25 12:55:15 +00:00
|
|
|
if (!((m_attributes >> static_cast<uint8_t>(attr)) & 1U)) {
|
2016-11-24 18:24:47 +00:00
|
|
|
return;
|
2016-11-25 12:55:15 +00:00
|
|
|
}
|
2016-11-24 18:24:47 +00:00
|
|
|
|
|
|
|
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;
|
|
|
|
}
|
2016-11-02 19:22:45 +00:00
|
|
|
}
|
|
|
|
|
2016-11-19 05:22:44 +00:00
|
|
|
POLYBAR_NS_END
|