From 5e705209d8692aeccca6cb56308b7025ca15a992 Mon Sep 17 00:00:00 2001 From: Eclipse842 Date: Fri, 18 Dec 2020 17:30:30 -0800 Subject: [PATCH] Added format_to(buffer, text_style, ...) overload --- include/fmt/color.h | 10 ++++++++++ test/color-test.cc | 9 +++++++++ 2 files changed, 19 insertions(+) diff --git a/include/fmt/color.h b/include/fmt/color.h index 42008f51..efdff0f3 100644 --- a/include/fmt/color.h +++ b/include/fmt/color.h @@ -618,6 +618,16 @@ inline auto format_to(OutputIt out, const text_style& ts, const S& format_str, fmt::make_args_checked(format_str, args...)); } +template ::value, char_t>> +inline typename buffer_context::iterator format_to( + basic_memory_buffer& buf, const text_style& ts, + const S& format_str, Args&&... args) { + const auto& vargs = fmt::make_args_checked(format_str, args...); + detail::vformat_to(buf, ts, to_string_view(format_str), vargs); + return detail::buffer_appender(buf); +} + FMT_END_NAMESPACE #endif // FMT_COLOR_H_ diff --git a/test/color-test.cc b/test/color-test.cc index 30738085..bf03c8ab 100644 --- a/test/color-test.cc +++ b/test/color-test.cc @@ -97,3 +97,12 @@ TEST(ColorsTest, FormatToOutAcceptsTextStyle) { EXPECT_EQ(fmt::to_string(out), "\x1b[38;2;255;020;030mrgb(255,20,30)123\x1b[0m"); } + +TEST(ColorsTest, FormatToBufAcceptsTextStyle) { + fmt::text_style ts = fg(fmt::rgb(255, 20, 30)); + fmt::memory_buffer buf; + fmt::format_to(buf, ts, "rgb(255,20,30){}{}{}", 1, 2, 3); + + EXPECT_EQ(std::string(buf.data(), buf.size()), + "\x1b[38;2;255;020;030mrgb(255,20,30)123\x1b[0m"); +}