From 24594c747e7d516baa6dac0113e81f3bfd1970b6 Mon Sep 17 00:00:00 2001 From: Nicolas Date: Mon, 10 Dec 2018 00:57:20 +0100 Subject: [PATCH] Disable printing the reset escape code when no style modifiers where applied. (#973) --- include/fmt/color.h | 8 +++++++- test/format-impl-test.cc | 1 + 2 files changed, 8 insertions(+), 1 deletion(-) diff --git a/include/fmt/color.h b/include/fmt/color.h index c5e30168..78b78956 100644 --- a/include/fmt/color.h +++ b/include/fmt/color.h @@ -438,20 +438,26 @@ template < typename S, typename Char = typename internal::char_t::type> void vprint(std::FILE *f, const text_style &ts, const S &format, basic_format_args::type> args) { + bool has_style = false; if (ts.has_emphasis()) { + has_style = true; internal::fputs( internal::make_emphasis(ts.get_emphasis()), f); } if (ts.has_foreground()) { + has_style = true; internal::fputs( internal::make_foreground_color(ts.get_foreground()), f); } if (ts.has_background()) { + has_style = true; internal::fputs( internal::make_background_color(ts.get_background()), f); } vprint(f, format, args); - internal::reset_color(f); + if (has_style) { + internal::reset_color(f); + } } /** diff --git a/test/format-impl-test.cc b/test/format-impl-test.cc index 9774c33a..f4d75aae 100644 --- a/test/format-impl-test.cc +++ b/test/format-impl-test.cc @@ -232,4 +232,5 @@ TEST(ColorsTest, Colors) { "\x1b[1mbold error\x1b[0m"); EXPECT_WRITE(stderr, fmt::print(stderr, fg(fmt::color::blue), "blue log"), "\x1b[38;2;000;000;255mblue log\x1b[0m"); + EXPECT_WRITE(stdout, fmt::print(fmt::text_style(), "hi"), "hi"); }