feat: added max and min values to tokens

This commit is contained in:
NBonaparte 2016-11-20 14:09:08 -08:00
parent d0915b82f4
commit 4179f8b7f9
4 changed files with 64 additions and 5 deletions

View File

@ -11,6 +11,12 @@ POLYBAR_NS
*/ */
namespace drawtypes { namespace drawtypes {
struct bounds {
size_t min;
size_t max;
};
class label; class label;
using label_t = shared_ptr<label>; using label_t = shared_ptr<label>;
@ -35,7 +41,8 @@ namespace drawtypes {
explicit label(string text, int font) : m_font(font), m_text(text), m_tokenized(m_text) {} explicit label(string text, int font) : m_font(font), m_text(text), m_tokenized(m_text) {}
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>())
: m_foreground(foreground) : m_foreground(foreground)
, m_background(background) , m_background(background)
, m_underline(underline) , m_underline(underline)
@ -46,7 +53,8 @@ namespace drawtypes {
, m_maxlen(maxlen) , m_maxlen(maxlen)
, m_ellipsis(ellipsis) , m_ellipsis(ellipsis)
, m_text(text) , m_text(text)
, m_tokenized(m_text) {} , m_tokenized(m_text)
, m_token_bounds(bound) {}
string get() const; string get() const;
operator bool(); operator bool();
@ -59,6 +67,9 @@ namespace drawtypes {
private: private:
string m_text, m_tokenized; string m_text, m_tokenized;
const vector<struct bounds> m_token_bounds;
vector<struct bounds>::const_iterator m_token_iterator;
}; };
label_t load_label( label_t load_label(

View File

@ -18,6 +18,8 @@ namespace string_util {
bool compare(const string& s1, const string& s2); bool compare(const string& s1, const string& s2);
string replace(const string& haystack, string needle, string repl, size_t start = 0, size_t end = string::npos); string replace(const string& haystack, string needle, string repl, size_t start = 0, size_t end = string::npos);
string replace_all(const string& haystack, string needle, string repl, size_t start = 0, size_t end = string::npos); string replace_all(const string& haystack, string needle, string repl, size_t start = 0, size_t end = string::npos);
string replace_all_bounded(const string& haystack, string needle, string replacement,
size_t min, size_t max, size_t start = 0, size_t end = string::npos);
string squeeze(const string& haystack, char needle); string squeeze(const string& haystack, char needle);
string strip(const string& haystack, char needle); string strip(const string& haystack, char needle);
string strip_trailing_newline(const string& haystack); string strip_trailing_newline(const string& haystack);

View File

@ -13,11 +13,12 @@ namespace drawtypes {
label_t label::clone() { label_t label::clone() {
return label_t{new label(m_text, m_foreground, m_background, m_underline, m_overline, m_font, return label_t{new label(m_text, m_foreground, m_background, m_underline, m_overline, m_font,
m_padding, m_margin, m_maxlen, m_ellipsis)}; m_padding, m_margin, m_maxlen, m_ellipsis, m_token_bounds)};
} }
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) {
@ -25,7 +26,13 @@ namespace drawtypes {
} }
void label::replace_token(string token, string replacement) { void label::replace_token(string token, string replacement) {
m_tokenized = string_util::replace_all(m_tokenized, token, replacement); if (m_text.find(token) == string::npos)
return;
m_tokenized = string_util::replace_all_bounded(m_tokenized, token, replacement,
m_token_iterator->min, m_token_iterator->max);
m_token_iterator++;
} }
void label::replace_defined_values(const label_t& label) { void label::replace_defined_values(const label_t& label) {
@ -64,6 +71,8 @@ namespace drawtypes {
* Create a label by loading values from the configuration * Create a label by loading values from the configuration
*/ */
label_t load_label(const config& conf, string section, string name, bool required, string def) { label_t load_label(const config& conf, string section, string name, bool required, string def) {
vector<struct bounds> bound;
name = string_util::ltrim(string_util::rtrim(name, '>'), '<'); name = string_util::ltrim(string_util::rtrim(name, '>'), '<');
string text; string text;
@ -73,6 +82,28 @@ namespace drawtypes {
else else
text = conf.get<string>(section, name, def); text = conf.get<string>(section, name, def);
vector<string> text_split = string_util::split(text, '%');
// get rid of false positives (lemonbar-style declarations)
for (size_t i = 0; i < text_split.size(); i++) {
if (!text_split[i].empty() && text_split[i].at(0) == '{')
text_split.erase(text_split.begin() + i--);
}
for (size_t i = 1; i < text_split.size(); i += 2) {
struct bounds bound_vals = {0, 0};
size_t delim_min = text_split[i].find(':'), delim_max = text_split[i].find(':', delim_min + 1);
if (delim_min != string::npos) {
if (delim_max != string::npos) {
bound_vals.min = stoul(text_split[i].substr(delim_min + 1, delim_max - delim_min - 1));
bound_vals.max = stoul(text_split[i].substr(delim_max + 1));
}
else
bound_vals.min = stoul(text_split[i].substr(delim_min + 1));
}
bound.push_back(bound_vals);
text = string_util::replace_all(text, text_split[i], text_split[i].substr(0, delim_min));
}
// 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", ""),
@ -83,7 +114,8 @@ namespace drawtypes {
conf.get<int>(section, name + "-padding", 0), conf.get<int>(section, name + "-padding", 0),
conf.get<int>(section, name + "-margin", 0), conf.get<int>(section, name + "-margin", 0),
conf.get<size_t>(section, name + "-maxlen", 0), conf.get<size_t>(section, name + "-maxlen", 0),
conf.get<bool>(section, name + "-ellipsis", true))}; conf.get<bool>(section, name + "-ellipsis", true),
bound)};
// clang-format on // clang-format on
} }

View File

@ -78,6 +78,20 @@ namespace string_util {
return replaced; return replaced;
} }
/**
* Replace all occurrences with bounded replacement
*/
string replace_all_bounded(const string& haystack, string needle, string replacement,
size_t min, size_t max, size_t start, size_t end) {
if (max != 0 && replacement.length() > max) {
replacement = replacement.erase(max);
}
else if (min != 0 && replacement.length() < min) {
replacement.insert(0, min - replacement.length(), ' ');
}
return replace_all(haystack, needle, replacement, start, end);
}
/** /**
* Replace all consecutive occurrences of needle in haystack * Replace all consecutive occurrences of needle in haystack
*/ */