From 15f363ab3a044a7259433b7fe4ce666210275628 Mon Sep 17 00:00:00 2001 From: Phoenix McAllister <33542651+phoenixmcallister@users.noreply.github.com> Date: Tue, 17 Jul 2018 14:49:40 +1000 Subject: [PATCH 1/3] Update format.h --- include/fmt/format.h | 24 ++++++++++++++++++++++++ 1 file changed, 24 insertions(+) diff --git a/include/fmt/format.h b/include/fmt/format.h index eb0db49c..5e075b4e 100644 --- a/include/fmt/format.h +++ b/include/fmt/format.h @@ -3830,6 +3830,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 @@ -3842,6 +3844,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'. @@ -3852,6 +3865,17 @@ template 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 From 948cdb42423108a5dbb74c5c88a84f58d2e57c46 Mon Sep 17 00:00:00 2001 From: Phoenix McAllister <33542651+phoenixmcallister@users.noreply.github.com> Date: Tue, 17 Jul 2018 14:50:36 +1000 Subject: [PATCH 2/3] Update format-inl.h --- include/fmt/format-inl.h | 28 ++++++++++++++++++++++++++++ 1 file changed, 28 insertions(+) diff --git a/include/fmt/format-inl.h b/include/fmt/format-inl.h index e66bbb7a..dbbf3507 100644 --- a/include/fmt/format-inl.h +++ b/include/fmt/format-inl.h @@ -535,6 +535,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) { + char escape_fd[] = "\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::fputs(escape_fd, stdout); + vprint(format, args); + std::fputs(internal::data::RESET_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 @@ -551,6 +562,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) { + char escape_fd[] = "\x1b[38;2;000;000;000m"; // foreground color + char escape_bg[] = "\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::fputs(escape_fd, stdout); + std::fputs(escape_bg, stdout); + vprint(format, args); + std::fputs(internal::data::RESET_COLOR, stdout); +} #endif FMT_FUNC locale locale_provider::locale() { return fmt::locale(); } From a806d877f8dc779837e4691086ddf51d73bae813 Mon Sep 17 00:00:00 2001 From: Phoenix McAllister <33542651+phoenixmcallister@users.noreply.github.com> Date: Tue, 17 Jul 2018 15:08:58 +1000 Subject: [PATCH 3/3] Update format-inl.h Changed std::fputs to std::fputws and changed char to wchar_t, since i forgot to change it. --- include/fmt/format-inl.h | 22 ++++++++++++++-------- 1 file changed, 14 insertions(+), 8 deletions(-) diff --git a/include/fmt/format-inl.h b/include/fmt/format-inl.h index dbbf3507..56be0896 100644 --- a/include/fmt/format-inl.h +++ b/include/fmt/format-inl.h @@ -522,6 +522,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) { @@ -536,14 +542,14 @@ FMT_FUNC void vprint_rgb(rgb fd, string_view format, format_args args) { } FMT_FUNC void vprint_rgb(rgb fd, wstring_view format, wformat_args args) { - char escape_fd[] = "\x1b[38;2;000;000;000m"; + 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::fputs(escape_fd, stdout); + std::fputws(escape_fd, stdout); vprint(format, args); - std::fputs(internal::data::RESET_COLOR, stdout); + std::fputws(internal::data::WRESET_COLOR, stdout); } FMT_FUNC void vprint_rgb(rgb fd, rgb bg, string_view format, format_args args) { @@ -564,8 +570,8 @@ FMT_FUNC void vprint_rgb(rgb fd, rgb bg, string_view format, format_args args) { } FMT_FUNC void vprint_rgb(rgb fd, rgb bg, wstring_view format, wformat_args args) { - char escape_fd[] = "\x1b[38;2;000;000;000m"; // foreground color - char escape_bg[] = "\x1b[48;2;000;000;000m"; // background color + 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); @@ -574,10 +580,10 @@ FMT_FUNC void vprint_rgb(rgb fd, rgb bg, wstring_view format, wformat_args args) internal::to_esc(bg.g, escape_bg, 11); internal::to_esc(bg.b, escape_bg, 15); - std::fputs(escape_fd, stdout); - std::fputs(escape_bg, stdout); + std::fputws(escape_fd, stdout); + std::fputws(escape_bg, stdout); vprint(format, args); - std::fputs(internal::data::RESET_COLOR, stdout); + std::fputws(internal::data::WRESET_COLOR, stdout); } #endif