diff --git a/include/fmt/format-inl.h b/include/fmt/format-inl.h index 019282fb..3c6b45d7 100644 --- a/include/fmt/format-inl.h +++ b/include/fmt/format-inl.h @@ -876,6 +876,12 @@ FMT_CONSTEXPR void to_esc(uint8_t c, char out[], int offset) { out[offset + 1] = static_cast('0' + c / 10 % 10); out[offset + 2] = static_cast('0' + c % 10); } + +FMT_CONSTEXPR void to_esc(uint8_t c, wchar_t out[], int offset) { + out[offset + 0] = static_cast('0' + c / 100); + out[offset + 1] = static_cast('0' + c / 10 % 10); + out[offset + 2] = static_cast('0' + c % 10); +} } // namespace internal FMT_FUNC void vprint_rgb(rgb fd, string_view format, format_args args) { @@ -889,6 +895,17 @@ FMT_FUNC void vprint_rgb(rgb fd, string_view format, format_args args) { std::fputs(internal::data::RESET_COLOR, stdout); } +FMT_FUNC void vprint_rgb(rgb fd, wstring_view format, wformat_args args) { + wchar_t escape_fd[] = L"\x1b[38;2;000;000;000m"; + internal::to_esc(fd.r, escape_fd, 7); + internal::to_esc(fd.g, escape_fd, 11); + internal::to_esc(fd.b, escape_fd, 15); + + std::fputws(escape_fd, stdout); + vprint(format, args); + std::fputws(internal::data::WRESET_COLOR, stdout); +} + FMT_FUNC void vprint_rgb(rgb fd, rgb bg, string_view format, format_args args) { char escape_fd[] = "\x1b[38;2;000;000;000m"; // foreground color char escape_bg[] = "\x1b[48;2;000;000;000m"; // background color @@ -905,6 +922,23 @@ FMT_FUNC void vprint_rgb(rgb fd, rgb bg, string_view format, format_args args) { vprint(format, args); std::fputs(internal::data::RESET_COLOR, stdout); } + +FMT_FUNC void vprint_rgb(rgb fd, rgb bg, wstring_view format, wformat_args args) { + wchar_t escape_fd[] = L"\x1b[38;2;000;000;000m"; // foreground color + wchar_t escape_bg[] = L"\x1b[48;2;000;000;000m"; // background color + internal::to_esc(fd.r, escape_fd, 7); + internal::to_esc(fd.g, escape_fd, 11); + internal::to_esc(fd.b, escape_fd, 15); + + internal::to_esc(bg.r, escape_bg, 7); + internal::to_esc(bg.g, escape_bg, 11); + internal::to_esc(bg.b, escape_bg, 15); + + std::fputws(escape_fd, stdout); + std::fputws(escape_bg, stdout); + vprint(format, args); + std::fputws(internal::data::WRESET_COLOR, stdout); +} #endif #if !defined(FMT_STATIC_THOUSANDS_SEPARATOR) FMT_FUNC locale locale_provider::locale() { return fmt::locale(); } diff --git a/include/fmt/format.h b/include/fmt/format.h index 657924c1..436ab929 100644 --- a/include/fmt/format.h +++ b/include/fmt/format.h @@ -3775,6 +3775,8 @@ struct rgb { void vprint_rgb(rgb fd, string_view format, format_args args); void vprint_rgb(rgb fd, rgb bg, string_view format, format_args args); +void vprint_rgb(rgb fd, wstring_view format, wformat_args args); +void vprint_rgb(rgb fd, rgb bg, wstring_view format, wformat_args args); /** Formats a string and prints it to stdout using ANSI escape sequences to @@ -3787,6 +3789,17 @@ inline void print(rgb fd, string_view format_str, const Args & ... args) { vprint_rgb(fd, format_str, make_format_args(args...)); } +/** + Formats a string and prints it to stdout using ANSI escape sequences to + specify foreground color 'fd'. + Example: + fmt::print(fmt::color::red, L"Elapsed time: {0:.2f} seconds", 1.23); + */ +template +inline void print(rgb fd, wstring_view format_str, const Args & ... args) { + vprint_rgb(fd, format_str, make_format_args(args...)); +} + /** Formats a string and prints it to stdout using ANSI escape sequences to specify foreground color 'fd' and background color 'bg'. @@ -3799,6 +3812,17 @@ inline void print(rgb fd, rgb bg, string_view format_str, const Args & ... args) { vprint_rgb(fd, bg, format_str, make_format_args(args...)); } + +/** + Formats a string and prints it to stdout using ANSI escape sequences to + specify foreground color 'fd' and background color 'bg'. + Example: + fmt::print(fmt::color::red, fmt::color::black, L"Elapsed time: {0:.2f} seconds", 1.23); + */ +template +inline void print(rgb fd, rgb bg, wstring_view format_str, const Args & ... args) { + vprint_rgb(fd, bg, format_str, make_format_args(args...)); +} #endif // FMT_EXTENDED_COLORS #if FMT_USE_USER_DEFINED_LITERALS