Skip to content

Commit

Permalink
parse: fix segfault when hex colors
Browse files Browse the repository at this point in the history
  • Loading branch information
Toni500github committed Oct 23, 2024
1 parent f4f80d0 commit 0efed43
Show file tree
Hide file tree
Showing 2 changed files with 19 additions and 13 deletions.
2 changes: 1 addition & 1 deletion .github/workflows/makefile.yml
Original file line number Diff line number Diff line change
Expand Up @@ -167,4 +167,4 @@ jobs:
# yes, i know too long
# the --color arguments are just for adding a missing color to the non done ascii
- name: Test all the ascii art logos
run: for f in assets/ascii/*; do printf "\e[31m%s\e[0m\n" $f && cufetch --wrap-lines=0 -s $f -D assets --color "c1=#fff111" --color "c2=#00ff1a" --color "c3=#faa311" --color "c4=#343412" --color "c5=#fff311" --color "c6=#faa3aa" && sleep 1; done
run: for f in assets/ascii/*; do printf "\e[31m%s\e[0m\n" "$f" && cufetch --wrap-lines=0 -s "$f" -D assets --color "c1=!#fff111" --color "c2=!#00ff1a" --color "c3=!#faa311" --color "c4=!#343412" --color "c5=!#fff311" --color "c6=!#faa3aa" && sleep 1; done
30 changes: 18 additions & 12 deletions src/parse.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -263,12 +263,12 @@ std::optional<std::string> parse_color_tag(Parser& parser, parse_args_t& parse_a

static std::vector<std::string> auto_colors;

std::string output;
const Config& config = parse_args.config;
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 ? "</span>" : "");
std::string output;
const Config& config = parse_args.config;
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 ? "</span>" : "");

if (config.m_disable_colors)
{
Expand Down Expand Up @@ -520,12 +520,18 @@ std::optional<std::string> parse_color_tag(Parser& parser, parse_args_t& parse_a
append_styles(style, fmt::fg(hexStringToColor(str_clr.substr(pos))));

// 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<char> ansi(rgb, bgcolor ? "\x1B[48;2;" : "\x1B[38;2;");
fmt::detail::ansi_color_escape<char> emph(style.get_emphasis());
output += emph.begin();
output += ansi.begin();
if (style.has_background() || style.has_foreground())
{
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<char> ansi(rgb, bgcolor ? "\x1B[48;2;" : "\x1B[38;2;");
output += ansi.begin();
}
if (style.has_emphasis())
{
fmt::detail::ansi_color_escape<char> emph(style.get_emphasis());
output += emph.begin();
}
}

// "\\e" is for checking in the ascii_art, \033 in the config
Expand Down

0 comments on commit 0efed43

Please sign in to comment.