1
0
Fork 0
mirror of https://github.com/polybar/polybar.git synced 2024-11-11 13:50:56 -05:00

Merge pull request #729 from NBonaparte/fix-prefix-suffix

fix(modules): Separate prefix/suffix tags, revert tag stack
This commit is contained in:
NBonaparte 2017-09-24 14:00:34 -07:00 committed by GitHub
commit 5b7d7b8232
3 changed files with 34 additions and 65 deletions

View file

@ -1,7 +1,5 @@
#pragma once
#include <stack>
#include "common.hpp"
#include "errors.hpp"
@ -30,9 +28,8 @@ class parser {
void codeblock(string&& data, const bar_settings& bar);
size_t text(string&& data);
unsigned int parse_color(std::stack<unsigned int>& color_stack, string& value, unsigned int fallback);
unsigned int parse_color_string(const string& s, unsigned int fallback = 0);
int parse_fontindex(const string& value);
unsigned int parse_color(const string& s, unsigned int fallback = 0);
int parse_fontindex(const string& s);
attribute parse_attr(const char attr);
mousebtn parse_action_btn(const string& data);
string parse_action_cmd(string&& data);
@ -41,12 +38,6 @@ class parser {
signal_emitter& m_sig;
vector<int> m_actions;
unique_ptr<parser> m_parser;
std::stack<unsigned int> m_fg;
std::stack<unsigned int> m_bg;
std::stack<unsigned int> m_ul;
std::stack<unsigned int> m_ol;
std::stack<int> m_fonts;
};
POLYBAR_NS_END

View file

@ -45,12 +45,6 @@ void parser::parse(const bar_settings& bar, string data) {
}
}
m_fg = std::stack<unsigned int>();
m_bg = std::stack<unsigned int>();
m_ul = std::stack<unsigned int>();
m_ol = std::stack<unsigned int>();
m_fonts = std::stack<int>();
if (!m_actions.empty()) {
throw unclosed_actionblocks(to_string(m_actions.size()) + " unclosed action block(s)");
}
@ -82,31 +76,29 @@ void parser::codeblock(string&& data, const bar_settings& bar) {
}
switch (tag) {
case 'B': {
m_sig.emit(change_background{parse_color(m_bg, value, bar.background)});
case 'B':
m_sig.emit(change_background{parse_color(value, bar.background)});
break;
}
case 'F': {
m_sig.emit(change_foreground{parse_color(m_fg, value, bar.foreground)});
case 'F':
m_sig.emit(change_foreground{parse_color(value, bar.foreground)});
break;
}
case 'T':
m_sig.emit(change_font{parse_fontindex(value)});
break;
case 'U':
m_sig.emit(change_underline{parse_color(m_ul, value, bar.underline.color)});
m_sig.emit(change_overline{parse_color(m_ol, value, bar.overline.color)});
m_sig.emit(change_underline{parse_color(value, bar.underline.color)});
m_sig.emit(change_overline{parse_color(value, bar.overline.color)});
break;
case 'u':
m_sig.emit(change_underline{parse_color(m_ul, value, bar.underline.color)});
m_sig.emit(change_underline{parse_color(value, bar.underline.color)});
break;
case 'o':
m_sig.emit(change_overline{parse_color(m_ol, value, bar.overline.color)});
m_sig.emit(change_overline{parse_color(value, bar.overline.color)});
break;
case 'R':
@ -184,24 +176,10 @@ size_t parser::text(string&& data) {
return data.size();
}
/**
* Determine color using passed stack and input value
*/
unsigned int parser::parse_color(std::stack<unsigned int>& color_stack, string& value, unsigned int fallback) {
if (!color_stack.empty() && !value.empty() && value[0] == '-') {
color_stack.pop();
}
auto parsed_value = parse_color_string(value, !color_stack.empty() ? color_stack.top() : fallback);
if (!value.empty() && value[0] != '-' && (color_stack.empty() || (parsed_value != color_stack.top()))) {
color_stack.push(parsed_value);
}
return parsed_value;
}
/**
* Process color hex string and convert it to the correct value
*/
unsigned int parser::parse_color_string(const string& s, unsigned int fallback) {
unsigned int parser::parse_color(const string& s, unsigned int fallback) {
if (!s.empty() && s[0] != '-') {
return color_util::parse(s, fallback);
}
@ -211,26 +189,15 @@ unsigned int parser::parse_color_string(const string& s, unsigned int fallback)
/**
* Process font index and convert it to the correct value
*/
int parser::parse_fontindex(const string& value) {
auto font_index = 0;
auto reset = value.empty() || value[0] == '-';
if (reset && !m_fonts.empty()) {
m_fonts.pop();
} else if (!reset) {
try {
font_index = std::stoul(value, nullptr, 10);
m_fonts.push(font_index);
return font_index;
} catch (const std::invalid_argument& err) {
return font_index;
}
int parser::parse_fontindex(const string& s) {
if (s.empty() || s[0] == '-') {
return 0;
}
if (!m_fonts.empty()) {
return m_fonts.top();
} else {
return font_index;
try {
return std::stoul(s, nullptr, 10);
} catch (const std::invalid_argument& err) {
return 0;
}
}

View file

@ -14,7 +14,6 @@ namespace modules {
builder->flush();
return "";
}
if (offset != 0) {
builder->offset(offset);
}
@ -37,11 +36,23 @@ namespace modules {
builder->space(padding);
}
if (!output.empty()) {
builder->node(prefix);
builder->append(move(output));
builder->node(suffix);
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);
if (padding > 0) {
builder->space(padding);