2016-11-25 12:55:15 +00:00
|
|
|
#include <utility>
|
|
|
|
|
2016-11-02 19:22:45 +00:00
|
|
|
#include "drawtypes/label.hpp"
|
|
|
|
|
2016-11-19 05:22:44 +00:00
|
|
|
POLYBAR_NS
|
2016-11-02 19:22:45 +00:00
|
|
|
|
|
|
|
namespace drawtypes {
|
|
|
|
string label::get() const {
|
|
|
|
return m_tokenized;
|
|
|
|
}
|
|
|
|
|
|
|
|
label::operator bool() {
|
|
|
|
return !m_text.empty();
|
|
|
|
}
|
|
|
|
|
|
|
|
label_t label::clone() {
|
2016-11-25 12:55:15 +00:00
|
|
|
return make_shared<label>(m_text, m_foreground, m_background, m_underline, m_overline, m_font, m_padding, m_margin,
|
|
|
|
m_maxlen, m_ellipsis, m_token_bounds);
|
2016-11-02 19:22:45 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
void label::reset_tokens() {
|
|
|
|
m_tokenized = m_text;
|
|
|
|
}
|
|
|
|
|
2016-11-25 12:55:15 +00:00
|
|
|
bool label::has_token(const string& token) {
|
2016-11-19 03:03:18 +00:00
|
|
|
return m_text.find(token) != string::npos;
|
|
|
|
}
|
|
|
|
|
2016-11-25 12:55:15 +00:00
|
|
|
void label::replace_token(const string& token, string replacement) {
|
|
|
|
if (!has_token(token)) {
|
2016-11-20 22:09:08 +00:00
|
|
|
return;
|
2016-11-25 12:55:15 +00:00
|
|
|
}
|
2016-11-20 22:09:08 +00:00
|
|
|
|
2016-11-22 02:35:30 +00:00
|
|
|
for (auto&& bound : m_token_bounds) {
|
2016-11-25 12:55:15 +00:00
|
|
|
if (token != bound.token) {
|
2016-11-22 02:35:30 +00:00
|
|
|
continue;
|
2016-11-25 12:55:15 +00:00
|
|
|
}
|
|
|
|
m_tokenized = string_util::replace_all_bounded(m_tokenized, token, move(replacement), bound.min, bound.max);
|
2016-11-22 02:35:30 +00:00
|
|
|
}
|
2016-11-02 19:22:45 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
void label::replace_defined_values(const label_t& label) {
|
2016-11-25 12:55:15 +00:00
|
|
|
if (!label->m_foreground.empty()) {
|
2016-11-02 19:22:45 +00:00
|
|
|
m_foreground = label->m_foreground;
|
2016-11-25 12:55:15 +00:00
|
|
|
}
|
|
|
|
if (!label->m_background.empty()) {
|
2016-11-02 19:22:45 +00:00
|
|
|
m_background = label->m_background;
|
2016-11-25 12:55:15 +00:00
|
|
|
}
|
|
|
|
if (!label->m_underline.empty()) {
|
2016-11-02 19:22:45 +00:00
|
|
|
m_underline = label->m_underline;
|
2016-11-25 12:55:15 +00:00
|
|
|
}
|
|
|
|
if (!label->m_overline.empty()) {
|
2016-11-02 19:22:45 +00:00
|
|
|
m_overline = label->m_overline;
|
2016-11-25 12:55:15 +00:00
|
|
|
}
|
2016-11-30 21:17:52 +00:00
|
|
|
if (label->m_font != 0) {
|
|
|
|
m_font = label->m_font;
|
|
|
|
}
|
|
|
|
if (label->m_padding != 0) {
|
|
|
|
m_padding = label->m_padding;
|
|
|
|
}
|
|
|
|
if (label->m_margin != 0) {
|
|
|
|
m_margin = label->m_margin;
|
|
|
|
}
|
|
|
|
if (label->m_maxlen != 0) {
|
|
|
|
m_maxlen = label->m_maxlen;
|
|
|
|
m_ellipsis = label->m_ellipsis;
|
|
|
|
}
|
2016-11-02 19:22:45 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
void label::copy_undefined(const label_t& label) {
|
2016-11-25 12:55:15 +00:00
|
|
|
if (m_foreground.empty() && !label->m_foreground.empty()) {
|
2016-11-02 19:22:45 +00:00
|
|
|
m_foreground = label->m_foreground;
|
2016-11-25 12:55:15 +00:00
|
|
|
}
|
|
|
|
if (m_background.empty() && !label->m_background.empty()) {
|
2016-11-02 19:22:45 +00:00
|
|
|
m_background = label->m_background;
|
2016-11-25 12:55:15 +00:00
|
|
|
}
|
|
|
|
if (m_underline.empty() && !label->m_underline.empty()) {
|
2016-11-02 19:22:45 +00:00
|
|
|
m_underline = label->m_underline;
|
2016-11-25 12:55:15 +00:00
|
|
|
}
|
|
|
|
if (m_overline.empty() && !label->m_overline.empty()) {
|
2016-11-02 19:22:45 +00:00
|
|
|
m_overline = label->m_overline;
|
2016-11-25 12:55:15 +00:00
|
|
|
}
|
|
|
|
if (m_font == 0 && label->m_font != 0) {
|
2016-11-02 19:22:45 +00:00
|
|
|
m_font = label->m_font;
|
2016-11-25 12:55:15 +00:00
|
|
|
}
|
|
|
|
if (m_padding == 0 && label->m_padding != 0) {
|
2016-11-02 19:22:45 +00:00
|
|
|
m_padding = label->m_padding;
|
2016-11-25 12:55:15 +00:00
|
|
|
}
|
|
|
|
if (m_margin == 0 && label->m_margin != 0) {
|
2016-11-02 19:22:45 +00:00
|
|
|
m_margin = label->m_margin;
|
2016-11-25 12:55:15 +00:00
|
|
|
}
|
2016-11-02 19:22:45 +00:00
|
|
|
if (m_maxlen == 0 && label->m_maxlen != 0) {
|
|
|
|
m_maxlen = label->m_maxlen;
|
|
|
|
m_ellipsis = label->m_ellipsis;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Create a label by loading values from the configuration
|
|
|
|
*/
|
2016-11-25 12:55:15 +00:00
|
|
|
label_t load_label(const config& conf, const string& section, string name, bool required, string def) {
|
2016-11-20 22:09:08 +00:00
|
|
|
vector<struct bounds> bound;
|
2016-11-21 05:33:50 +00:00
|
|
|
size_t start, end, pos;
|
2016-11-20 22:09:08 +00:00
|
|
|
|
2016-11-02 19:22:45 +00:00
|
|
|
name = string_util::ltrim(string_util::rtrim(name, '>'), '<');
|
|
|
|
|
|
|
|
string text;
|
|
|
|
|
2016-11-25 12:55:15 +00:00
|
|
|
if (required) {
|
2016-11-02 19:22:45 +00:00
|
|
|
text = conf.get<string>(section, name);
|
2016-11-25 12:55:15 +00:00
|
|
|
} else {
|
|
|
|
text = conf.get<string>(section, name, move(def));
|
|
|
|
}
|
2016-11-02 19:22:45 +00:00
|
|
|
|
2016-11-21 06:44:35 +00:00
|
|
|
string line{text};
|
|
|
|
|
2016-11-21 05:33:50 +00:00
|
|
|
while ((start = line.find('%')) != string::npos && (end = line.find('%', start + 1)) != string::npos) {
|
2016-11-22 20:14:34 +00:00
|
|
|
auto token = line.substr(start, end - start + 1);
|
2016-11-20 22:09:08 +00:00
|
|
|
|
2016-11-21 05:33:50 +00:00
|
|
|
// ignore false positives (lemonbar-style declarations)
|
2016-11-22 22:10:35 +00:00
|
|
|
if (token[1] == '{') {
|
|
|
|
line.erase(0, start + 1);
|
2016-11-21 05:33:50 +00:00
|
|
|
continue;
|
2016-11-22 22:10:35 +00:00
|
|
|
}
|
2016-11-21 05:33:50 +00:00
|
|
|
|
2016-11-22 22:10:35 +00:00
|
|
|
line.erase(start, end - start + 1);
|
2016-11-22 02:35:30 +00:00
|
|
|
bound.emplace_back(bounds{token, 0, 0});
|
2016-11-21 05:33:50 +00:00
|
|
|
|
|
|
|
// find min delimiter
|
2016-11-25 12:55:15 +00:00
|
|
|
if ((pos = token.find(':')) == string::npos) {
|
2016-11-21 05:33:50 +00:00
|
|
|
continue;
|
2016-11-25 12:55:15 +00:00
|
|
|
}
|
2016-11-21 05:33:50 +00:00
|
|
|
|
|
|
|
// strip min/max specifiers from the label string token
|
2016-11-22 02:35:30 +00:00
|
|
|
bound.back().token = token.substr(0, pos) + '%';
|
|
|
|
text = string_util::replace(text, token, bound.back().token);
|
2016-11-20 22:09:08 +00:00
|
|
|
|
2016-11-21 05:33:50 +00:00
|
|
|
try {
|
|
|
|
bound.back().min = std::stoul(&token[pos + 1], nullptr, 10);
|
|
|
|
} catch (const std::invalid_argument& err) {
|
|
|
|
continue;
|
2016-11-20 22:09:08 +00:00
|
|
|
}
|
2016-11-21 05:33:50 +00:00
|
|
|
|
|
|
|
// find max delimiter
|
2016-11-25 12:55:15 +00:00
|
|
|
if ((pos = token.find(':', pos + 1)) == string::npos) {
|
2016-11-21 05:33:50 +00:00
|
|
|
continue;
|
2016-11-25 12:55:15 +00:00
|
|
|
}
|
2016-11-21 05:33:50 +00:00
|
|
|
|
|
|
|
try {
|
|
|
|
bound.back().max = std::stoul(&token[pos + 1], nullptr, 10);
|
|
|
|
} catch (const std::invalid_argument& err) {
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
|
|
|
// ignore max lengths less than min
|
2016-11-25 12:55:15 +00:00
|
|
|
if (bound.back().max < bound.back().min) {
|
2016-11-21 05:33:50 +00:00
|
|
|
bound.back().max = 0;
|
2016-11-25 12:55:15 +00:00
|
|
|
}
|
2016-11-20 22:09:08 +00:00
|
|
|
}
|
2016-11-22 02:35:30 +00:00
|
|
|
|
2016-11-02 19:22:45 +00:00
|
|
|
// clang-format off
|
2016-11-25 12:55:15 +00:00
|
|
|
return make_shared<label>(text,
|
2016-11-02 19:22:45 +00:00
|
|
|
conf.get<string>(section, name + "-foreground", ""),
|
|
|
|
conf.get<string>(section, name + "-background", ""),
|
|
|
|
conf.get<string>(section, name + "-underline", ""),
|
|
|
|
conf.get<string>(section, name + "-overline", ""),
|
|
|
|
conf.get<int>(section, name + "-font", 0),
|
|
|
|
conf.get<int>(section, name + "-padding", 0),
|
|
|
|
conf.get<int>(section, name + "-margin", 0),
|
|
|
|
conf.get<size_t>(section, name + "-maxlen", 0),
|
2016-11-20 22:09:08 +00:00
|
|
|
conf.get<bool>(section, name + "-ellipsis", true),
|
2016-11-25 12:55:15 +00:00
|
|
|
bound);
|
2016-11-02 19:22:45 +00:00
|
|
|
// clang-format on
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Create a label by loading optional values from the configuration
|
|
|
|
*/
|
|
|
|
label_t load_optional_label(const config& conf, string section, string name, string def) {
|
2016-11-25 12:55:15 +00:00
|
|
|
return load_label(conf, move(section), move(name), false, move(def));
|
2016-11-02 19:22:45 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Create an icon by loading values from the configuration
|
|
|
|
*/
|
|
|
|
icon_t load_icon(const config& conf, string section, string name, bool required, string def) {
|
2016-11-25 12:55:15 +00:00
|
|
|
return load_label(conf, move(section), move(name), required, move(def));
|
2016-11-02 19:22:45 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Create an icon by loading optional values from the configuration
|
|
|
|
*/
|
|
|
|
icon_t load_optional_icon(const config& conf, string section, string name, string def) {
|
2016-11-25 12:55:15 +00:00
|
|
|
return load_icon(conf, move(section), move(name), false, move(def));
|
2016-11-02 19:22:45 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2016-11-19 05:22:44 +00:00
|
|
|
POLYBAR_NS_END
|