Back to home page

EIC code displayed by LXR

 
 

    


File indexing completed on 2025-01-18 09:57:20

0001 // Formatting library for C++ - color support
0002 //
0003 // Copyright (c) 2018 - present, Victor Zverovich and fmt contributors
0004 // All rights reserved.
0005 //
0006 // For the license information refer to format.h.
0007 
0008 #ifndef FMT_COLOR_H_
0009 #define FMT_COLOR_H_
0010 
0011 #include "format.h"
0012 
0013 FMT_BEGIN_NAMESPACE
0014 FMT_MODULE_EXPORT_BEGIN
0015 
0016 enum class color : uint32_t {
0017   alice_blue = 0xF0F8FF,               // rgb(240,248,255)
0018   antique_white = 0xFAEBD7,            // rgb(250,235,215)
0019   aqua = 0x00FFFF,                     // rgb(0,255,255)
0020   aquamarine = 0x7FFFD4,               // rgb(127,255,212)
0021   azure = 0xF0FFFF,                    // rgb(240,255,255)
0022   beige = 0xF5F5DC,                    // rgb(245,245,220)
0023   bisque = 0xFFE4C4,                   // rgb(255,228,196)
0024   black = 0x000000,                    // rgb(0,0,0)
0025   blanched_almond = 0xFFEBCD,          // rgb(255,235,205)
0026   blue = 0x0000FF,                     // rgb(0,0,255)
0027   blue_violet = 0x8A2BE2,              // rgb(138,43,226)
0028   brown = 0xA52A2A,                    // rgb(165,42,42)
0029   burly_wood = 0xDEB887,               // rgb(222,184,135)
0030   cadet_blue = 0x5F9EA0,               // rgb(95,158,160)
0031   chartreuse = 0x7FFF00,               // rgb(127,255,0)
0032   chocolate = 0xD2691E,                // rgb(210,105,30)
0033   coral = 0xFF7F50,                    // rgb(255,127,80)
0034   cornflower_blue = 0x6495ED,          // rgb(100,149,237)
0035   cornsilk = 0xFFF8DC,                 // rgb(255,248,220)
0036   crimson = 0xDC143C,                  // rgb(220,20,60)
0037   cyan = 0x00FFFF,                     // rgb(0,255,255)
0038   dark_blue = 0x00008B,                // rgb(0,0,139)
0039   dark_cyan = 0x008B8B,                // rgb(0,139,139)
0040   dark_golden_rod = 0xB8860B,          // rgb(184,134,11)
0041   dark_gray = 0xA9A9A9,                // rgb(169,169,169)
0042   dark_green = 0x006400,               // rgb(0,100,0)
0043   dark_khaki = 0xBDB76B,               // rgb(189,183,107)
0044   dark_magenta = 0x8B008B,             // rgb(139,0,139)
0045   dark_olive_green = 0x556B2F,         // rgb(85,107,47)
0046   dark_orange = 0xFF8C00,              // rgb(255,140,0)
0047   dark_orchid = 0x9932CC,              // rgb(153,50,204)
0048   dark_red = 0x8B0000,                 // rgb(139,0,0)
0049   dark_salmon = 0xE9967A,              // rgb(233,150,122)
0050   dark_sea_green = 0x8FBC8F,           // rgb(143,188,143)
0051   dark_slate_blue = 0x483D8B,          // rgb(72,61,139)
0052   dark_slate_gray = 0x2F4F4F,          // rgb(47,79,79)
0053   dark_turquoise = 0x00CED1,           // rgb(0,206,209)
0054   dark_violet = 0x9400D3,              // rgb(148,0,211)
0055   deep_pink = 0xFF1493,                // rgb(255,20,147)
0056   deep_sky_blue = 0x00BFFF,            // rgb(0,191,255)
0057   dim_gray = 0x696969,                 // rgb(105,105,105)
0058   dodger_blue = 0x1E90FF,              // rgb(30,144,255)
0059   fire_brick = 0xB22222,               // rgb(178,34,34)
0060   floral_white = 0xFFFAF0,             // rgb(255,250,240)
0061   forest_green = 0x228B22,             // rgb(34,139,34)
0062   fuchsia = 0xFF00FF,                  // rgb(255,0,255)
0063   gainsboro = 0xDCDCDC,                // rgb(220,220,220)
0064   ghost_white = 0xF8F8FF,              // rgb(248,248,255)
0065   gold = 0xFFD700,                     // rgb(255,215,0)
0066   golden_rod = 0xDAA520,               // rgb(218,165,32)
0067   gray = 0x808080,                     // rgb(128,128,128)
0068   green = 0x008000,                    // rgb(0,128,0)
0069   green_yellow = 0xADFF2F,             // rgb(173,255,47)
0070   honey_dew = 0xF0FFF0,                // rgb(240,255,240)
0071   hot_pink = 0xFF69B4,                 // rgb(255,105,180)
0072   indian_red = 0xCD5C5C,               // rgb(205,92,92)
0073   indigo = 0x4B0082,                   // rgb(75,0,130)
0074   ivory = 0xFFFFF0,                    // rgb(255,255,240)
0075   khaki = 0xF0E68C,                    // rgb(240,230,140)
0076   lavender = 0xE6E6FA,                 // rgb(230,230,250)
0077   lavender_blush = 0xFFF0F5,           // rgb(255,240,245)
0078   lawn_green = 0x7CFC00,               // rgb(124,252,0)
0079   lemon_chiffon = 0xFFFACD,            // rgb(255,250,205)
0080   light_blue = 0xADD8E6,               // rgb(173,216,230)
0081   light_coral = 0xF08080,              // rgb(240,128,128)
0082   light_cyan = 0xE0FFFF,               // rgb(224,255,255)
0083   light_golden_rod_yellow = 0xFAFAD2,  // rgb(250,250,210)
0084   light_gray = 0xD3D3D3,               // rgb(211,211,211)
0085   light_green = 0x90EE90,              // rgb(144,238,144)
0086   light_pink = 0xFFB6C1,               // rgb(255,182,193)
0087   light_salmon = 0xFFA07A,             // rgb(255,160,122)
0088   light_sea_green = 0x20B2AA,          // rgb(32,178,170)
0089   light_sky_blue = 0x87CEFA,           // rgb(135,206,250)
0090   light_slate_gray = 0x778899,         // rgb(119,136,153)
0091   light_steel_blue = 0xB0C4DE,         // rgb(176,196,222)
0092   light_yellow = 0xFFFFE0,             // rgb(255,255,224)
0093   lime = 0x00FF00,                     // rgb(0,255,0)
0094   lime_green = 0x32CD32,               // rgb(50,205,50)
0095   linen = 0xFAF0E6,                    // rgb(250,240,230)
0096   magenta = 0xFF00FF,                  // rgb(255,0,255)
0097   maroon = 0x800000,                   // rgb(128,0,0)
0098   medium_aquamarine = 0x66CDAA,        // rgb(102,205,170)
0099   medium_blue = 0x0000CD,              // rgb(0,0,205)
0100   medium_orchid = 0xBA55D3,            // rgb(186,85,211)
0101   medium_purple = 0x9370DB,            // rgb(147,112,219)
0102   medium_sea_green = 0x3CB371,         // rgb(60,179,113)
0103   medium_slate_blue = 0x7B68EE,        // rgb(123,104,238)
0104   medium_spring_green = 0x00FA9A,      // rgb(0,250,154)
0105   medium_turquoise = 0x48D1CC,         // rgb(72,209,204)
0106   medium_violet_red = 0xC71585,        // rgb(199,21,133)
0107   midnight_blue = 0x191970,            // rgb(25,25,112)
0108   mint_cream = 0xF5FFFA,               // rgb(245,255,250)
0109   misty_rose = 0xFFE4E1,               // rgb(255,228,225)
0110   moccasin = 0xFFE4B5,                 // rgb(255,228,181)
0111   navajo_white = 0xFFDEAD,             // rgb(255,222,173)
0112   navy = 0x000080,                     // rgb(0,0,128)
0113   old_lace = 0xFDF5E6,                 // rgb(253,245,230)
0114   olive = 0x808000,                    // rgb(128,128,0)
0115   olive_drab = 0x6B8E23,               // rgb(107,142,35)
0116   orange = 0xFFA500,                   // rgb(255,165,0)
0117   orange_red = 0xFF4500,               // rgb(255,69,0)
0118   orchid = 0xDA70D6,                   // rgb(218,112,214)
0119   pale_golden_rod = 0xEEE8AA,          // rgb(238,232,170)
0120   pale_green = 0x98FB98,               // rgb(152,251,152)
0121   pale_turquoise = 0xAFEEEE,           // rgb(175,238,238)
0122   pale_violet_red = 0xDB7093,          // rgb(219,112,147)
0123   papaya_whip = 0xFFEFD5,              // rgb(255,239,213)
0124   peach_puff = 0xFFDAB9,               // rgb(255,218,185)
0125   peru = 0xCD853F,                     // rgb(205,133,63)
0126   pink = 0xFFC0CB,                     // rgb(255,192,203)
0127   plum = 0xDDA0DD,                     // rgb(221,160,221)
0128   powder_blue = 0xB0E0E6,              // rgb(176,224,230)
0129   purple = 0x800080,                   // rgb(128,0,128)
0130   rebecca_purple = 0x663399,           // rgb(102,51,153)
0131   red = 0xFF0000,                      // rgb(255,0,0)
0132   rosy_brown = 0xBC8F8F,               // rgb(188,143,143)
0133   royal_blue = 0x4169E1,               // rgb(65,105,225)
0134   saddle_brown = 0x8B4513,             // rgb(139,69,19)
0135   salmon = 0xFA8072,                   // rgb(250,128,114)
0136   sandy_brown = 0xF4A460,              // rgb(244,164,96)
0137   sea_green = 0x2E8B57,                // rgb(46,139,87)
0138   sea_shell = 0xFFF5EE,                // rgb(255,245,238)
0139   sienna = 0xA0522D,                   // rgb(160,82,45)
0140   silver = 0xC0C0C0,                   // rgb(192,192,192)
0141   sky_blue = 0x87CEEB,                 // rgb(135,206,235)
0142   slate_blue = 0x6A5ACD,               // rgb(106,90,205)
0143   slate_gray = 0x708090,               // rgb(112,128,144)
0144   snow = 0xFFFAFA,                     // rgb(255,250,250)
0145   spring_green = 0x00FF7F,             // rgb(0,255,127)
0146   steel_blue = 0x4682B4,               // rgb(70,130,180)
0147   tan = 0xD2B48C,                      // rgb(210,180,140)
0148   teal = 0x008080,                     // rgb(0,128,128)
0149   thistle = 0xD8BFD8,                  // rgb(216,191,216)
0150   tomato = 0xFF6347,                   // rgb(255,99,71)
0151   turquoise = 0x40E0D0,                // rgb(64,224,208)
0152   violet = 0xEE82EE,                   // rgb(238,130,238)
0153   wheat = 0xF5DEB3,                    // rgb(245,222,179)
0154   white = 0xFFFFFF,                    // rgb(255,255,255)
0155   white_smoke = 0xF5F5F5,              // rgb(245,245,245)
0156   yellow = 0xFFFF00,                   // rgb(255,255,0)
0157   yellow_green = 0x9ACD32              // rgb(154,205,50)
0158 };                                     // enum class color
0159 
0160 enum class terminal_color : uint8_t {
0161   black = 30,
0162   red,
0163   green,
0164   yellow,
0165   blue,
0166   magenta,
0167   cyan,
0168   white,
0169   bright_black = 90,
0170   bright_red,
0171   bright_green,
0172   bright_yellow,
0173   bright_blue,
0174   bright_magenta,
0175   bright_cyan,
0176   bright_white
0177 };
0178 
0179 enum class emphasis : uint8_t {
0180   bold = 1,
0181   faint = 1 << 1,
0182   italic = 1 << 2,
0183   underline = 1 << 3,
0184   blink = 1 << 4,
0185   reverse = 1 << 5,
0186   conceal = 1 << 6,
0187   strikethrough = 1 << 7,
0188 };
0189 
0190 // rgb is a struct for red, green and blue colors.
0191 // Using the name "rgb" makes some editors show the color in a tooltip.
0192 struct rgb {
0193   FMT_CONSTEXPR rgb() : r(0), g(0), b(0) {}
0194   FMT_CONSTEXPR rgb(uint8_t r_, uint8_t g_, uint8_t b_) : r(r_), g(g_), b(b_) {}
0195   FMT_CONSTEXPR rgb(uint32_t hex)
0196       : r((hex >> 16) & 0xFF), g((hex >> 8) & 0xFF), b(hex & 0xFF) {}
0197   FMT_CONSTEXPR rgb(color hex)
0198       : r((uint32_t(hex) >> 16) & 0xFF),
0199         g((uint32_t(hex) >> 8) & 0xFF),
0200         b(uint32_t(hex) & 0xFF) {}
0201   uint8_t r;
0202   uint8_t g;
0203   uint8_t b;
0204 };
0205 
0206 FMT_BEGIN_DETAIL_NAMESPACE
0207 
0208 // color is a struct of either a rgb color or a terminal color.
0209 struct color_type {
0210   FMT_CONSTEXPR color_type() noexcept : is_rgb(), value{} {}
0211   FMT_CONSTEXPR color_type(color rgb_color) noexcept : is_rgb(true), value{} {
0212     value.rgb_color = static_cast<uint32_t>(rgb_color);
0213   }
0214   FMT_CONSTEXPR color_type(rgb rgb_color) noexcept : is_rgb(true), value{} {
0215     value.rgb_color = (static_cast<uint32_t>(rgb_color.r) << 16) |
0216                       (static_cast<uint32_t>(rgb_color.g) << 8) | rgb_color.b;
0217   }
0218   FMT_CONSTEXPR color_type(terminal_color term_color) noexcept
0219       : is_rgb(), value{} {
0220     value.term_color = static_cast<uint8_t>(term_color);
0221   }
0222   bool is_rgb;
0223   union color_union {
0224     uint8_t term_color;
0225     uint32_t rgb_color;
0226   } value;
0227 };
0228 
0229 FMT_END_DETAIL_NAMESPACE
0230 
0231 /** A text style consisting of foreground and background colors and emphasis. */
0232 class text_style {
0233  public:
0234   FMT_CONSTEXPR text_style(emphasis em = emphasis()) noexcept
0235       : set_foreground_color(), set_background_color(), ems(em) {}
0236 
0237   FMT_CONSTEXPR text_style& operator|=(const text_style& rhs) {
0238     if (!set_foreground_color) {
0239       set_foreground_color = rhs.set_foreground_color;
0240       foreground_color = rhs.foreground_color;
0241     } else if (rhs.set_foreground_color) {
0242       if (!foreground_color.is_rgb || !rhs.foreground_color.is_rgb)
0243         FMT_THROW(format_error("can't OR a terminal color"));
0244       foreground_color.value.rgb_color |= rhs.foreground_color.value.rgb_color;
0245     }
0246 
0247     if (!set_background_color) {
0248       set_background_color = rhs.set_background_color;
0249       background_color = rhs.background_color;
0250     } else if (rhs.set_background_color) {
0251       if (!background_color.is_rgb || !rhs.background_color.is_rgb)
0252         FMT_THROW(format_error("can't OR a terminal color"));
0253       background_color.value.rgb_color |= rhs.background_color.value.rgb_color;
0254     }
0255 
0256     ems = static_cast<emphasis>(static_cast<uint8_t>(ems) |
0257                                 static_cast<uint8_t>(rhs.ems));
0258     return *this;
0259   }
0260 
0261   friend FMT_CONSTEXPR text_style operator|(text_style lhs,
0262                                             const text_style& rhs) {
0263     return lhs |= rhs;
0264   }
0265 
0266   FMT_CONSTEXPR bool has_foreground() const noexcept {
0267     return set_foreground_color;
0268   }
0269   FMT_CONSTEXPR bool has_background() const noexcept {
0270     return set_background_color;
0271   }
0272   FMT_CONSTEXPR bool has_emphasis() const noexcept {
0273     return static_cast<uint8_t>(ems) != 0;
0274   }
0275   FMT_CONSTEXPR detail::color_type get_foreground() const noexcept {
0276     FMT_ASSERT(has_foreground(), "no foreground specified for this style");
0277     return foreground_color;
0278   }
0279   FMT_CONSTEXPR detail::color_type get_background() const noexcept {
0280     FMT_ASSERT(has_background(), "no background specified for this style");
0281     return background_color;
0282   }
0283   FMT_CONSTEXPR emphasis get_emphasis() const noexcept {
0284     FMT_ASSERT(has_emphasis(), "no emphasis specified for this style");
0285     return ems;
0286   }
0287 
0288  private:
0289   FMT_CONSTEXPR text_style(bool is_foreground,
0290                            detail::color_type text_color) noexcept
0291       : set_foreground_color(), set_background_color(), ems() {
0292     if (is_foreground) {
0293       foreground_color = text_color;
0294       set_foreground_color = true;
0295     } else {
0296       background_color = text_color;
0297       set_background_color = true;
0298     }
0299   }
0300 
0301   friend FMT_CONSTEXPR text_style fg(detail::color_type foreground) noexcept;
0302 
0303   friend FMT_CONSTEXPR text_style bg(detail::color_type background) noexcept;
0304 
0305   detail::color_type foreground_color;
0306   detail::color_type background_color;
0307   bool set_foreground_color;
0308   bool set_background_color;
0309   emphasis ems;
0310 };
0311 
0312 /** Creates a text style from the foreground (text) color. */
0313 FMT_CONSTEXPR inline text_style fg(detail::color_type foreground) noexcept {
0314   return text_style(true, foreground);
0315 }
0316 
0317 /** Creates a text style from the background color. */
0318 FMT_CONSTEXPR inline text_style bg(detail::color_type background) noexcept {
0319   return text_style(false, background);
0320 }
0321 
0322 FMT_CONSTEXPR inline text_style operator|(emphasis lhs, emphasis rhs) noexcept {
0323   return text_style(lhs) | rhs;
0324 }
0325 
0326 FMT_BEGIN_DETAIL_NAMESPACE
0327 
0328 template <typename Char> struct ansi_color_escape {
0329   FMT_CONSTEXPR ansi_color_escape(detail::color_type text_color,
0330                                   const char* esc) noexcept {
0331     // If we have a terminal color, we need to output another escape code
0332     // sequence.
0333     if (!text_color.is_rgb) {
0334       bool is_background = esc == string_view("\x1b[48;2;");
0335       uint32_t value = text_color.value.term_color;
0336       // Background ASCII codes are the same as the foreground ones but with
0337       // 10 more.
0338       if (is_background) value += 10u;
0339 
0340       size_t index = 0;
0341       buffer[index++] = static_cast<Char>('\x1b');
0342       buffer[index++] = static_cast<Char>('[');
0343 
0344       if (value >= 100u) {
0345         buffer[index++] = static_cast<Char>('1');
0346         value %= 100u;
0347       }
0348       buffer[index++] = static_cast<Char>('0' + value / 10u);
0349       buffer[index++] = static_cast<Char>('0' + value % 10u);
0350 
0351       buffer[index++] = static_cast<Char>('m');
0352       buffer[index++] = static_cast<Char>('\0');
0353       return;
0354     }
0355 
0356     for (int i = 0; i < 7; i++) {
0357       buffer[i] = static_cast<Char>(esc[i]);
0358     }
0359     rgb color(text_color.value.rgb_color);
0360     to_esc(color.r, buffer + 7, ';');
0361     to_esc(color.g, buffer + 11, ';');
0362     to_esc(color.b, buffer + 15, 'm');
0363     buffer[19] = static_cast<Char>(0);
0364   }
0365   FMT_CONSTEXPR ansi_color_escape(emphasis em) noexcept {
0366     uint8_t em_codes[num_emphases] = {};
0367     if (has_emphasis(em, emphasis::bold)) em_codes[0] = 1;
0368     if (has_emphasis(em, emphasis::faint)) em_codes[1] = 2;
0369     if (has_emphasis(em, emphasis::italic)) em_codes[2] = 3;
0370     if (has_emphasis(em, emphasis::underline)) em_codes[3] = 4;
0371     if (has_emphasis(em, emphasis::blink)) em_codes[4] = 5;
0372     if (has_emphasis(em, emphasis::reverse)) em_codes[5] = 7;
0373     if (has_emphasis(em, emphasis::conceal)) em_codes[6] = 8;
0374     if (has_emphasis(em, emphasis::strikethrough)) em_codes[7] = 9;
0375 
0376     size_t index = 0;
0377     for (size_t i = 0; i < num_emphases; ++i) {
0378       if (!em_codes[i]) continue;
0379       buffer[index++] = static_cast<Char>('\x1b');
0380       buffer[index++] = static_cast<Char>('[');
0381       buffer[index++] = static_cast<Char>('0' + em_codes[i]);
0382       buffer[index++] = static_cast<Char>('m');
0383     }
0384     buffer[index++] = static_cast<Char>(0);
0385   }
0386   FMT_CONSTEXPR operator const Char*() const noexcept { return buffer; }
0387 
0388   FMT_CONSTEXPR const Char* begin() const noexcept { return buffer; }
0389   FMT_CONSTEXPR_CHAR_TRAITS const Char* end() const noexcept {
0390     return buffer + std::char_traits<Char>::length(buffer);
0391   }
0392 
0393  private:
0394   static constexpr size_t num_emphases = 8;
0395   Char buffer[7u + 3u * num_emphases + 1u];
0396 
0397   static FMT_CONSTEXPR void to_esc(uint8_t c, Char* out,
0398                                    char delimiter) noexcept {
0399     out[0] = static_cast<Char>('0' + c / 100);
0400     out[1] = static_cast<Char>('0' + c / 10 % 10);
0401     out[2] = static_cast<Char>('0' + c % 10);
0402     out[3] = static_cast<Char>(delimiter);
0403   }
0404   static FMT_CONSTEXPR bool has_emphasis(emphasis em, emphasis mask) noexcept {
0405     return static_cast<uint8_t>(em) & static_cast<uint8_t>(mask);
0406   }
0407 };
0408 
0409 template <typename Char>
0410 FMT_CONSTEXPR ansi_color_escape<Char> make_foreground_color(
0411     detail::color_type foreground) noexcept {
0412   return ansi_color_escape<Char>(foreground, "\x1b[38;2;");
0413 }
0414 
0415 template <typename Char>
0416 FMT_CONSTEXPR ansi_color_escape<Char> make_background_color(
0417     detail::color_type background) noexcept {
0418   return ansi_color_escape<Char>(background, "\x1b[48;2;");
0419 }
0420 
0421 template <typename Char>
0422 FMT_CONSTEXPR ansi_color_escape<Char> make_emphasis(emphasis em) noexcept {
0423   return ansi_color_escape<Char>(em);
0424 }
0425 
0426 template <typename Char> inline void fputs(const Char* chars, FILE* stream) {
0427   int result = std::fputs(chars, stream);
0428   if (result < 0)
0429     FMT_THROW(system_error(errno, FMT_STRING("cannot write to file")));
0430 }
0431 
0432 template <> inline void fputs<wchar_t>(const wchar_t* chars, FILE* stream) {
0433   int result = std::fputws(chars, stream);
0434   if (result < 0)
0435     FMT_THROW(system_error(errno, FMT_STRING("cannot write to file")));
0436 }
0437 
0438 template <typename Char> inline void reset_color(FILE* stream) {
0439   fputs("\x1b[0m", stream);
0440 }
0441 
0442 template <> inline void reset_color<wchar_t>(FILE* stream) {
0443   fputs(L"\x1b[0m", stream);
0444 }
0445 
0446 template <typename Char> inline void reset_color(buffer<Char>& buffer) {
0447   auto reset_color = string_view("\x1b[0m");
0448   buffer.append(reset_color.begin(), reset_color.end());
0449 }
0450 
0451 template <typename T> struct styled_arg {
0452   const T& value;
0453   text_style style;
0454 };
0455 
0456 template <typename Char>
0457 void vformat_to(buffer<Char>& buf, const text_style& ts,
0458                 basic_string_view<Char> format_str,
0459                 basic_format_args<buffer_context<type_identity_t<Char>>> args) {
0460   bool has_style = false;
0461   if (ts.has_emphasis()) {
0462     has_style = true;
0463     auto emphasis = detail::make_emphasis<Char>(ts.get_emphasis());
0464     buf.append(emphasis.begin(), emphasis.end());
0465   }
0466   if (ts.has_foreground()) {
0467     has_style = true;
0468     auto foreground = detail::make_foreground_color<Char>(ts.get_foreground());
0469     buf.append(foreground.begin(), foreground.end());
0470   }
0471   if (ts.has_background()) {
0472     has_style = true;
0473     auto background = detail::make_background_color<Char>(ts.get_background());
0474     buf.append(background.begin(), background.end());
0475   }
0476   detail::vformat_to(buf, format_str, args, {});
0477   if (has_style) detail::reset_color<Char>(buf);
0478 }
0479 
0480 FMT_END_DETAIL_NAMESPACE
0481 
0482 template <typename S, typename Char = char_t<S>>
0483 void vprint(std::FILE* f, const text_style& ts, const S& format,
0484             basic_format_args<buffer_context<type_identity_t<Char>>> args) {
0485   basic_memory_buffer<Char> buf;
0486   detail::vformat_to(buf, ts, detail::to_string_view(format), args);
0487   if (detail::is_utf8()) {
0488     detail::print(f, basic_string_view<Char>(buf.begin(), buf.size()));
0489   } else {
0490     buf.push_back(Char(0));
0491     detail::fputs(buf.data(), f);
0492   }
0493 }
0494 
0495 /**
0496   \rst
0497   Formats a string and prints it to the specified file stream using ANSI
0498   escape sequences to specify text formatting.
0499 
0500   **Example**::
0501 
0502     fmt::print(fmt::emphasis::bold | fg(fmt::color::red),
0503                "Elapsed time: {0:.2f} seconds", 1.23);
0504   \endrst
0505  */
0506 template <typename S, typename... Args,
0507           FMT_ENABLE_IF(detail::is_string<S>::value)>
0508 void print(std::FILE* f, const text_style& ts, const S& format_str,
0509            const Args&... args) {
0510   vprint(f, ts, format_str,
0511          fmt::make_format_args<buffer_context<char_t<S>>>(args...));
0512 }
0513 
0514 /**
0515   \rst
0516   Formats a string and prints it to stdout using ANSI escape sequences to
0517   specify text formatting.
0518 
0519   **Example**::
0520 
0521     fmt::print(fmt::emphasis::bold | fg(fmt::color::red),
0522                "Elapsed time: {0:.2f} seconds", 1.23);
0523   \endrst
0524  */
0525 template <typename S, typename... Args,
0526           FMT_ENABLE_IF(detail::is_string<S>::value)>
0527 void print(const text_style& ts, const S& format_str, const Args&... args) {
0528   return print(stdout, ts, format_str, args...);
0529 }
0530 
0531 template <typename S, typename Char = char_t<S>>
0532 inline std::basic_string<Char> vformat(
0533     const text_style& ts, const S& format_str,
0534     basic_format_args<buffer_context<type_identity_t<Char>>> args) {
0535   basic_memory_buffer<Char> buf;
0536   detail::vformat_to(buf, ts, detail::to_string_view(format_str), args);
0537   return fmt::to_string(buf);
0538 }
0539 
0540 /**
0541   \rst
0542   Formats arguments and returns the result as a string using ANSI
0543   escape sequences to specify text formatting.
0544 
0545   **Example**::
0546 
0547     #include <fmt/color.h>
0548     std::string message = fmt::format(fmt::emphasis::bold | fg(fmt::color::red),
0549                                       "The answer is {}", 42);
0550   \endrst
0551 */
0552 template <typename S, typename... Args, typename Char = char_t<S>>
0553 inline std::basic_string<Char> format(const text_style& ts, const S& format_str,
0554                                       const Args&... args) {
0555   return fmt::vformat(ts, detail::to_string_view(format_str),
0556                       fmt::make_format_args<buffer_context<Char>>(args...));
0557 }
0558 
0559 /**
0560   Formats a string with the given text_style and writes the output to ``out``.
0561  */
0562 template <typename OutputIt, typename Char,
0563           FMT_ENABLE_IF(detail::is_output_iterator<OutputIt, Char>::value)>
0564 OutputIt vformat_to(
0565     OutputIt out, const text_style& ts, basic_string_view<Char> format_str,
0566     basic_format_args<buffer_context<type_identity_t<Char>>> args) {
0567   auto&& buf = detail::get_buffer<Char>(out);
0568   detail::vformat_to(buf, ts, format_str, args);
0569   return detail::get_iterator(buf);
0570 }
0571 
0572 /**
0573   \rst
0574   Formats arguments with the given text_style, writes the result to the output
0575   iterator ``out`` and returns the iterator past the end of the output range.
0576 
0577   **Example**::
0578 
0579     std::vector<char> out;
0580     fmt::format_to(std::back_inserter(out),
0581                    fmt::emphasis::bold | fg(fmt::color::red), "{}", 42);
0582   \endrst
0583 */
0584 template <typename OutputIt, typename S, typename... Args,
0585           bool enable = detail::is_output_iterator<OutputIt, char_t<S>>::value&&
0586               detail::is_string<S>::value>
0587 inline auto format_to(OutputIt out, const text_style& ts, const S& format_str,
0588                       Args&&... args) ->
0589     typename std::enable_if<enable, OutputIt>::type {
0590   return vformat_to(out, ts, detail::to_string_view(format_str),
0591                     fmt::make_format_args<buffer_context<char_t<S>>>(args...));
0592 }
0593 
0594 template <typename T, typename Char>
0595 struct formatter<detail::styled_arg<T>, Char> : formatter<T, Char> {
0596   template <typename FormatContext>
0597   auto format(const detail::styled_arg<T>& arg, FormatContext& ctx) const
0598       -> decltype(ctx.out()) {
0599     const auto& ts = arg.style;
0600     const auto& value = arg.value;
0601     auto out = ctx.out();
0602 
0603     bool has_style = false;
0604     if (ts.has_emphasis()) {
0605       has_style = true;
0606       auto emphasis = detail::make_emphasis<Char>(ts.get_emphasis());
0607       out = std::copy(emphasis.begin(), emphasis.end(), out);
0608     }
0609     if (ts.has_foreground()) {
0610       has_style = true;
0611       auto foreground =
0612           detail::make_foreground_color<Char>(ts.get_foreground());
0613       out = std::copy(foreground.begin(), foreground.end(), out);
0614     }
0615     if (ts.has_background()) {
0616       has_style = true;
0617       auto background =
0618           detail::make_background_color<Char>(ts.get_background());
0619       out = std::copy(background.begin(), background.end(), out);
0620     }
0621     out = formatter<T, Char>::format(value, ctx);
0622     if (has_style) {
0623       auto reset_color = string_view("\x1b[0m");
0624       out = std::copy(reset_color.begin(), reset_color.end(), out);
0625     }
0626     return out;
0627   }
0628 };
0629 
0630 /**
0631   \rst
0632   Returns an argument that will be formatted using ANSI escape sequences,
0633   to be used in a formatting function.
0634 
0635   **Example**::
0636 
0637     fmt::print("Elapsed time: {0:.2f} seconds",
0638                fmt::styled(1.23, fmt::fg(fmt::color::green) |
0639                                  fmt::bg(fmt::color::blue)));
0640   \endrst
0641  */
0642 template <typename T>
0643 FMT_CONSTEXPR auto styled(const T& value, text_style ts)
0644     -> detail::styled_arg<remove_cvref_t<T>> {
0645   return detail::styled_arg<remove_cvref_t<T>>{value, ts};
0646 }
0647 
0648 FMT_MODULE_EXPORT_END
0649 FMT_END_NAMESPACE
0650 
0651 #endif  // FMT_COLOR_H_