From 3ce69f4ca5c3be9693d8ad8e3aeaed34adf77357 Mon Sep 17 00:00:00 2001 From: Tanki Zhang Date: Tue, 8 Oct 2019 23:46:20 -0400 Subject: [PATCH] use memory_buffer to make color print behave atomic #1348 --- include/fmt/color.h | 22 +++++++++++++++------- 1 file changed, 15 insertions(+), 7 deletions(-) diff --git a/include/fmt/color.h b/include/fmt/color.h index f2dd24f2..3d0668e6 100644 --- a/include/fmt/color.h +++ b/include/fmt/color.h @@ -504,24 +504,32 @@ 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); + + format_to( buf, "{}", format ); + if (has_style) { - internal::reset_color(f); + size_t retset_color_char_count = sizeof( internal::data::reset_color ) / (*internal::data::reset_color); + buf.append(internal::data::reset_color, internal::data::reset_color + retset_color_char_count); } + + + vprint(f, buf.data(), args); } /**