1
0
Fork 0
mirror of https://github.com/polybar/polybar.git synced 2025-03-31 17:25:14 -04:00

feat(builder) Add minlen

The counterpart of maxlen, that forces a minimum length on a string.
Pads right with spaces if a string is too short.
This commit is contained in:
M1dgard 2018-05-29 15:35:11 +02:00
parent 6f0a1dfc54
commit 4df725aab9
No known key found for this signature in database
GPG key ID: 511C112F1331BBB4
3 changed files with 32 additions and 3 deletions

View file

@ -49,11 +49,12 @@ namespace drawtypes {
*/
size_t m_maxlen{0_z};
bool m_ellipsis{true};
size_t m_minlen{0_z};
explicit label(string text, int font) : m_font(font), m_text(text), m_tokenized(m_text) {}
explicit label(string text, string foreground = ""s, string background = ""s, string underline = ""s,
string overline = ""s, int font = 0, struct side_values padding = {0U,0U}, struct side_values margin = {0U,0U},
size_t maxlen = 0_z, bool ellipsis = true, vector<token>&& tokens = {})
size_t maxlen = 0_z, bool ellipsis = true, size_t minlen = 0_z, vector<token>&& tokens = {})
: m_foreground(foreground)
, m_background(background)
, m_underline(underline)
@ -63,10 +64,12 @@ namespace drawtypes {
, m_margin(margin)
, m_maxlen(maxlen)
, m_ellipsis(ellipsis)
, m_minlen(minlen)
, m_text(text)
, m_tokenized(m_text)
, m_tokens(forward<vector<token>>(tokens)) {
assert(!m_ellipsis || (m_maxlen == 0 || m_maxlen >= 3));
assert(!m_ellipsis || m_minlen <= m_maxlen);
}
string get() const;

View file

@ -559,9 +559,12 @@ string builder::foreground_hex() {
string builder::get_label_text(const label_t& label) {
string text{label->get()};
size_t len = string_util::char_len(text);
size_t maxlen = label->m_maxlen;
size_t minlen = label->m_minlen;
if (maxlen > 0 && string_util::char_len(text) > maxlen) {
// Truncate if too long
if (maxlen > 0 && len > maxlen) {
if (label->m_ellipsis) {
text = string_util::utf8_truncate(std::move(text), maxlen - 3) + "...";
}
@ -570,6 +573,12 @@ string builder::get_label_text(const label_t& label) {
}
}
// Pad right if too short
if (len < minlen) {
std::string s = "123";
text.insert(text.end(), minlen - len, ' ');
}
return text;
}

View file

@ -22,7 +22,7 @@ namespace drawtypes {
std::copy(m_tokens.begin(), m_tokens.end(), back_it);
}
return factory_util::shared<label>(m_text, m_foreground, m_background, m_underline, m_overline, m_font, m_padding,
m_margin, m_maxlen, m_ellipsis, move(tokens));
m_margin, m_maxlen, m_ellipsis, m_minlen, move(tokens));
}
void label::clear() {
@ -95,6 +95,9 @@ namespace drawtypes {
m_maxlen = label->m_maxlen;
m_ellipsis = label->m_ellipsis;
}
if (label->m_minlen != 0_z) {
m_minlen = label->m_minlen;
}
}
void label::copy_undefined(const label_t& label) {
@ -129,6 +132,9 @@ namespace drawtypes {
m_maxlen = label->m_maxlen;
m_ellipsis = label->m_ellipsis;
}
if (m_minlen == 0_z && label->m_minlen != 0_z) {
m_minlen = label->m_minlen;
}
}
/**
@ -223,6 +229,7 @@ namespace drawtypes {
size_t maxlen = conf.get(section, name + "-maxlen", 0_z);
bool ellipsis = conf.get(section, name + "-ellipsis", true);
size_t minlen = conf.get(section, name + "-minlen", 0_z);
if(ellipsis && maxlen > 0 && maxlen < 3) {
logger::make().err(sstream() << "Label " << section << "." << name
@ -232,6 +239,15 @@ namespace drawtypes {
ellipsis = false;
}
if (ellipsis && minlen > maxlen) {
logger::make().err(sstream() << "Label " << section << "." << name
<< " has minlen " << minlen
<< ", which is greater than its maxlen " << maxlen
<< ", disabling minlen...");
minlen = 0_z;
}
// clang-format off
return factory_util::shared<label>(text,
conf.get(section, name + "-foreground", ""s),
@ -243,6 +259,7 @@ namespace drawtypes {
margin,
maxlen,
ellipsis,
minlen,
move(tokens));
// clang-format on
}