mirror of https://github.com/polybar/polybar.git
refactor(builder): No exception when maxlen < 3
The check of the maxlen and ellipsis condition was also moved to the label creation, this way get_label_text doesn't need to care about the restrictions placed on maxlen and ellipsis
This commit is contained in:
parent
7dc42f543f
commit
0346a965a7
|
@ -1,5 +1,7 @@
|
|||
#pragma once
|
||||
|
||||
#include <cassert>
|
||||
|
||||
#include "common.hpp"
|
||||
#include "components/config.hpp"
|
||||
#include "components/types.hpp"
|
||||
|
@ -37,6 +39,14 @@ namespace drawtypes {
|
|||
int m_font{0};
|
||||
side_values m_padding{0U,0U};
|
||||
side_values m_margin{0U,0U};
|
||||
|
||||
/*
|
||||
* If m_ellipsis is true, m_maxlen MUST be larger or equal to the length of
|
||||
* the ellipsis (3), everything else is a programming error
|
||||
*
|
||||
* load_label should take care of this, but be aware, if you are creating
|
||||
* labels in a different way.
|
||||
*/
|
||||
size_t m_maxlen{0_z};
|
||||
bool m_ellipsis{true};
|
||||
|
||||
|
@ -55,7 +65,9 @@ namespace drawtypes {
|
|||
, m_ellipsis(ellipsis)
|
||||
, m_text(text)
|
||||
, m_tokenized(m_text)
|
||||
, m_tokens(forward<vector<token>>(tokens)) {}
|
||||
, m_tokens(forward<vector<token>>(tokens)) {
|
||||
assert(!m_ellipsis || (m_maxlen == 0 || m_maxlen >= 3));
|
||||
}
|
||||
|
||||
string get() const;
|
||||
operator bool();
|
||||
|
|
|
@ -557,14 +557,8 @@ string builder::get_label_text(const label_t& label) {
|
|||
|
||||
size_t maxlen = label->m_maxlen;
|
||||
|
||||
if (maxlen > 0 && string_util::char_len(text) > maxlen ) {
|
||||
if(label->m_ellipsis) {
|
||||
if(maxlen < 3) {
|
||||
throw application_error(sstream()
|
||||
<< "Label has maxlen (" << maxlen
|
||||
<< ") that is smaller than size of ellipsis(3)");
|
||||
}
|
||||
|
||||
if (maxlen > 0 && string_util::char_len(text) > maxlen) {
|
||||
if (label->m_ellipsis) {
|
||||
text = string_util::utf8_truncate(std::move(text), maxlen - 3) + "...";
|
||||
}
|
||||
else {
|
||||
|
|
|
@ -221,6 +221,17 @@ namespace drawtypes {
|
|||
}
|
||||
}
|
||||
|
||||
size_t maxlen = conf.get(section, name + "-maxlen", 0_z);
|
||||
bool ellipsis = conf.get(section, name + "-ellipsis", true);
|
||||
|
||||
if(ellipsis && maxlen > 0 && maxlen < 3) {
|
||||
logger::make().err(sstream() << "Label " << section << "." << name
|
||||
<< " has maxlen " << maxlen
|
||||
<< ", which is smaller than length of ellipsis (3), disabling ellipsis...");
|
||||
|
||||
ellipsis = false;
|
||||
}
|
||||
|
||||
// clang-format off
|
||||
return factory_util::shared<label>(text,
|
||||
conf.get(section, name + "-foreground", ""s),
|
||||
|
@ -230,8 +241,8 @@ namespace drawtypes {
|
|||
conf.get(section, name + "-font", 0),
|
||||
padding,
|
||||
margin,
|
||||
conf.get(section, name + "-maxlen", 0_z),
|
||||
conf.get(section, name + "-ellipsis", true),
|
||||
maxlen,
|
||||
ellipsis,
|
||||
move(tokens));
|
||||
// clang-format on
|
||||
}
|
||||
|
|
|
@ -65,16 +65,4 @@ TEST_P(GetLabelTextTest, correctness) {
|
|||
EXPECT_LE(text.length(), m_label->m_maxlen) << "Returned text is longer than maxlen";
|
||||
}
|
||||
|
||||
/**
|
||||
* \brief Tests, if get_label_text throws an exception, when ellipsis is
|
||||
* turned on and m_maxlen is lower than 3 (length of the ellipsis)
|
||||
*/
|
||||
TEST_F(GetLabelTextTest, throwsException) {
|
||||
label_t m_label = factory_util::shared<label>("abcdef");
|
||||
m_label->m_ellipsis = true;
|
||||
m_label->m_maxlen = 2;
|
||||
|
||||
EXPECT_ANY_THROW(m_builder.get_label_text(m_label));
|
||||
}
|
||||
|
||||
// }}}
|
||||
|
|
Loading…
Reference in New Issue