From a82c1dc6d94464af5b8551fdff2beb61b7166418 Mon Sep 17 00:00:00 2001 From: Tanki Zhang Date: Thu, 10 Oct 2019 11:28:56 -0400 Subject: [PATCH] use memory_buffer to make color print behave atomic #1348 (#1351) --- include/fmt/color.h | 20 +++++++++++++------- 1 file changed, 13 insertions(+), 7 deletions(-) diff --git a/include/fmt/color.h b/include/fmt/color.h index f2dd24f2..6c64b43b 100644 --- a/include/fmt/color.h +++ b/include/fmt/color.h @@ -504,24 +504,30 @@ template > void vprint(std::FILE* f, const text_style& ts, const S& format, basic_format_args > args) { bool has_style = false; + basic_memory_buffer buf; if (ts.has_emphasis()) { has_style = true; - internal::fputs(internal::make_emphasis(ts.get_emphasis()), f); + auto emphasis = internal::make_emphasis(ts.get_emphasis()); + buf.append(emphasis.begin(), emphasis.end()); } if (ts.has_foreground()) { has_style = true; - internal::fputs( - internal::make_foreground_color(ts.get_foreground()), f); + auto foreground = + internal::make_foreground_color(ts.get_foreground()); + buf.append(foreground.begin(), foreground.end()); } if (ts.has_background()) { has_style = true; - internal::fputs( - internal::make_background_color(ts.get_background()), f); + auto background = + internal::make_background_color(ts.get_background()); + buf.append(background.begin(), background.end()); } - vprint(f, format, args); + vformat_to(buf, format, args); if (has_style) { - internal::reset_color(f); + internal::reset_color(buf); } + buf.push_back(Char(0)); + internal::fputs(buf.data(), f); } /**