diff --git a/include/fmt/color.h b/include/fmt/color.h index 7647d516..c933849d 100644 --- a/include/fmt/color.h +++ b/include/fmt/color.h @@ -460,10 +460,11 @@ void vformat_to(buffer& buf, const text_style& ts, FMT_END_DETAIL_NAMESPACE inline void vprint(std::FILE* f, const text_style& ts, string_view fmt, - format_args args) { + bool new_line, format_args args) { // Legacy wide streams are not supported. auto buf = memory_buffer(); detail::vformat_to(buf, ts, fmt, args); + if (new_line) buf.push_back('\n'); if (detail::is_utf8()) { detail::print(f, string_view(buf.begin(), buf.size())); return; @@ -489,7 +490,26 @@ template ::value)> void print(std::FILE* f, const text_style& ts, const S& format_str, const Args&... args) { - vprint(f, ts, format_str, + vprint(f, ts, format_str, false, + fmt::make_format_args>>(args...)); +} + +/** + \rst + Formats a string and prints it to the specified file stream using ANSI + escape sequences to specify text formatting followed by a newline. + + **Example**:: + + fmt::println(fmt::emphasis::bold | fg(fmt::color::red), + "Elapsed time: {0:.2f} seconds", 1.23); + \endrst + */ +template ::value)> +void println(std::FILE* f, const text_style& ts, const S& format_str, + const Args&... args) { + vprint(f, ts, format_str, true, fmt::make_format_args>>(args...)); } @@ -510,6 +530,23 @@ void print(const text_style& ts, const S& format_str, const Args&... args) { return print(stdout, ts, format_str, args...); } +/** + \rst + Formats a string and prints it to stdout using ANSI escape sequences to + specify text formatting followed by a newline. + + **Example**:: + + fmt::println(fmt::emphasis::bold | fg(fmt::color::red), + "Elapsed time: {0:.2f} seconds", 1.23); + \endrst + */ +template ::value)> +void println(const text_style& ts, const S& format_str, const Args&... args) { + return println(stdout, ts, format_str, args...); +} + template > inline std::basic_string vformat( const text_style& ts, const S& format_str, diff --git a/test/color-test.cc b/test/color-test.cc index c2ba13a9..38add10e 100644 --- a/test/color-test.cc +++ b/test/color-test.cc @@ -69,4 +69,6 @@ TEST(color_test, format_to) { TEST(color_test, print) { EXPECT_WRITE(stdout, fmt::print(fg(fmt::rgb(255, 20, 30)), "rgb(255,20,30)"), "\x1b[38;2;255;020;030mrgb(255,20,30)\x1b[0m"); + EXPECT_WRITE(stdout, fmt::println(fg(fmt::rgb(255, 20, 30)), "rgb(255,20,30)"), + "\x1b[38;2;255;020;030mrgb(255,20,30)\x1b[0m\n"); }