polybar/src/modules/meta/base.cpp

143 lines
3.7 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
}
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
if (!output.empty()) {
builder->node(prefix);
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
}
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) {
2016-12-27 03:03:46 +00:00
using namespace std::string_literals;
2016-11-20 22:04:31 +00:00
auto format = make_unique<module_format>();
format->value = m_conf.get(m_modname, name, move(fallback));
2016-12-27 03:03:46 +00:00
format->fg = m_conf.get(m_modname, name + "-foreground", ""s);
format->bg = m_conf.get(m_modname, name + "-background", ""s);
format->ul = m_conf.get(m_modname, name + "-underline", ""s);
format->ol = m_conf.get(m_modname, name + "-overline", ""s);
2016-12-27 03:58:41 +00:00
format->spacing = m_conf.get(m_modname, name + "-spacing", 1_z);
2016-12-27 03:03:46 +00:00
format->padding = m_conf.get(m_modname, name + "-padding", 0_z);
format->margin = m_conf.get(m_modname, name + "-margin", 0_z);
format->offset = m_conf.get(m_modname, name + "-offset", 0_z);
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
}
2016-11-20 22:04:31 +00:00
for (auto&& tag : string_util::split(format->value, ' ')) {
2016-11-25 12:55:15 +00:00
if (tag[0] != '<' || tag[tag.length() - 1] != '>') {
2016-11-20 22:04:31 +00:00
continue;
2016-12-27 03:03:46 +00:00
} else if (find(format->tags.begin(), format->tags.end(), tag) != format->tags.end()) {
2016-11-20 22:04:31 +00:00
continue;
2016-12-27 03:03:46 +00:00
} else if (find(whitelist.begin(), whitelist.end(), tag) != whitelist.end()) {
2016-11-20 22:04:31 +00:00
continue;
2016-12-27 03:03:46 +00:00
} else {
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
}
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