1 // Formatting library for C++ - color support
2 //
3 // Copyright (c) 2018 - present, Victor Zverovich and fmt contributors
4 // All rights reserved.
5 //
6 // For the license information refer to format.h.
7 
8 #ifndef FMT_COLOR_H_
9 #define FMT_COLOR_H_
10 
11 #include "format.h"
12 
13 FMT_BEGIN_NAMESPACE
14 
15 #ifdef FMT_DEPRECATED_COLORS
16 
17 // color and (v)print_colored are deprecated.
18 enum color { black, red, green, yellow, blue, magenta, cyan, white };
19 FMT_API void vprint_colored(color c, string_view format, format_args args);
20 FMT_API void vprint_colored(color c, wstring_view format, wformat_args args);
21 template <typename... Args>
print_colored(color c,string_view format_str,const Args &...args)22 inline void print_colored(color c, string_view format_str,
23                           const Args & ... args) {
24   vprint_colored(c, format_str, make_format_args(args...));
25 }
26 template <typename... Args>
print_colored(color c,wstring_view format_str,const Args &...args)27 inline void print_colored(color c, wstring_view format_str,
28                           const Args & ... args) {
29   vprint_colored(c, format_str, make_format_args<wformat_context>(args...));
30 }
31 
vprint_colored(color c,string_view format,format_args args)32 inline void vprint_colored(color c, string_view format, format_args args) {
33   char escape[] = "\x1b[30m";
34   escape[3] = static_cast<char>('0' + c);
35   std::fputs(escape, stdout);
36   vprint(format, args);
37   std::fputs(internal::data::RESET_COLOR, stdout);
38 }
39 
vprint_colored(color c,wstring_view format,wformat_args args)40 inline void vprint_colored(color c, wstring_view format, wformat_args args) {
41   wchar_t escape[] = L"\x1b[30m";
42   escape[3] = static_cast<wchar_t>('0' + c);
43   std::fputws(escape, stdout);
44   vprint(format, args);
45   std::fputws(internal::data::WRESET_COLOR, stdout);
46 }
47 
48 #else
49 
50 enum class color : uint32_t {
51   alice_blue              = 0xF0F8FF, // rgb(240,248,255)
52   antique_white           = 0xFAEBD7, // rgb(250,235,215)
53   aqua                    = 0x00FFFF, // rgb(0,255,255)
54   aquamarine              = 0x7FFFD4, // rgb(127,255,212)
55   azure                   = 0xF0FFFF, // rgb(240,255,255)
56   beige                   = 0xF5F5DC, // rgb(245,245,220)
57   bisque                  = 0xFFE4C4, // rgb(255,228,196)
58   black                   = 0x000000, // rgb(0,0,0)
59   blanched_almond         = 0xFFEBCD, // rgb(255,235,205)
60   blue                    = 0x0000FF, // rgb(0,0,255)
61   blue_violet             = 0x8A2BE2, // rgb(138,43,226)
62   brown                   = 0xA52A2A, // rgb(165,42,42)
63   burly_wood              = 0xDEB887, // rgb(222,184,135)
64   cadet_blue              = 0x5F9EA0, // rgb(95,158,160)
65   chartreuse              = 0x7FFF00, // rgb(127,255,0)
66   chocolate               = 0xD2691E, // rgb(210,105,30)
67   coral                   = 0xFF7F50, // rgb(255,127,80)
68   cornflower_blue         = 0x6495ED, // rgb(100,149,237)
69   cornsilk                = 0xFFF8DC, // rgb(255,248,220)
70   crimson                 = 0xDC143C, // rgb(220,20,60)
71   cyan                    = 0x00FFFF, // rgb(0,255,255)
72   dark_blue               = 0x00008B, // rgb(0,0,139)
73   dark_cyan               = 0x008B8B, // rgb(0,139,139)
74   dark_golden_rod         = 0xB8860B, // rgb(184,134,11)
75   dark_gray               = 0xA9A9A9, // rgb(169,169,169)
76   dark_green              = 0x006400, // rgb(0,100,0)
77   dark_khaki              = 0xBDB76B, // rgb(189,183,107)
78   dark_magenta            = 0x8B008B, // rgb(139,0,139)
79   dark_olive_green        = 0x556B2F, // rgb(85,107,47)
80   dark_orange             = 0xFF8C00, // rgb(255,140,0)
81   dark_orchid             = 0x9932CC, // rgb(153,50,204)
82   dark_red                = 0x8B0000, // rgb(139,0,0)
83   dark_salmon             = 0xE9967A, // rgb(233,150,122)
84   dark_sea_green          = 0x8FBC8F, // rgb(143,188,143)
85   dark_slate_blue         = 0x483D8B, // rgb(72,61,139)
86   dark_slate_gray         = 0x2F4F4F, // rgb(47,79,79)
87   dark_turquoise          = 0x00CED1, // rgb(0,206,209)
88   dark_violet             = 0x9400D3, // rgb(148,0,211)
89   deep_pink               = 0xFF1493, // rgb(255,20,147)
90   deep_sky_blue           = 0x00BFFF, // rgb(0,191,255)
91   dim_gray                = 0x696969, // rgb(105,105,105)
92   dodger_blue             = 0x1E90FF, // rgb(30,144,255)
93   fire_brick              = 0xB22222, // rgb(178,34,34)
94   floral_white            = 0xFFFAF0, // rgb(255,250,240)
95   forest_green            = 0x228B22, // rgb(34,139,34)
96   fuchsia                 = 0xFF00FF, // rgb(255,0,255)
97   gainsboro               = 0xDCDCDC, // rgb(220,220,220)
98   ghost_white             = 0xF8F8FF, // rgb(248,248,255)
99   gold                    = 0xFFD700, // rgb(255,215,0)
100   golden_rod              = 0xDAA520, // rgb(218,165,32)
101   gray                    = 0x808080, // rgb(128,128,128)
102   green                   = 0x008000, // rgb(0,128,0)
103   green_yellow            = 0xADFF2F, // rgb(173,255,47)
104   honey_dew               = 0xF0FFF0, // rgb(240,255,240)
105   hot_pink                = 0xFF69B4, // rgb(255,105,180)
106   indian_red              = 0xCD5C5C, // rgb(205,92,92)
107   indigo                  = 0x4B0082, // rgb(75,0,130)
108   ivory                   = 0xFFFFF0, // rgb(255,255,240)
109   khaki                   = 0xF0E68C, // rgb(240,230,140)
110   lavender                = 0xE6E6FA, // rgb(230,230,250)
111   lavender_blush          = 0xFFF0F5, // rgb(255,240,245)
112   lawn_green              = 0x7CFC00, // rgb(124,252,0)
113   lemon_chiffon           = 0xFFFACD, // rgb(255,250,205)
114   light_blue              = 0xADD8E6, // rgb(173,216,230)
115   light_coral             = 0xF08080, // rgb(240,128,128)
116   light_cyan              = 0xE0FFFF, // rgb(224,255,255)
117   light_golden_rod_yellow = 0xFAFAD2, // rgb(250,250,210)
118   light_gray              = 0xD3D3D3, // rgb(211,211,211)
119   light_green             = 0x90EE90, // rgb(144,238,144)
120   light_pink              = 0xFFB6C1, // rgb(255,182,193)
121   light_salmon            = 0xFFA07A, // rgb(255,160,122)
122   light_sea_green         = 0x20B2AA, // rgb(32,178,170)
123   light_sky_blue          = 0x87CEFA, // rgb(135,206,250)
124   light_slate_gray        = 0x778899, // rgb(119,136,153)
125   light_steel_blue        = 0xB0C4DE, // rgb(176,196,222)
126   light_yellow            = 0xFFFFE0, // rgb(255,255,224)
127   lime                    = 0x00FF00, // rgb(0,255,0)
128   lime_green              = 0x32CD32, // rgb(50,205,50)
129   linen                   = 0xFAF0E6, // rgb(250,240,230)
130   magenta                 = 0xFF00FF, // rgb(255,0,255)
131   maroon                  = 0x800000, // rgb(128,0,0)
132   medium_aquamarine       = 0x66CDAA, // rgb(102,205,170)
133   medium_blue             = 0x0000CD, // rgb(0,0,205)
134   medium_orchid           = 0xBA55D3, // rgb(186,85,211)
135   medium_purple           = 0x9370DB, // rgb(147,112,219)
136   medium_sea_green        = 0x3CB371, // rgb(60,179,113)
137   medium_slate_blue       = 0x7B68EE, // rgb(123,104,238)
138   medium_spring_green     = 0x00FA9A, // rgb(0,250,154)
139   medium_turquoise        = 0x48D1CC, // rgb(72,209,204)
140   medium_violet_red       = 0xC71585, // rgb(199,21,133)
141   midnight_blue           = 0x191970, // rgb(25,25,112)
142   mint_cream              = 0xF5FFFA, // rgb(245,255,250)
143   misty_rose              = 0xFFE4E1, // rgb(255,228,225)
144   moccasin                = 0xFFE4B5, // rgb(255,228,181)
145   navajo_white            = 0xFFDEAD, // rgb(255,222,173)
146   navy                    = 0x000080, // rgb(0,0,128)
147   old_lace                = 0xFDF5E6, // rgb(253,245,230)
148   olive                   = 0x808000, // rgb(128,128,0)
149   olive_drab              = 0x6B8E23, // rgb(107,142,35)
150   orange                  = 0xFFA500, // rgb(255,165,0)
151   orange_red              = 0xFF4500, // rgb(255,69,0)
152   orchid                  = 0xDA70D6, // rgb(218,112,214)
153   pale_golden_rod         = 0xEEE8AA, // rgb(238,232,170)
154   pale_green              = 0x98FB98, // rgb(152,251,152)
155   pale_turquoise          = 0xAFEEEE, // rgb(175,238,238)
156   pale_violet_red         = 0xDB7093, // rgb(219,112,147)
157   papaya_whip             = 0xFFEFD5, // rgb(255,239,213)
158   peach_puff              = 0xFFDAB9, // rgb(255,218,185)
159   peru                    = 0xCD853F, // rgb(205,133,63)
160   pink                    = 0xFFC0CB, // rgb(255,192,203)
161   plum                    = 0xDDA0DD, // rgb(221,160,221)
162   powder_blue             = 0xB0E0E6, // rgb(176,224,230)
163   purple                  = 0x800080, // rgb(128,0,128)
164   rebecca_purple          = 0x663399, // rgb(102,51,153)
165   red                     = 0xFF0000, // rgb(255,0,0)
166   rosy_brown              = 0xBC8F8F, // rgb(188,143,143)
167   royal_blue              = 0x4169E1, // rgb(65,105,225)
168   saddle_brown            = 0x8B4513, // rgb(139,69,19)
169   salmon                  = 0xFA8072, // rgb(250,128,114)
170   sandy_brown             = 0xF4A460, // rgb(244,164,96)
171   sea_green               = 0x2E8B57, // rgb(46,139,87)
172   sea_shell               = 0xFFF5EE, // rgb(255,245,238)
173   sienna                  = 0xA0522D, // rgb(160,82,45)
174   silver                  = 0xC0C0C0, // rgb(192,192,192)
175   sky_blue                = 0x87CEEB, // rgb(135,206,235)
176   slate_blue              = 0x6A5ACD, // rgb(106,90,205)
177   slate_gray              = 0x708090, // rgb(112,128,144)
178   snow                    = 0xFFFAFA, // rgb(255,250,250)
179   spring_green            = 0x00FF7F, // rgb(0,255,127)
180   steel_blue              = 0x4682B4, // rgb(70,130,180)
181   tan                     = 0xD2B48C, // rgb(210,180,140)
182   teal                    = 0x008080, // rgb(0,128,128)
183   thistle                 = 0xD8BFD8, // rgb(216,191,216)
184   tomato                  = 0xFF6347, // rgb(255,99,71)
185   turquoise               = 0x40E0D0, // rgb(64,224,208)
186   violet                  = 0xEE82EE, // rgb(238,130,238)
187   wheat                   = 0xF5DEB3, // rgb(245,222,179)
188   white                   = 0xFFFFFF, // rgb(255,255,255)
189   white_smoke             = 0xF5F5F5, // rgb(245,245,245)
190   yellow                  = 0xFFFF00, // rgb(255,255,0)
191   yellow_green            = 0x9ACD32  // rgb(154,205,50)
192 };  // enum class color
193 
194 enum class terminal_color : uint8_t {
195   black = 30,
196   red,
197   green,
198   yellow,
199   blue,
200   magenta,
201   cyan,
202   white,
203   bright_black = 90,
204   bright_red,
205   bright_green,
206   bright_yellow,
207   bright_blue,
208   bright_magenta,
209   bright_cyan,
210   bright_white
211 };  // enum class terminal_color
212 
213 enum class emphasis : uint8_t {
214   bold = 1,
215   italic = 1 << 1,
216   underline = 1 << 2,
217   strikethrough = 1 << 3
218 };  // enum class emphasis
219 
220 // rgb is a struct for red, green and blue colors.
221 // We use rgb as name because some editors will show it as color direct in the
222 // editor.
223 struct rgb {
224   FMT_CONSTEXPR_DECL rgb() : r(0), g(0), b(0) {}
225   FMT_CONSTEXPR_DECL rgb(uint8_t r_, uint8_t g_, uint8_t b_)
226     : r(r_), g(g_), b(b_) {}
227   FMT_CONSTEXPR_DECL rgb(uint32_t hex)
228     : r((hex >> 16) & 0xFF), g((hex >> 8) & 0xFF), b((hex) & 0xFF) {}
229   FMT_CONSTEXPR_DECL rgb(color hex)
230     : r((uint32_t(hex) >> 16) & 0xFF), g((uint32_t(hex) >> 8) & 0xFF),
231       b(uint32_t(hex) & 0xFF) {}
232   uint8_t r;
233   uint8_t g;
234   uint8_t b;
235 };
236 
237 namespace internal {
238 
239 // color is a struct of either a rgb color or a terminal color.
240 struct color_type {
241   FMT_CONSTEXPR color_type() FMT_NOEXCEPT
242     : is_rgb(), value{} {}
243   FMT_CONSTEXPR color_type(color rgb_color) FMT_NOEXCEPT
244     : is_rgb(true), value{} {
245     value.rgb_color = static_cast<uint32_t>(rgb_color);
246   }
247   FMT_CONSTEXPR color_type(rgb rgb_color) FMT_NOEXCEPT
248     : is_rgb(true), value{} {
249     value.rgb_color = (static_cast<uint32_t>(rgb_color.r) << 16)
250        | (static_cast<uint32_t>(rgb_color.g) << 8) | rgb_color.b;
251   }
252   FMT_CONSTEXPR color_type(terminal_color term_color) FMT_NOEXCEPT
253     : is_rgb(), value{} {
254     value.term_color = static_cast<uint8_t>(term_color);
255   }
256   bool is_rgb;
257   union color_union {
258     uint8_t term_color;
259     uint32_t rgb_color;
260   } value;
261 };
262 } // namespace internal
263 
264 // Experimental text formatting support.
265 class text_style {
266  public:
267   FMT_CONSTEXPR text_style(emphasis em = emphasis()) FMT_NOEXCEPT
268       : set_foreground_color(), set_background_color(), ems(em) {}
269 
270   FMT_CONSTEXPR text_style &operator|=(const text_style &rhs) {
271     if (!set_foreground_color) {
272       set_foreground_color = rhs.set_foreground_color;
273       foreground_color = rhs.foreground_color;
274     } else if (rhs.set_foreground_color) {
275       if (!foreground_color.is_rgb || !rhs.foreground_color.is_rgb)
276         throw format_error("can't OR a terminal color");
277       foreground_color.value.rgb_color |= rhs.foreground_color.value.rgb_color;
278     }
279 
280     if (!set_background_color) {
281       set_background_color = rhs.set_background_color;
282       background_color = rhs.background_color;
283     } else if (rhs.set_background_color) {
284       if (!background_color.is_rgb || !rhs.background_color.is_rgb)
285         throw format_error("can't OR a terminal color");
286       background_color.value.rgb_color |= rhs.background_color.value.rgb_color;
287     }
288 
289     ems = static_cast<emphasis>(static_cast<uint8_t>(ems) |
290                                 static_cast<uint8_t>(rhs.ems));
291     return *this;
292   }
293 
294   friend FMT_CONSTEXPR
295   text_style operator|(text_style lhs, const text_style &rhs) {
296     return lhs |= rhs;
297   }
298 
299   FMT_CONSTEXPR text_style &operator&=(const text_style &rhs) {
300     if (!set_foreground_color) {
301       set_foreground_color = rhs.set_foreground_color;
302       foreground_color = rhs.foreground_color;
303     } else if (rhs.set_foreground_color) {
304       if (!foreground_color.is_rgb || !rhs.foreground_color.is_rgb)
305         throw format_error("can't AND a terminal color");
306       foreground_color.value.rgb_color &= rhs.foreground_color.value.rgb_color;
307     }
308 
309     if (!set_background_color) {
310       set_background_color = rhs.set_background_color;
311       background_color = rhs.background_color;
312     } else if (rhs.set_background_color) {
313       if (!background_color.is_rgb || !rhs.background_color.is_rgb)
314         throw format_error("can't AND a terminal color");
315       background_color.value.rgb_color &= rhs.background_color.value.rgb_color;
316     }
317 
318     ems = static_cast<emphasis>(static_cast<uint8_t>(ems) &
319                                 static_cast<uint8_t>(rhs.ems));
320     return *this;
321   }
322 
323   friend FMT_CONSTEXPR
324   text_style operator&(text_style lhs, const text_style &rhs) {
325     return lhs &= rhs;
326   }
327 
328   FMT_CONSTEXPR bool has_foreground() const FMT_NOEXCEPT {
329     return set_foreground_color;
330   }
331   FMT_CONSTEXPR bool has_background() const FMT_NOEXCEPT {
332     return set_background_color;
333   }
334   FMT_CONSTEXPR bool has_emphasis() const FMT_NOEXCEPT {
335     return static_cast<uint8_t>(ems) != 0;
336   }
337   FMT_CONSTEXPR internal::color_type get_foreground() const FMT_NOEXCEPT {
338     assert(has_foreground() && "no foreground specified for this style");
339     return foreground_color;
340   }
341   FMT_CONSTEXPR internal::color_type get_background() const FMT_NOEXCEPT {
342     assert(has_background() && "no background specified for this style");
343     return background_color;
344   }
345   FMT_CONSTEXPR emphasis get_emphasis() const FMT_NOEXCEPT {
346     assert(has_emphasis() && "no emphasis specified for this style");
347     return ems;
348   }
349 
350 private:
351  FMT_CONSTEXPR text_style(bool is_foreground,
352                           internal::color_type text_color) FMT_NOEXCEPT
353      : set_foreground_color(),
354        set_background_color(),
355        ems() {
356    if (is_foreground) {
357      foreground_color = text_color;
358      set_foreground_color = true;
359    } else {
360      background_color = text_color;
361      set_background_color = true;
362    }
363  }
364 
365   friend FMT_CONSTEXPR_DECL text_style fg(internal::color_type foreground)
366       FMT_NOEXCEPT;
367   friend FMT_CONSTEXPR_DECL text_style bg(internal::color_type background)
368       FMT_NOEXCEPT;
369 
370   internal::color_type foreground_color;
371   internal::color_type background_color;
372   bool set_foreground_color;
373   bool set_background_color;
374   emphasis ems;
375 };
376 
377 FMT_CONSTEXPR text_style fg(internal::color_type foreground) FMT_NOEXCEPT {
378   return text_style(/*is_foreground=*/true, foreground);
379 }
380 
381 FMT_CONSTEXPR text_style bg(internal::color_type background) FMT_NOEXCEPT {
382   return text_style(/*is_foreground=*/false, background);
383 }
384 
385 FMT_CONSTEXPR text_style operator|(emphasis lhs, emphasis rhs) FMT_NOEXCEPT {
386   return text_style(lhs) | rhs;
387 }
388 
389 namespace internal {
390 
391 template <typename Char>
392 struct ansi_color_escape {
393   FMT_CONSTEXPR ansi_color_escape(internal::color_type text_color,
394                                   const char * esc) FMT_NOEXCEPT {
395     // If we have a terminal color, we need to output another escape code
396     // sequence.
397     if (!text_color.is_rgb) {
398       bool is_background = esc == internal::data::BACKGROUND_COLOR;
399       uint32_t value = text_color.value.term_color;
400       // Background ASCII codes are the same as the foreground ones but with
401       // 10 more.
402       if (is_background)
403         value += 10u;
404 
405       std::size_t index = 0;
406       buffer[index++] = static_cast<Char>('\x1b');
407       buffer[index++] = static_cast<Char>('[');
408 
409       if (value >= 100u) {
410         buffer[index++] = static_cast<Char>('1');
411         value %= 100u;
412       }
413       buffer[index++] = static_cast<Char>('0' + value / 10u);
414       buffer[index++] = static_cast<Char>('0' + value % 10u);
415 
416       buffer[index++] = static_cast<Char>('m');
417       buffer[index++] = static_cast<Char>('\0');
418       return;
419     }
420 
421     for (int i = 0; i < 7; i++) {
422       buffer[i] = static_cast<Char>(esc[i]);
423     }
424     rgb color(text_color.value.rgb_color);
425     to_esc(color.r, buffer +  7, ';');
426     to_esc(color.g, buffer + 11, ';');
427     to_esc(color.b, buffer + 15, 'm');
428     buffer[19] = static_cast<Char>(0);
429   }
430   FMT_CONSTEXPR ansi_color_escape(emphasis em) FMT_NOEXCEPT {
431     uint8_t em_codes[4] = {};
432     uint8_t em_bits = static_cast<uint8_t>(em);
433     if (em_bits & static_cast<uint8_t>(emphasis::bold))
434       em_codes[0] = 1;
435     if (em_bits & static_cast<uint8_t>(emphasis::italic))
436       em_codes[1] = 3;
437     if (em_bits & static_cast<uint8_t>(emphasis::underline))
438       em_codes[2] = 4;
439     if (em_bits & static_cast<uint8_t>(emphasis::strikethrough))
440       em_codes[3] = 9;
441 
442     std::size_t index = 0;
443     for (int i = 0; i < 4; ++i) {
444       if (!em_codes[i])
445         continue;
446       buffer[index++] = static_cast<Char>('\x1b');
447       buffer[index++] = static_cast<Char>('[');
448       buffer[index++] = static_cast<Char>('0' + em_codes[i]);
449       buffer[index++] = static_cast<Char>('m');
450     }
451     buffer[index++] = static_cast<Char>(0);
452   }
453   FMT_CONSTEXPR operator const Char *() const FMT_NOEXCEPT { return buffer; }
454 
455 private:
456   Char buffer[7u + 3u * 4u + 1u];
457 
458   static FMT_CONSTEXPR void to_esc(uint8_t c, Char *out,
459                                    char delimiter) FMT_NOEXCEPT {
460     out[0] = static_cast<Char>('0' + c / 100);
461     out[1] = static_cast<Char>('0' + c / 10 % 10);
462     out[2] = static_cast<Char>('0' + c % 10);
463     out[3] = static_cast<Char>(delimiter);
464   }
465 };
466 
467 template <typename Char>
468 FMT_CONSTEXPR ansi_color_escape<Char>
469 make_foreground_color(internal::color_type foreground) FMT_NOEXCEPT {
470   return ansi_color_escape<Char>(foreground, internal::data::FOREGROUND_COLOR);
471 }
472 
473 template <typename Char>
474 FMT_CONSTEXPR ansi_color_escape<Char>
475 make_background_color(internal::color_type background) FMT_NOEXCEPT {
476   return ansi_color_escape<Char>(background, internal::data::BACKGROUND_COLOR);
477 }
478 
479 template <typename Char>
480 FMT_CONSTEXPR ansi_color_escape<Char>
481 make_emphasis(emphasis em) FMT_NOEXCEPT {
482   return ansi_color_escape<Char>(em);
483 }
484 
485 template <typename Char>
486 inline void fputs(const Char *chars, FILE *stream) FMT_NOEXCEPT {
487   std::fputs(chars, stream);
488 }
489 
490 template <>
491 inline void fputs<wchar_t>(const wchar_t *chars, FILE *stream) FMT_NOEXCEPT {
492   std::fputws(chars, stream);
493 }
494 
495 template <typename Char>
496 inline void reset_color(FILE *stream) FMT_NOEXCEPT {
497   fputs(internal::data::RESET_COLOR, stream);
498 }
499 
500 template <>
501 inline void reset_color<wchar_t>(FILE *stream) FMT_NOEXCEPT {
502   fputs(internal::data::WRESET_COLOR, stream);
503 }
504 
505 // The following specialiazation disables using std::FILE as a character type,
506 // which is needed because or else
507 //   fmt::print(stderr, fmt::emphasis::bold, "");
508 // would take stderr (a std::FILE *) as the format string.
509 template <>
510 struct is_string<std::FILE *> : std::false_type {};
511 template <>
512 struct is_string<const std::FILE *> : std::false_type {};
513 } // namespace internal
514 
515 template <
516   typename S, typename Char = typename internal::char_t<S>::type>
517 void vprint(std::FILE *f, const text_style &ts, const S &format,
518             basic_format_args<typename buffer_context<Char>::type> args) {
519   bool has_style = false;
520   if (ts.has_emphasis()) {
521     has_style = true;
522     internal::fputs<Char>(
523           internal::make_emphasis<Char>(ts.get_emphasis()), f);
524   }
525   if (ts.has_foreground()) {
526     has_style = true;
527     internal::fputs<Char>(
528           internal::make_foreground_color<Char>(ts.get_foreground()), f);
529   }
530   if (ts.has_background()) {
531     has_style = true;
532     internal::fputs<Char>(
533         internal::make_background_color<Char>(ts.get_background()), f);
534   }
535   vprint(f, format, args);
536   if (has_style) {
537     internal::reset_color<Char>(f);
538   }
539 }
540 
541 /**
542   Formats a string and prints it to the specified file stream using ANSI
543   escape sequences to specify text formatting.
544   Example:
545     fmt::print(fmt::emphasis::bold | fg(fmt::color::red),
546                "Elapsed time: {0:.2f} seconds", 1.23);
547  */
548 template <typename String, typename... Args>
549 typename std::enable_if<internal::is_string<String>::value>::type print(
550     std::FILE *f, const text_style &ts, const String &format_str,
551     const Args &... args) {
552   internal::check_format_string<Args...>(format_str);
553   typedef typename internal::char_t<String>::type char_t;
554   typedef typename buffer_context<char_t>::type context_t;
555   format_arg_store<context_t, Args...> as{args...};
556   vprint(f, ts, format_str, basic_format_args<context_t>(as));
557 }
558 
559 /**
560   Formats a string and prints it to stdout using ANSI escape sequences to
561   specify text formatting.
562   Example:
563     fmt::print(fmt::emphasis::bold | fg(fmt::color::red),
564                "Elapsed time: {0:.2f} seconds", 1.23);
565  */
566 template <typename String, typename... Args>
567 typename std::enable_if<internal::is_string<String>::value>::type print(
568     const text_style &ts, const String &format_str,
569     const Args &... args) {
570   return print(stdout, ts, format_str, args...);
571 }
572 
573 #endif
574 
575 FMT_END_NAMESPACE
576 
577 #endif  // FMT_COLOR_H_
578