feat(parser): Keep track of formatting colors #544

This commit is contained in:
Michael Carlberg 2017-04-25 20:26:48 +02:00
parent d19b610109
commit f754fa2710
2 changed files with 30 additions and 6 deletions

View File

@ -1,5 +1,7 @@
#pragma once #pragma once
#include <stack>
#include "common.hpp" #include "common.hpp"
#include "errors.hpp" #include "errors.hpp"
@ -28,6 +30,7 @@ class parser {
void codeblock(string&& data, const bar_settings& bar); void codeblock(string&& data, const bar_settings& bar);
size_t text(string&& data); size_t text(string&& data);
unsigned int color(std::stack<unsigned int>& color_stack, string& value, unsigned int fallback);
unsigned int parse_color(const string& s, unsigned int fallback = 0); unsigned int parse_color(const string& s, unsigned int fallback = 0);
int parse_fontindex(const string& s); int parse_fontindex(const string& s);
attribute parse_attr(const char attr); attribute parse_attr(const char attr);
@ -38,6 +41,11 @@ class parser {
signal_emitter& m_sig; signal_emitter& m_sig;
vector<int> m_actions; vector<int> m_actions;
unique_ptr<parser> m_parser; 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;
}; };
POLYBAR_NS_END POLYBAR_NS_END

View File

@ -76,13 +76,15 @@ void parser::codeblock(string&& data, const bar_settings& bar) {
} }
switch (tag) { switch (tag) {
case 'B': case 'B': {
m_sig.emit(change_background{parse_color(value, 0)}); m_sig.emit(change_background{color(m_bg, value, 0UL)});
break; break;
}
case 'F': case 'F': {
m_sig.emit(change_foreground{parse_color(value, bar.foreground)}); m_sig.emit(change_foreground{color(m_fg, value, bar.foreground)});
break; break;
}
case 'T': case 'T':
m_sig.emit(change_font{parse_fontindex(value)}); m_sig.emit(change_font{parse_fontindex(value)});
@ -94,11 +96,11 @@ void parser::codeblock(string&& data, const bar_settings& bar) {
break; break;
case 'u': case 'u':
m_sig.emit(change_underline{parse_color(value, bar.underline.color)}); m_sig.emit(change_underline{color(m_ul, value, bar.underline.color)});
break; break;
case 'o': case 'o':
m_sig.emit(change_overline{parse_color(value, bar.overline.color)}); m_sig.emit(change_underline{color(m_ul, value, bar.overline.color)});
break; break;
case 'R': case 'R':
@ -177,6 +179,20 @@ size_t parser::text(string&& data) {
return data.size(); return data.size();
} }
/**
* Determine color using passed stack and input value
*/
unsigned int parser::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(value, !color_stack.empty() ? color_stack.top() : fallback);
if (!value.empty() && value[0] != '-') {
color_stack.push(parsed_value);
}
return parsed_value;
}
/** /**
* Process color hex string and convert it to the correct value * Process color hex string and convert it to the correct value
*/ */