From 6857ba5e56c1ad6c80475e56747e4466ce7a12e8 Mon Sep 17 00:00:00 2001 From: Toni500git Date: Tue, 22 Oct 2024 15:48:24 +0200 Subject: [PATCH] gui: fix color parsing by closing the tag before a new one also fix android_small.txt --- assets/ascii/android_small.txt | 2 +- include/parse.hpp | 2 +- src/parse.cpp | 49 ++++++++++------------------------ 3 files changed, 16 insertions(+), 37 deletions(-) diff --git a/assets/ascii/android_small.txt b/assets/ascii/android_small.txt index a48d1633..d3754d1e 100644 --- a/assets/ascii/android_small.txt +++ b/assets/ascii/android_small.txt @@ -1,6 +1,6 @@ ${green} ;, ,; ${green} ';,.-----.,;' ${green} ,' ', -${green} / O O \ +${green} / O O \\ ${green}| | ${green}'-----------------' diff --git a/include/parse.hpp b/include/parse.hpp index 0c813107..f0676b49 100644 --- a/include/parse.hpp +++ b/include/parse.hpp @@ -15,7 +15,7 @@ struct parse_args_t const Config& config; const colors_t& colors; const bool parsingLayout; - bool& firstrun_noclr; + bool& firstrun_clr; }; /* Parse input, in-place, with data from systemInfo. diff --git a/src/parse.cpp b/src/parse.cpp index d4115b39..21dc03b8 100644 --- a/src/parse.cpp +++ b/src/parse.cpp @@ -251,7 +251,6 @@ std::optional parse_command_tag(Parser& parser, parse_args_t& parse return cmd_output; } -std::uint16_t endspan_count = 0; std::optional parse_color_tag(Parser& parser, parse_args_t& parse_args, const bool evaluate) { if (!parser.try_read('{')) @@ -269,6 +268,7 @@ std::optional parse_color_tag(Parser& parser, parse_args_t& parse_a const colors_t& colors = parse_args.colors; const size_t taglen = color.length() + "${}"_len; const size_t tagpos = parse_args.pureOutput.find("${" + color + "}"); + const std::string& endspan = (!parse_args.firstrun_clr ? "" : ""); if (config.m_disable_colors) { @@ -318,25 +318,11 @@ std::optional parse_color_tag(Parser& parser, parse_args_t& parse_a jumpauto: if (color == "1") { - if (parse_args.firstrun_noclr) - output += config.gui ? "" : NOCOLOR_BOLD; - else - { - output += config.gui ? "" : NOCOLOR_BOLD; - if (endspan_count > 0) - endspan_count--; - } + output += config.gui ? endspan + "" : NOCOLOR_BOLD; } else if (color == "0") { - if (parse_args.firstrun_noclr) - output += config.gui ? "" : NOCOLOR; - else - { - output += config.gui ? "" : NOCOLOR; - if (endspan_count > 0) - endspan_count--; - } + output += config.gui ? endspan + "" : NOCOLOR; } else { @@ -439,7 +425,7 @@ std::optional parse_color_tag(Parser& parser, parse_args_t& parse_a tagfmt += "fgcolor='" + str_clr.substr(pos) + "' "; tagfmt.pop_back(); - output += "<" + tagfmt + ">"; + output += endspan + "<" + tagfmt + ">"; } // "\\e" is for checking in the ascii_art, \033 in the config @@ -451,7 +437,7 @@ std::optional parse_color_tag(Parser& parser, parse_args_t& parse_a if (hasStart(noesc_str, "38;2;") || hasStart(noesc_str, "48;2;")) { const std::string& hexclr = convert_ansi_escape_rgb(noesc_str); - output += fmt::format("", hasStart(noesc_str, "38") ? 'f' : 'b', hexclr); + output += fmt::format("{}", endspan, hasStart(noesc_str, "38") ? 'f' : 'b', hexclr); } else { @@ -459,7 +445,7 @@ std::optional parse_color_tag(Parser& parser, parse_args_t& parse_a const std::string_view color = clrs.at(0); const std::string_view weight = clrs.at(1); const std::string_view type = clrs.at(2); - output += fmt::format("", type, color, weight); + output += fmt::format("{}", endspan, type, color, weight); } } @@ -536,7 +522,7 @@ std::optional parse_color_tag(Parser& parser, parse_args_t& parse_a // you can't fmt::format(style, ""); ughh const uint32_t rgb_num = bgcolor ? style.get_background().value.rgb_color : style.get_foreground().value.rgb_color; fmt::rgb rgb(rgb_num); - fmt::detail::ansi_color_escape ansi(rgb, bgcolor ? "\x1b[48;2;" : "\x1b[38;2;"); + fmt::detail::ansi_color_escape ansi(rgb, bgcolor ? "\x1B[48;2;" : "\x1B[38;2;"); fmt::detail::ansi_color_escape emph(style.get_emphasis()); output += emph.begin(); output += ansi.begin(); @@ -564,9 +550,8 @@ std::optional parse_color_tag(Parser& parser, parse_args_t& parse_a if (!parse_args.parsingLayout && tagpos != std::string::npos) parse_args.pureOutput.erase(tagpos, taglen); - parse_args.firstrun_noclr = false; + parse_args.firstrun_clr = false; - ++endspan_count; return output; } @@ -668,11 +653,8 @@ std::string parse(const std::string_view input, systemInfo_t& systemInfo, std::s std::string output{ input.data() }; pureOutput = output; - // we only use it in GUI mode, - // prevent issue where in the ascii art, - // theres at first either ${1} or ${0} - // and that's a problem with pango markup - bool firstrun_noclr = true; + // we only use it in GUI mode + bool firstrun_clr = true; if (!config.sep_reset.empty() && parsingLayout) { @@ -705,16 +687,10 @@ std::string parse(const std::string_view input, systemInfo_t& systemInfo, std::s replace_str(pureOutput, "\\<", "<"); replace_str(pureOutput, "\\&", "&"); - parse_args_t parse_args{ systemInfo, pureOutput, config, colors, parsingLayout, firstrun_noclr }; + parse_args_t parse_args{ systemInfo, pureOutput, config, colors, parsingLayout, firstrun_clr }; Parser parser{ output }; std::string ret{ parse(parser, parse_args) }; - if (parse_args.config.gui) - { - for (std::uint16_t i = 0; i < endspan_count; ++i) - ret += ""; - } - endspan_count = 0; size_t pos = 0; while ((pos = pureOutput.find('\\', pos)) != pureOutput.npos) @@ -723,6 +699,9 @@ std::string parse(const std::string_view input, systemInfo_t& systemInfo, std::s ++pos; } + if (!parse_args.firstrun_clr) + ret += ""; + return ret; }