Skip to content

Commit

Permalink
Provide more overloads for the wide string flavour
Browse files Browse the repository at this point in the history
Signed-off-by: Daniela Engert <dani@ngrt.de>
  • Loading branch information
DanielaE committed Sep 17, 2018
1 parent 894b6fa commit 0575cac
Showing 1 changed file with 47 additions and 4 deletions.
51 changes: 47 additions & 4 deletions include/fmt/color.h
Original file line number Diff line number Diff line change
Expand Up @@ -211,6 +211,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
Expand All @@ -223,6 +225,11 @@ inline void print(rgb fd, string_view format_str, const Args & ... args) {
vprint_rgb(fd, format_str, make_format_args(args...));
}

template <typename... Args>
inline void print(rgb fd, wstring_view format_str, const Args & ... args) {
vprint_rgb(fd, format_str, make_format_args<wformat_context>(args...));
}

/**
Formats a string and prints it to stdout using ANSI escape sequences to
specify foreground color 'fd' and background color 'bg'.
Expand All @@ -235,11 +242,19 @@ inline void print(rgb fd, rgb bg, string_view format_str,
const Args & ... args) {
vprint_rgb(fd, bg, format_str, make_format_args(args...));
}

template <typename... Args>
inline void print(rgb fd, rgb bg, wstring_view format_str,
const Args & ... args) {
vprint_rgb(fd, bg, format_str, make_format_args<wformat_context>(args...));
}

namespace internal {
FMT_CONSTEXPR void to_esc(uint8_t c, char out[], int offset) {
out[offset + 0] = static_cast<char>('0' + c / 100);
out[offset + 1] = static_cast<char>('0' + c / 10 % 10);
out[offset + 2] = static_cast<char>('0' + c % 10);
template <typename Char>
FMT_CONSTEXPR void to_esc(uint8_t c, Char out[], int offset) {
out[offset + 0] = static_cast<Char>('0' + c / 100);
out[offset + 1] = static_cast<Char>('0' + c / 10 % 10);
out[offset + 2] = static_cast<Char>('0' + c % 10);
}
} // namespace internal

Expand All @@ -254,6 +269,17 @@ inline void vprint_rgb(rgb fd, string_view format, format_args args) {
std::fputs(internal::data::RESET_COLOR, stdout);
}

inline 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);
}

inline 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
Expand All @@ -271,6 +297,23 @@ inline void vprint_rgb(rgb fd, rgb bg, string_view format, format_args args) {
std::fputs(internal::data::RESET_COLOR, stdout);
}

inline 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

FMT_END_NAMESPACE
Expand Down

0 comments on commit 0575cac

Please sign in to comment.