feat: Label maxlen property

label-NAME-maxlen = N
label-NAME-ellipsis = bool

Fixes jaagr/lemonbuddy#49
This commit is contained in:
Michael Carlberg 2016-08-31 17:39:51 +02:00
parent afb787f0d7
commit c3f84c22f0
4 changed files with 23 additions and 6 deletions

View File

@ -281,7 +281,7 @@ The configuration syntax is based on the `ini` file format.
;
; The rest of the drawtypes follow the same pattern.
;
; label-NAME[-(foreground|background|(under|over)line|font|padding)] = ?
; label-NAME[-(foreground|background|(under|over)line|font|padding|maxlen|ellipsis)] = ?
; icon-NAME[-(foreground|background|(under|over)line|font|padding)] = ?
; ramp-NAME-[0-9]+[-(foreground|background|(under|over)line|font|padding)] = ?
; animation-NAME-[0-9]+[-(foreground|background|(under|over)line|font|padding)] = ?
@ -300,6 +300,10 @@ The configuration syntax is based on the `ini` file format.
format-offline = <label-offline>
format-offline-offset = -8
; Cap the song label without trailing ellipsis
label-song-maxlen = 30
label-song-ellipsis = false
; By only specifying alpha value, it will be applied to the bar's default foreground
label-time-foreground = #66

View File

@ -9,11 +9,13 @@ namespace drawtypes
{
std::string text, fg, bg, ul, ol;
int font = 0, padding = 0, margin = 0;
size_t maxlen = 0;
bool ellipsis = true;
Label(std::string text, int font)
: text(text), font(font){}
Label(std::string text, std::string fg = "", std::string bg = "", std::string ul = "", std::string ol = "", int font = 0, int padding = 0, int margin = 0)
: text(text), fg(fg), bg(bg), ul(ul), ol(ol), font(font), padding(padding), margin(margin){}
Label(std::string text, std::string fg = "", std::string bg = "", std::string ul = "", std::string ol = "", int font = 0, int padding = 0, int margin = 0, size_t maxlen = 0, bool ellipsis = true)
: text(text), fg(fg), bg(bg), ul(ul), ol(ol), font(font), padding(padding), margin(margin), maxlen(maxlen), ellipsis(ellipsis){}
operator bool() {
return !this->text.empty();

View File

@ -6,7 +6,7 @@ namespace drawtypes
{
std::unique_ptr<Label> Label::clone() {
return std::unique_ptr<Label> { new Label(
this->text, this->fg, this->bg, this->ul, this->ol, this->font, this->padding, this->margin) };
this->text, this->fg, this->bg, this->ul, this->ol, this->font, this->padding, this->margin, this->maxlen, this->ellipsis) };
}
void Label::replace_token(std::string token, std::string replacement) {
@ -37,7 +37,9 @@ namespace drawtypes
config::get<std::string>(config_path, label_name +"-overline", ""),
config::get<int>(config_path, label_name +"-font", 0),
config::get<int>(config_path, label_name +"-padding", 0),
config::get<int>(config_path, label_name +"-margin", 0)) };
config::get<int>(config_path, label_name +"-margin", 0),
config::get<size_t>(config_path, label_name +"-maxlen", 0),
config::get<bool>(config_path, label_name +"-ellipsis", true)) };
}
std::unique_ptr<Label> get_optional_config_label(std::string config_path, std::string label_name, std::string def) {

View File

@ -211,6 +211,15 @@ void Builder::node(drawtypes::Label *label, bool add_space)
{
if (!*label) return;
auto text = label->text;
if (label->maxlen > 0) {
text.resize(label->maxlen);
if (label->ellipsis && label->text.length() > label->maxlen)
text.append("...");
}
if ((label->ol.empty() && this->o > 0) || (this->o > 0 && label->margin > 0))
this->overline_close(true);
if ((label->ul.empty() && this->u > 0) || (this->u > 0 && label->margin > 0))
@ -228,7 +237,7 @@ void Builder::node(drawtypes::Label *label, bool add_space)
this->color(label->fg);
if (label->padding > 0)
this->space(label->padding);
this->node(label->text, label->font, add_space);
this->node(text, label->font, add_space);
if (label->padding > 0)
this->space(label->padding);
this->color_close(lazy_closing && label->margin > 0);