Skip to content

Commit

Permalink
fix: display invalid tags as they are
Browse files Browse the repository at this point in the history
Fixes #2713
  • Loading branch information
Zaphoood committed May 5, 2022
1 parent f0dbb4c commit ae765f2
Show file tree
Hide file tree
Showing 5 changed files with 18 additions and 49 deletions.
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0

### Fixed
- Waiting for double click interval on modules that don't have a double click action ([`#2663`](https://github.com/polybar/polybar/issues/2663), [`#2695`](https://github.com/polybar/polybar/pull/2695))
- Display invalid format tags on the bar instead of hiding them ([`#2713`](https://github.com/polybar/polybar/issues/2713))

## [3.6.3] - 2022-05-04
### Fixed
Expand Down
6 changes: 3 additions & 3 deletions include/modules/meta/base.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -83,15 +83,15 @@ namespace modules {
public:
explicit module_formatter(const config& conf, string modname) : m_conf(conf), m_modname(modname) {}

void add(string name, string fallback, vector<string>&& tags, vector<string>&& whitelist = {});
void add_optional(string name, vector<string>&& tags, vector<string>&& whitelist = {});
void add(string name, string fallback, vector<string>&& tags);
void add_optional(string name, vector<string>&& tags);
bool has(const string& tag, const string& format_name);
bool has(const string& tag);
bool has_format(const string& format_name);
shared_ptr<module_format> get(const string& format_name);

protected:
void add_value(string&& name, string&& value, vector<string>&& tags, vector<string>&& whitelist);
void add_value(string&& name, string&& value, vector<string>&& tags);

const config& m_conf;
string m_modname;
Expand Down
29 changes: 8 additions & 21 deletions include/modules/meta/base.inl
Original file line number Diff line number Diff line change
Expand Up @@ -249,31 +249,18 @@ namespace modules {
non_tag = string_util::ltrim(move(non_tag), ' ');
}
}
m_builder->node(non_tag);

if (has_tags) {
// format-spacing is added between all tags
m_builder->spacing(format->spacing);
}
has_tags = true;

string tag = value.substr(start, end - start + 1);
bool tag_built = CONST_MOD(Impl).build(&tag_builder, tag);
string tag_content = tag_builder.flush();

/*
* Remove exactly one space between two tags if the second tag was not built.
*/
if (!tag_built && has_tags && !format->spacing) {
if (!non_tag.empty() && non_tag.back() == ' ') {
non_tag.erase(non_tag.size() - 1);
}
}

m_builder->node(non_tag);

if (tag_built) {
if (has_tags) {
// format-spacing is added between all tags
m_builder->spacing(format->spacing);
}

m_builder->node(tag_content);
has_tags = true;
}
m_builder->node(tag_built ? tag_content : tag);

cursor = end + 1;
}
Expand Down
2 changes: 1 addition & 1 deletion src/modules/bspwm.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -63,7 +63,7 @@ namespace modules {
m_fuzzy_match = m_conf.get(name(), "fuzzy-match", m_fuzzy_match);

// Add formats and create components
m_formatter->add(DEFAULT_FORMAT, TAG_LABEL_STATE, {TAG_LABEL_STATE}, {TAG_LABEL_MONITOR, TAG_LABEL_MODE});
m_formatter->add(DEFAULT_FORMAT, TAG_LABEL_STATE, {TAG_LABEL_STATE, TAG_LABEL_MONITOR, TAG_LABEL_MODE});

if (m_formatter->has(TAG_LABEL_MONITOR)) {
m_monitorlabel = load_optional_label(m_conf, name(), "label-monitor", DEFAULT_MONITOR_LABEL);
Expand Down
29 changes: 5 additions & 24 deletions src/modules/meta/base.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -86,7 +86,7 @@ namespace modules {
// }}}
// module_formatter {{{

void module_formatter::add_value(string&& name, string&& value, vector<string>&& tags, vector<string>&& whitelist) {
void module_formatter::add_value(string&& name, string&& value, vector<string>&& tags) {
const auto formatdef = [&](const string& param, const auto& fallback) {
return m_conf.get("settings", "format-" + param, fallback);
};
Expand Down Expand Up @@ -118,37 +118,18 @@ namespace modules {
// 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;
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()) {
throw undefined_format_tag(tag + " is not a valid format tag for \"" + name + "\"");
}
value.erase(0, tag.size());
}

m_formats.insert(make_pair(move(name), move(format)));
}

void module_formatter::add(string name, string fallback, vector<string>&& tags, vector<string>&& whitelist) {
void module_formatter::add(string name, string fallback, vector<string>&& tags) {
string value = m_conf.get(m_modname, name, move(fallback));
add_value(move(name), move(value), forward<vector<string>>(tags), forward<vector<string>>(whitelist));
add_value(move(name), move(value), forward<vector<string>>(tags));
}

void module_formatter::add_optional(string name, vector<string>&& tags, vector<string>&& whitelist) {
void module_formatter::add_optional(string name, vector<string>&& tags) {
if (m_conf.has(m_modname, name)) {
string value = m_conf.get(m_modname, name);
add_value(move(name), move(value), move(tags), move(whitelist));
add_value(move(name), move(value), move(tags));
}
}

Expand Down

0 comments on commit ae765f2

Please sign in to comment.