From f754fa27103e0df9a10b0083babd7ad29605e016 Mon Sep 17 00:00:00 2001 From: Michael Carlberg Date: Tue, 25 Apr 2017 20:26:48 +0200 Subject: [PATCH] feat(parser): Keep track of formatting colors #544 --- include/components/parser.hpp | 8 ++++++++ src/components/parser.cpp | 28 ++++++++++++++++++++++------ 2 files changed, 30 insertions(+), 6 deletions(-) diff --git a/include/components/parser.hpp b/include/components/parser.hpp index 8aeba5c0..993cd59e 100644 --- a/include/components/parser.hpp +++ b/include/components/parser.hpp @@ -1,5 +1,7 @@ #pragma once +#include + #include "common.hpp" #include "errors.hpp" @@ -28,6 +30,7 @@ class parser { void codeblock(string&& data, const bar_settings& bar); size_t text(string&& data); + unsigned int color(std::stack& color_stack, string& value, unsigned int fallback); unsigned int parse_color(const string& s, unsigned int fallback = 0); int parse_fontindex(const string& s); attribute parse_attr(const char attr); @@ -38,6 +41,11 @@ class parser { signal_emitter& m_sig; vector m_actions; unique_ptr m_parser; + + std::stack m_fg; + std::stack m_bg; + std::stack m_ul; + std::stack m_ol; }; POLYBAR_NS_END diff --git a/src/components/parser.cpp b/src/components/parser.cpp index f30e5802..7efc9f9d 100644 --- a/src/components/parser.cpp +++ b/src/components/parser.cpp @@ -76,13 +76,15 @@ void parser::codeblock(string&& data, const bar_settings& bar) { } switch (tag) { - case 'B': - m_sig.emit(change_background{parse_color(value, 0)}); + case 'B': { + m_sig.emit(change_background{color(m_bg, value, 0UL)}); break; + } - case 'F': - m_sig.emit(change_foreground{parse_color(value, bar.foreground)}); + case 'F': { + m_sig.emit(change_foreground{color(m_fg, value, bar.foreground)}); break; + } case 'T': m_sig.emit(change_font{parse_fontindex(value)}); @@ -94,11 +96,11 @@ void parser::codeblock(string&& data, const bar_settings& bar) { break; 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; 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; case 'R': @@ -177,6 +179,20 @@ size_t parser::text(string&& data) { return data.size(); } +/** + * Determine color using passed stack and input value + */ +unsigned int parser::color(std::stack& 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 */