Merge pull request #163 from NBonaparte/padding

feat: add padding for values
This commit is contained in:
Michael Carlberg 2016-11-21 08:09:51 +01:00 committed by GitHub
commit 48da703970
4 changed files with 83 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 || m_token_iterator == m_token_bounds.end())
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,9 @@ 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;
size_t start, end, pos;
name = string_util::ltrim(string_util::rtrim(name, '>'), '<'); name = string_util::ltrim(string_util::rtrim(name, '>'), '<');
string text; string text;
@ -73,6 +83,46 @@ namespace drawtypes {
else else
text = conf.get<string>(section, name, def); text = conf.get<string>(section, name, def);
string line{text};
while ((start = line.find('%')) != string::npos && (end = line.find('%', start + 1)) != string::npos) {
auto token = line.substr(start, end);
line.erase(0, end);
// ignore false positives (lemonbar-style declarations)
if (token[1] == '{')
continue;
bound.emplace_back(bounds{0, 0});
// find min delimiter
if ((pos = token.find(':')) == string::npos)
continue;
// strip min/max specifiers from the label string token
text = string_util::replace(text, token, token.substr(0, pos));
try {
bound.back().min = std::stoul(&token[pos + 1], nullptr, 10);
} catch (const std::invalid_argument& err) {
continue;
}
// find max delimiter
if ((pos = token.find(':', pos + 1)) == string::npos)
continue;
try {
bound.back().max = std::stoul(&token[pos + 1], nullptr, 10);
} catch (const std::invalid_argument& err) {
continue;
}
// ignore max lengths less than min
if (bound.back().max < bound.back().min)
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", ""),
@ -83,7 +133,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
*/ */