polybar/src/modules/meta/base.cpp

171 lines
5.0 KiB
C++
Raw Normal View History

2016-11-25 12:55:15 +00:00
#include <utility>
2016-11-25 07:42:31 +00:00
#include "components/builder.hpp"
#include "drawtypes/label.hpp"
2016-12-04 03:11:47 +00:00
#include "modules/meta/base.hpp"
2016-11-20 22:04:31 +00:00
POLYBAR_NS
namespace modules {
// module_format {{{
string module_format::decorate(builder* builder, string output) {
2016-12-27 03:03:46 +00:00
if (output.empty()) {
builder->flush();
return "";
}
2016-11-25 12:55:15 +00:00
if (offset != 0) {
2016-11-20 22:04:31 +00:00
builder->offset(offset);
2016-11-25 12:55:15 +00:00
}
if (margin > 0) {
2016-11-20 22:04:31 +00:00
builder->space(margin);
2016-11-25 12:55:15 +00:00
}
if (!bg.empty()) {
2016-11-20 22:04:31 +00:00
builder->background(bg);
2016-11-25 12:55:15 +00:00
}
if (!fg.empty()) {
2016-11-20 22:04:31 +00:00
builder->color(fg);
2016-11-25 12:55:15 +00:00
}
if (!ul.empty()) {
2016-11-20 22:04:31 +00:00
builder->underline(ul);
2016-11-25 12:55:15 +00:00
}
if (!ol.empty()) {
2016-11-20 22:04:31 +00:00
builder->overline(ol);
2016-11-25 12:55:15 +00:00
}
2019-01-13 21:10:34 +00:00
if(font > 0) {
builder->font(font);
}
2016-11-25 12:55:15 +00:00
if (padding > 0) {
2016-11-20 22:04:31 +00:00
builder->space(padding);
2016-11-25 12:55:15 +00:00
}
2016-11-20 22:04:31 +00:00
builder->node(prefix);
if (!bg.empty()) {
builder->background(bg);
}
if (!fg.empty()) {
builder->color(fg);
}
if (!ul.empty()) {
builder->underline(ul);
}
if (!ol.empty()) {
builder->overline(ol);
}
builder->append(move(output));
builder->node(suffix);
2016-11-20 22:04:31 +00:00
2016-11-25 12:55:15 +00:00
if (padding > 0) {
2016-11-20 22:04:31 +00:00
builder->space(padding);
2016-11-25 12:55:15 +00:00
}
2019-01-13 21:10:34 +00:00
if(font > 0) {
builder->font_close();
}
2016-11-25 12:55:15 +00:00
if (!ol.empty()) {
2016-11-20 22:04:31 +00:00
builder->overline_close();
2016-11-25 12:55:15 +00:00
}
if (!ul.empty()) {
2016-11-20 22:04:31 +00:00
builder->underline_close();
2016-11-25 12:55:15 +00:00
}
if (!fg.empty()) {
2016-11-20 22:04:31 +00:00
builder->color_close();
2016-11-25 12:55:15 +00:00
}
if (!bg.empty()) {
2016-11-20 22:04:31 +00:00
builder->background_close();
2016-11-25 12:55:15 +00:00
}
if (margin > 0) {
2016-11-20 22:04:31 +00:00
builder->space(margin);
2016-11-25 12:55:15 +00:00
}
2016-11-20 22:04:31 +00:00
return builder->flush();
}
// }}}
// module_formatter {{{
void module_formatter::add(string name, string fallback, vector<string>&& tags, vector<string>&& whitelist) {
const auto formatdef = [&](
const string& param, const auto& fallback) { return m_conf.get("settings", "format-" + param, fallback); };
2016-11-20 22:04:31 +00:00
auto format = make_unique<module_format>();
format->value = m_conf.get(m_modname, name, move(fallback));
format->fg = m_conf.get(m_modname, name + "-foreground", formatdef("foreground", format->fg));
format->bg = m_conf.get(m_modname, name + "-background", formatdef("background", format->bg));
format->ul = m_conf.get(m_modname, name + "-underline", formatdef("underline", format->ul));
format->ol = m_conf.get(m_modname, name + "-overline", formatdef("overline", format->ol));
format->ulsize = m_conf.get(m_modname, name + "-underline-size", formatdef("underline-size", format->ulsize));
format->olsize = m_conf.get(m_modname, name + "-overline-size", formatdef("overline-size", format->olsize));
format->spacing = m_conf.get(m_modname, name + "-spacing", formatdef("spacing", format->spacing));
format->padding = m_conf.get(m_modname, name + "-padding", formatdef("padding", format->padding));
format->margin = m_conf.get(m_modname, name + "-margin", formatdef("margin", format->margin));
format->offset = m_conf.get(m_modname, name + "-offset", formatdef("offset", format->offset));
2019-01-13 21:10:34 +00:00
format->font = m_conf.get(m_modname, name + "-font", formatdef("font", format->font));
2016-11-20 22:04:31 +00:00
format->tags.swap(tags);
try {
format->prefix = load_label(m_conf, m_modname, name + "-prefix");
} catch (const key_error& err) {
// prefix not defined
}
try {
format->suffix = load_label(m_conf, m_modname, name + "-suffix");
} catch (const key_error& err) {
// suffix not defined
}
vector<string> tag_collection;
tag_collection.reserve(format->tags.size() + whitelist.size());
tag_collection.insert(tag_collection.end(), format->tags.begin(), format->tags.end());
tag_collection.insert(tag_collection.end(), whitelist.begin(), whitelist.end());
size_t start, end;
string value{format->value};
while ((start = value.find('<')) != string::npos && (end = value.find('>', start)) != string::npos) {
if (start > 0) {
value.erase(0, start);
end -= start;
start = 0;
}
string tag{value.substr(start, end + 1)};
if (find(tag_collection.begin(), tag_collection.end(), tag) == tag_collection.end()) {
2016-12-31 03:32:11 +00:00
throw undefined_format_tag(tag + " is not a valid format tag for \"" + name + "\"");
2016-11-25 12:55:15 +00:00
}
value.erase(0, tag.size());
2016-11-20 22:04:31 +00:00
}
2016-12-27 03:03:46 +00:00
m_formats.insert(make_pair(move(name), move(format)));
2016-11-20 22:04:31 +00:00
}
2016-11-25 12:55:15 +00:00
bool module_formatter::has(const string& tag, const string& format_name) {
2016-11-20 22:04:31 +00:00
auto format = m_formats.find(format_name);
2016-11-25 12:55:15 +00:00
if (format == m_formats.end()) {
throw undefined_format(format_name);
}
2016-11-20 22:04:31 +00:00
return format->second->value.find(tag) != string::npos;
}
2016-11-25 12:55:15 +00:00
bool module_formatter::has(const string& tag) {
for (auto&& format : m_formats) {
if (format.second->value.find(tag) != string::npos) {
2016-11-20 22:04:31 +00:00
return true;
2016-11-25 12:55:15 +00:00
}
}
2016-11-20 22:04:31 +00:00
return false;
}
2016-11-25 12:55:15 +00:00
shared_ptr<module_format> module_formatter::get(const string& format_name) {
2016-11-20 22:04:31 +00:00
auto format = m_formats.find(format_name);
2016-11-25 12:55:15 +00:00
if (format == m_formats.end()) {
2016-11-20 22:04:31 +00:00
throw undefined_format("Format \"" + format_name + "\" has not been added");
2016-11-25 12:55:15 +00:00
}
2016-11-20 22:04:31 +00:00
return format->second;
}
// }}}
}
POLYBAR_NS_END