From b07b860bb9b2771485c565123ab6507305ed0e47 Mon Sep 17 00:00:00 2001 From: Nicolas Lesser Date: Tue, 4 Dec 2018 11:44:28 +0100 Subject: [PATCH] Add experimental emphasis support. --- include/fmt/color.h | 97 +++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 97 insertions(+) diff --git a/include/fmt/color.h b/include/fmt/color.h index 4d7193ce..a5add5eb 100644 --- a/include/fmt/color.h +++ b/include/fmt/color.h @@ -192,6 +192,11 @@ enum class color : uint32_t { yellow_green = 0x9ACD32 // rgb(154,205,50) }; // enum class color +enum class emphasis : uint8_t { + bold = 1, + underline = 4, +}; // enum class emphasis + // rgb is a struct for red, green and blue colors. // We use rgb as name because some editors will show it as color direct in the // editor. @@ -222,6 +227,13 @@ struct ansi_color_escape { to_esc(color.b, buffer + 15, 'm'); buffer[19] = static_cast(0); } + FMT_CONSTEXPR ansi_color_escape(emphasis em) FMT_NOEXCEPT { + buffer[0] = '\x1b'; + buffer[1] = '['; + buffer[2] = '0' + (uint8_t)em; + buffer[3] = 'm'; + buffer[4] = '\0'; + } FMT_CONSTEXPR operator const Char *() const FMT_NOEXCEPT { return buffer; } private: @@ -247,6 +259,11 @@ make_background_color(rgb color) FMT_NOEXCEPT { return ansi_color_escape(color, internal::data::BACKGROUND_COLOR); } +template +FMT_CONSTEXPR ansi_color_escape make_emphasis(emphasis em) FMT_NOEXCEPT { + return ansi_color_escape(em); +} + template inline void fputs(const Char *chars, FILE *stream) FMT_NOEXCEPT { std::fputs(chars, stream); @@ -287,6 +304,36 @@ void vprint_rgb(rgb fd, rgb bg, const S &format, internal::reset_color(stdout); } +template ::type> +void vprint_emphasis( + emphasis em, const S &format, + basic_format_args::type> args) { + internal::fputs(internal::make_emphasis(em), stdout); + vprint(format, args); + internal::reset_color(stdout); +} + +template ::type> +void vprint_emphasis( + emphasis em, rgb fd, const S &format, + basic_format_args::type> args) { + internal::fputs(internal::make_emphasis(em), stdout); + internal::fputs(internal::make_foreground_color(fd), stdout); + vprint(format, args); + internal::reset_color(stdout); +} + +template ::type> +void vprint_emphasis( + emphasis em, rgb fd, rgb bg, const S &format, + basic_format_args::type> args) { + internal::fputs(internal::make_emphasis(em), stdout); + internal::fputs(internal::make_foreground_color(fd), stdout); + internal::fputs(internal::make_background_color(bg), stdout); + vprint(format, args); + internal::reset_color(stdout); +} + /** Formats a string and prints it to stdout using ANSI escape sequences to specify foreground color 'fd'. @@ -320,6 +367,56 @@ print(rgb fd, rgb bg, const String &format_str, const Args & ... args) { vprint_rgb(fd, bg, format_str, basic_format_args(as)); } +/** + Formats a string and prints it to stdout using ANSI escape sequences to + specify the text emphasis 'em'. + Example: + fmt::print(fmt::emphasis::bold, "Be careful!"); + */ +template +typename std::enable_if::value>::type print( + emphasis em, const String &format_str, const Args &... args) { + internal::check_format_string(format_str); + typedef typename internal::char_t::type char_t; + typedef typename buffer_context::type context_t; + format_arg_store as{args...}; + vprint_emphasis(em, format_str, basic_format_args(as)); +} + +/** + Formats a string and prints it to stdout using ANSI escape sequences to + specify the text emphasis 'em' and the foreground color 'fd'. + Example: + fmt::print(fmt::emphasis::bold, fmt::color::red, "Be careful!"); + */ +template +typename std::enable_if::value>::type print( + emphasis em, rgb fd, const String &format_str, const Args &... args) { + internal::check_format_string(format_str); + typedef typename internal::char_t::type char_t; + typedef typename buffer_context::type context_t; + format_arg_store as{args...}; + vprint_emphasis(em, fd, format_str, basic_format_args(as)); +} + +/** + Formats a string and prints it to stdout using ANSI escape sequences to + specify the text emphasis 'em', the foreground color 'fg' and the background + color 'bg'. + Example: + fmt::print(fmt::emphasis::bold, "Be careful!"); + */ +template +typename std::enable_if::value>::type print( + emphasis em, rgb fg, rgb bg, const String &format_str, + const Args &... args) { + internal::check_format_string(format_str); + typedef typename internal::char_t::type char_t; + typedef typename buffer_context::type context_t; + format_arg_store as{args...}; + vprint_emphasis(em, fg, bg, format_str, basic_format_args(as)); +} + #endif FMT_END_NAMESPACE