2016-11-25 07:55:15 -05:00
|
|
|
#include <utility>
|
|
|
|
|
2016-11-02 15:22:45 -04:00
|
|
|
#include "drawtypes/label.hpp"
|
2016-12-09 03:02:47 -05:00
|
|
|
#include "utils/factory.hpp"
|
2016-11-02 15:22:45 -04:00
|
|
|
|
2016-11-19 00:22:44 -05:00
|
|
|
POLYBAR_NS
|
2016-11-02 15:22:45 -04:00
|
|
|
|
|
|
|
namespace drawtypes {
|
|
|
|
string label::get() const {
|
|
|
|
return m_tokenized;
|
|
|
|
}
|
|
|
|
|
|
|
|
label::operator bool() {
|
2016-12-19 23:52:59 -05:00
|
|
|
return !m_tokenized.empty();
|
2016-11-02 15:22:45 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
label_t label::clone() {
|
2016-12-03 22:03:17 -05:00
|
|
|
vector<token> tokens;
|
|
|
|
if (!m_tokens.empty()) {
|
|
|
|
std::back_insert_iterator<decltype(tokens)> back_it(tokens);
|
|
|
|
std::copy(m_tokens.begin(), m_tokens.end(), back_it);
|
|
|
|
}
|
2016-12-09 03:02:47 -05:00
|
|
|
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));
|
2016-11-02 15:22:45 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
void label::reset_tokens() {
|
|
|
|
m_tokenized = m_text;
|
|
|
|
}
|
|
|
|
|
2016-11-25 07:55:15 -05:00
|
|
|
bool label::has_token(const string& token) {
|
2016-11-18 22:03:18 -05:00
|
|
|
return m_text.find(token) != string::npos;
|
|
|
|
}
|
|
|
|
|
2016-11-25 07:55:15 -05:00
|
|
|
void label::replace_token(const string& token, string replacement) {
|
|
|
|
if (!has_token(token)) {
|
2016-11-20 17:09:08 -05:00
|
|
|
return;
|
2016-11-25 07:55:15 -05:00
|
|
|
}
|
2016-11-20 17:09:08 -05:00
|
|
|
|
2016-12-03 22:03:17 -05:00
|
|
|
for (auto&& tok : m_tokens) {
|
|
|
|
if (token == tok.token) {
|
2016-12-30 22:32:11 -05:00
|
|
|
if (tok.max != 0_z && replacement.length() > tok.max) {
|
2016-12-03 22:03:17 -05:00
|
|
|
replacement = replacement.erase(tok.max) + tok.suffix;
|
2016-12-30 22:32:11 -05:00
|
|
|
} else if (tok.min != 0_z && replacement.length() < tok.min) {
|
|
|
|
replacement.insert(0_z, tok.min - replacement.length(), ' ');
|
2016-12-03 22:03:17 -05:00
|
|
|
}
|
|
|
|
m_tokenized = string_util::replace_all(m_tokenized, token, move(replacement));
|
2016-11-25 07:55:15 -05:00
|
|
|
}
|
2016-11-21 21:35:30 -05:00
|
|
|
}
|
2016-11-02 15:22:45 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
void label::replace_defined_values(const label_t& label) {
|
2016-11-25 07:55:15 -05:00
|
|
|
if (!label->m_foreground.empty()) {
|
2016-11-02 15:22:45 -04:00
|
|
|
m_foreground = label->m_foreground;
|
2016-11-25 07:55:15 -05:00
|
|
|
}
|
|
|
|
if (!label->m_background.empty()) {
|
2016-11-02 15:22:45 -04:00
|
|
|
m_background = label->m_background;
|
2016-11-25 07:55:15 -05:00
|
|
|
}
|
|
|
|
if (!label->m_underline.empty()) {
|
2016-11-02 15:22:45 -04:00
|
|
|
m_underline = label->m_underline;
|
2016-11-25 07:55:15 -05:00
|
|
|
}
|
|
|
|
if (!label->m_overline.empty()) {
|
2016-11-02 15:22:45 -04:00
|
|
|
m_overline = label->m_overline;
|
2016-11-25 07:55:15 -05:00
|
|
|
}
|
2016-11-30 16:17:52 -05:00
|
|
|
if (label->m_font != 0) {
|
|
|
|
m_font = label->m_font;
|
|
|
|
}
|
2016-12-30 22:32:11 -05:00
|
|
|
if (label->m_padding.left != 0U) {
|
2016-12-04 05:57:33 -05:00
|
|
|
m_padding.left = label->m_padding.left;
|
2016-11-30 16:17:52 -05:00
|
|
|
}
|
2016-12-30 22:32:11 -05:00
|
|
|
if (label->m_padding.right != 0U) {
|
2016-12-04 05:57:33 -05:00
|
|
|
m_padding.right = label->m_padding.right;
|
|
|
|
}
|
2016-12-30 22:32:11 -05:00
|
|
|
if (label->m_margin.left != 0U) {
|
2016-12-04 05:57:33 -05:00
|
|
|
m_margin.left = label->m_margin.left;
|
|
|
|
}
|
2016-12-30 22:32:11 -05:00
|
|
|
if (label->m_margin.right != 0U) {
|
2016-12-04 05:57:33 -05:00
|
|
|
m_margin.right = label->m_margin.right;
|
2016-11-30 16:17:52 -05:00
|
|
|
}
|
2016-12-30 22:32:11 -05:00
|
|
|
if (label->m_maxlen != 0_z) {
|
2016-11-30 16:17:52 -05:00
|
|
|
m_maxlen = label->m_maxlen;
|
|
|
|
m_ellipsis = label->m_ellipsis;
|
|
|
|
}
|
2016-11-02 15:22:45 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
void label::copy_undefined(const label_t& label) {
|
2016-11-25 07:55:15 -05:00
|
|
|
if (m_foreground.empty() && !label->m_foreground.empty()) {
|
2016-11-02 15:22:45 -04:00
|
|
|
m_foreground = label->m_foreground;
|
2016-11-25 07:55:15 -05:00
|
|
|
}
|
|
|
|
if (m_background.empty() && !label->m_background.empty()) {
|
2016-11-02 15:22:45 -04:00
|
|
|
m_background = label->m_background;
|
2016-11-25 07:55:15 -05:00
|
|
|
}
|
|
|
|
if (m_underline.empty() && !label->m_underline.empty()) {
|
2016-11-02 15:22:45 -04:00
|
|
|
m_underline = label->m_underline;
|
2016-11-25 07:55:15 -05:00
|
|
|
}
|
|
|
|
if (m_overline.empty() && !label->m_overline.empty()) {
|
2016-11-02 15:22:45 -04:00
|
|
|
m_overline = label->m_overline;
|
2016-11-25 07:55:15 -05:00
|
|
|
}
|
|
|
|
if (m_font == 0 && label->m_font != 0) {
|
2016-11-02 15:22:45 -04:00
|
|
|
m_font = label->m_font;
|
2016-11-25 07:55:15 -05:00
|
|
|
}
|
2016-12-30 22:32:11 -05:00
|
|
|
if (m_padding.left == 0U && label->m_padding.left != 0U) {
|
2016-12-04 05:57:33 -05:00
|
|
|
m_padding.left = label->m_padding.left;
|
|
|
|
}
|
2016-12-30 22:32:11 -05:00
|
|
|
if (m_padding.right == 0U && label->m_padding.right != 0U) {
|
2016-12-04 05:57:33 -05:00
|
|
|
m_padding.right = label->m_padding.right;
|
|
|
|
}
|
2016-12-30 22:32:11 -05:00
|
|
|
if (m_margin.left == 0U && label->m_margin.left != 0U) {
|
2016-12-04 05:57:33 -05:00
|
|
|
m_margin.left = label->m_margin.left;
|
2016-11-25 07:55:15 -05:00
|
|
|
}
|
2016-12-30 22:32:11 -05:00
|
|
|
if (m_margin.right == 0U && label->m_margin.right != 0U) {
|
2016-12-04 05:57:33 -05:00
|
|
|
m_margin.right = label->m_margin.right;
|
2016-11-25 07:55:15 -05:00
|
|
|
}
|
2016-12-30 22:32:11 -05:00
|
|
|
if (m_maxlen == 0_z && label->m_maxlen != 0_z) {
|
2016-11-02 15:22:45 -04:00
|
|
|
m_maxlen = label->m_maxlen;
|
|
|
|
m_ellipsis = label->m_ellipsis;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Create a label by loading values from the configuration
|
|
|
|
*/
|
2016-11-25 07:55:15 -05:00
|
|
|
label_t load_label(const config& conf, const string& section, string name, bool required, string def) {
|
2016-12-03 22:03:17 -05:00
|
|
|
vector<token> tokens;
|
2016-11-21 00:33:50 -05:00
|
|
|
size_t start, end, pos;
|
2016-11-20 17:09:08 -05:00
|
|
|
|
2016-12-14 00:51:07 -05:00
|
|
|
name = string_util::ltrim(string_util::rtrim(move(name), '>'), '<');
|
2016-11-02 15:22:45 -04:00
|
|
|
|
|
|
|
string text;
|
|
|
|
|
2016-12-15 03:29:14 -05:00
|
|
|
struct side_values padding {
|
|
|
|
}, margin{};
|
2016-12-04 05:57:33 -05:00
|
|
|
|
2016-11-25 07:55:15 -05:00
|
|
|
if (required) {
|
2016-12-30 17:32:05 -05:00
|
|
|
text = conf.get(section, name);
|
2016-11-25 07:55:15 -05:00
|
|
|
} else {
|
2016-12-30 17:32:05 -05:00
|
|
|
text = conf.get(section, name, move(def));
|
2016-11-25 07:55:15 -05:00
|
|
|
}
|
2016-11-02 15:22:45 -04:00
|
|
|
|
2016-12-04 11:28:57 -05:00
|
|
|
size_t len{text.size()};
|
|
|
|
|
|
|
|
if (len > 2 && text[0] == '"' && text[len - 1] == '"') {
|
|
|
|
text = text.substr(1, len - 2);
|
|
|
|
}
|
|
|
|
|
2016-12-04 05:57:33 -05:00
|
|
|
const auto get_left_right = [&](string key) {
|
2016-12-30 22:32:11 -05:00
|
|
|
auto value = conf.get(section, key, 0U);
|
2016-12-30 17:32:05 -05:00
|
|
|
auto left = conf.get(section, key + "-left", value);
|
|
|
|
auto right = conf.get(section, key + "-right", value);
|
2016-12-05 14:41:00 -05:00
|
|
|
return side_values{static_cast<uint16_t>(left), static_cast<uint16_t>(right)};
|
2016-12-04 05:57:33 -05:00
|
|
|
};
|
|
|
|
|
|
|
|
padding = get_left_right(name + "-padding");
|
|
|
|
margin = get_left_right(name + "-margin");
|
|
|
|
|
2016-11-21 01:44:35 -05:00
|
|
|
string line{text};
|
|
|
|
|
2016-11-21 00:33:50 -05:00
|
|
|
while ((start = line.find('%')) != string::npos && (end = line.find('%', start + 1)) != string::npos) {
|
2016-12-03 22:03:17 -05:00
|
|
|
auto token_str = line.substr(start, end - start + 1);
|
2016-11-20 17:09:08 -05:00
|
|
|
|
2016-11-21 00:33:50 -05:00
|
|
|
// ignore false positives (lemonbar-style declarations)
|
2016-12-03 22:03:17 -05:00
|
|
|
if (token_str[1] == '{') {
|
2016-11-22 17:10:35 -05:00
|
|
|
line.erase(0, start + 1);
|
2016-11-21 00:33:50 -05:00
|
|
|
continue;
|
2016-11-22 17:10:35 -05:00
|
|
|
}
|
2016-11-21 00:33:50 -05:00
|
|
|
|
2016-11-22 17:10:35 -05:00
|
|
|
line.erase(start, end - start + 1);
|
2016-12-30 22:32:11 -05:00
|
|
|
tokens.emplace_back(token{token_str, 0_z, 0_z});
|
2016-12-03 22:03:17 -05:00
|
|
|
auto& token = tokens.back();
|
2016-11-21 00:33:50 -05:00
|
|
|
|
|
|
|
// find min delimiter
|
2016-12-03 22:03:17 -05:00
|
|
|
if ((pos = token_str.find(':')) == string::npos) {
|
2016-11-21 00:33:50 -05:00
|
|
|
continue;
|
2016-11-25 07:55:15 -05:00
|
|
|
}
|
2016-11-21 00:33:50 -05:00
|
|
|
|
|
|
|
// strip min/max specifiers from the label string token
|
2016-12-03 22:03:17 -05:00
|
|
|
token.token = token_str.substr(0, pos) + '%';
|
|
|
|
text = string_util::replace(text, token_str, token.token);
|
2016-11-20 17:09:08 -05:00
|
|
|
|
2016-11-21 00:33:50 -05:00
|
|
|
try {
|
2016-12-03 22:03:17 -05:00
|
|
|
token.min = std::stoul(&token_str[pos + 1], nullptr, 10);
|
2016-11-21 00:33:50 -05:00
|
|
|
} catch (const std::invalid_argument& err) {
|
|
|
|
continue;
|
2016-11-20 17:09:08 -05:00
|
|
|
}
|
2016-11-21 00:33:50 -05:00
|
|
|
|
|
|
|
// find max delimiter
|
2016-12-03 22:03:17 -05:00
|
|
|
if ((pos = token_str.find(':', pos + 1)) == string::npos) {
|
2016-11-21 00:33:50 -05:00
|
|
|
continue;
|
2016-11-25 07:55:15 -05:00
|
|
|
}
|
2016-11-21 00:33:50 -05:00
|
|
|
|
|
|
|
try {
|
2016-12-03 22:03:17 -05:00
|
|
|
token.max = std::stoul(&token_str[pos + 1], nullptr, 10);
|
2016-11-21 00:33:50 -05:00
|
|
|
} catch (const std::invalid_argument& err) {
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
|
|
|
// ignore max lengths less than min
|
2016-12-03 22:03:17 -05:00
|
|
|
if (token.max < token.min) {
|
2016-12-30 22:32:11 -05:00
|
|
|
token.max = 0_z;
|
2016-12-03 22:03:17 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
// find suffix delimiter
|
|
|
|
if ((pos = token_str.find(':', pos + 1)) != string::npos) {
|
|
|
|
token.suffix = token_str.substr(pos + 1, end - pos - 1);
|
2016-11-25 07:55:15 -05:00
|
|
|
}
|
2016-11-20 17:09:08 -05:00
|
|
|
}
|
2016-11-21 21:35:30 -05:00
|
|
|
|
2016-11-02 15:22:45 -04:00
|
|
|
// clang-format off
|
2016-12-09 03:02:47 -05:00
|
|
|
return factory_util::shared<label>(text,
|
2016-12-30 17:32:05 -05:00
|
|
|
conf.get(section, name + "-foreground", ""s),
|
|
|
|
conf.get(section, name + "-background", ""s),
|
|
|
|
conf.get(section, name + "-underline", ""s),
|
|
|
|
conf.get(section, name + "-overline", ""s),
|
|
|
|
conf.get(section, name + "-font", 0),
|
2016-12-04 05:57:33 -05:00
|
|
|
padding,
|
|
|
|
margin,
|
2016-12-30 17:32:05 -05:00
|
|
|
conf.get(section, name + "-maxlen", 0_z),
|
|
|
|
conf.get(section, name + "-ellipsis", true),
|
2016-12-03 22:03:17 -05:00
|
|
|
move(tokens));
|
2016-11-02 15:22:45 -04: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 07:55:15 -05:00
|
|
|
return load_label(conf, move(section), move(name), false, move(def));
|
2016-11-02 15:22:45 -04: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 07:55:15 -05:00
|
|
|
return load_label(conf, move(section), move(name), required, move(def));
|
2016-11-02 15:22:45 -04: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 07:55:15 -05:00
|
|
|
return load_icon(conf, move(section), move(name), false, move(def));
|
2016-11-02 15:22:45 -04:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2016-11-19 00:22:44 -05:00
|
|
|
POLYBAR_NS_END
|