fix(label): Support variable token order

Bound specifiers required the tokens to be
replaced in the same order they were defined.

This fixes that by storing and comparing
the token string.
This commit is contained in:
Michael Carlberg 2016-11-22 03:35:30 +01:00
parent 09e0ea1932
commit 4ef0c55dda
2 changed files with 13 additions and 12 deletions

View File

@ -11,8 +11,8 @@ POLYBAR_NS
*/ */
namespace drawtypes { namespace drawtypes {
struct bounds { struct bounds {
string token;
size_t min; size_t min;
size_t max; size_t max;
}; };
@ -42,7 +42,7 @@ namespace drawtypes {
explicit label(string text, string foreground = "", string background = "", explicit label(string text, string foreground = "", string background = "",
string underline = "", string overline = "", int font = 0, int padding = 0, int margin = 0, string underline = "", string overline = "", int font = 0, int padding = 0, int margin = 0,
size_t maxlen = 0, bool ellipsis = true, size_t maxlen = 0, bool ellipsis = true,
const vector<struct bounds>& bound = vector<struct bounds>()) const vector<struct bounds>& bound = {})
: m_foreground(foreground) : m_foreground(foreground)
, m_background(background) , m_background(background)
, m_underline(underline) , m_underline(underline)
@ -68,7 +68,6 @@ namespace drawtypes {
private: private:
string m_text, m_tokenized; string m_text, m_tokenized;
const vector<struct bounds> m_token_bounds; const vector<struct bounds> m_token_bounds;
vector<struct bounds>::const_iterator m_token_iterator;
}; };

View File

@ -18,7 +18,6 @@ namespace drawtypes {
void label::reset_tokens() { void label::reset_tokens() {
m_tokenized = m_text; m_tokenized = m_text;
m_token_iterator = m_token_bounds.begin();
} }
bool label::has_token(string token) { bool label::has_token(string token) {
@ -26,13 +25,14 @@ namespace drawtypes {
} }
void label::replace_token(string token, string replacement) { void label::replace_token(string token, string replacement) {
if (m_text.find(token) == string::npos || m_token_iterator == m_token_bounds.end()) if (!has_token(token))
return; return;
m_tokenized = string_util::replace_all_bounded(m_tokenized, token, replacement, for (auto&& bound : m_token_bounds) {
m_token_iterator->min, m_token_iterator->max); if (token != bound.token)
continue;
m_token_iterator++; m_tokenized = string_util::replace_all_bounded(m_tokenized, token, replacement, bound.min, bound.max);
}
} }
void label::replace_defined_values(const label_t& label) { void label::replace_defined_values(const label_t& label) {
@ -86,7 +86,7 @@ namespace drawtypes {
string line{text}; string line{text};
while ((start = line.find('%')) != string::npos && (end = line.find('%', start + 1)) != string::npos) { while ((start = line.find('%')) != string::npos && (end = line.find('%', start + 1)) != string::npos) {
auto token = line.substr(start, end); auto token = line.substr(start, end + 1);
line.erase(0, end); line.erase(0, end);
@ -94,14 +94,15 @@ namespace drawtypes {
if (token[1] == '{') if (token[1] == '{')
continue; continue;
bound.emplace_back(bounds{0, 0}); bound.emplace_back(bounds{token, 0, 0});
// find min delimiter // find min delimiter
if ((pos = token.find(':')) == string::npos) if ((pos = token.find(':')) == string::npos)
continue; continue;
// strip min/max specifiers from the label string token // strip min/max specifiers from the label string token
text = string_util::replace(text, token, token.substr(0, pos)); bound.back().token = token.substr(0, pos) + '%';
text = string_util::replace(text, token, bound.back().token);
try { try {
bound.back().min = std::stoul(&token[pos + 1], nullptr, 10); bound.back().min = std::stoul(&token[pos + 1], nullptr, 10);
@ -123,6 +124,7 @@ namespace drawtypes {
if (bound.back().max < bound.back().min) if (bound.back().max < bound.back().min)
bound.back().max = 0; bound.back().max = 0;
} }
// clang-format off // clang-format off
return label_t{new label_t::element_type(text, return label_t{new label_t::element_type(text,
conf.get<string>(section, name + "-foreground", ""), conf.get<string>(section, name + "-foreground", ""),