Skip to content

Commit

Permalink
Disable printing the reset escape code when no style modifiers where …
Browse files Browse the repository at this point in the history
…applied. (#973)
  • Loading branch information
Rakete1111 authored and vitaut committed Dec 9, 2018
1 parent b0f2224 commit 24594c7
Show file tree
Hide file tree
Showing 2 changed files with 8 additions and 1 deletion.
8 changes: 7 additions & 1 deletion include/fmt/color.h
Original file line number Diff line number Diff line change
Expand Up @@ -438,20 +438,26 @@ template <
typename S, typename Char = typename internal::char_t<S>::type>
void vprint(std::FILE *f, const text_style &ts, const S &format,
basic_format_args<typename buffer_context<Char>::type> args) {
bool has_style = false;
if (ts.has_emphasis()) {
has_style = true;
internal::fputs<Char>(
internal::make_emphasis<Char>(ts.get_emphasis()), f);
}
if (ts.has_foreground()) {
has_style = true;
internal::fputs<Char>(
internal::make_foreground_color<Char>(ts.get_foreground()), f);
}
if (ts.has_background()) {
has_style = true;
internal::fputs<Char>(
internal::make_background_color<Char>(ts.get_background()), f);
}
vprint(f, format, args);
internal::reset_color<Char>(f);
if (has_style) {
internal::reset_color<Char>(f);
}
}

/**
Expand Down
1 change: 1 addition & 0 deletions test/format-impl-test.cc
Original file line number Diff line number Diff line change
Expand Up @@ -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");
}

0 comments on commit 24594c7

Please sign in to comment.