diff --git a/include/fmt/color.h b/include/fmt/color.h index c5e3016877ed..78b7895640fb 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 9774c33a274c..f4d75aaef287 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"); }