ampsci
c++ program for high-precision atomic structure calculations of single-valence systems
color.h
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 #ifdef __GNUC__
12 // Avoid tons of warnings with root code
13 #pragma GCC system_header
14 #endif
15 #ifdef __clang__
16 // Avoid tons of warnings with root code
17 #pragma clang system_header
18 #endif
19 
20 #include "format.h"
21 
22 FMT_BEGIN_NAMESPACE
23 FMT_MODULE_EXPORT_BEGIN
24 
25 enum class color : uint32_t {
26  alice_blue = 0xF0F8FF, // rgb(240,248,255)
27  antique_white = 0xFAEBD7, // rgb(250,235,215)
28  aqua = 0x00FFFF, // rgb(0,255,255)
29  aquamarine = 0x7FFFD4, // rgb(127,255,212)
30  azure = 0xF0FFFF, // rgb(240,255,255)
31  beige = 0xF5F5DC, // rgb(245,245,220)
32  bisque = 0xFFE4C4, // rgb(255,228,196)
33  black = 0x000000, // rgb(0,0,0)
34  blanched_almond = 0xFFEBCD, // rgb(255,235,205)
35  blue = 0x0000FF, // rgb(0,0,255)
36  blue_violet = 0x8A2BE2, // rgb(138,43,226)
37  brown = 0xA52A2A, // rgb(165,42,42)
38  burly_wood = 0xDEB887, // rgb(222,184,135)
39  cadet_blue = 0x5F9EA0, // rgb(95,158,160)
40  chartreuse = 0x7FFF00, // rgb(127,255,0)
41  chocolate = 0xD2691E, // rgb(210,105,30)
42  coral = 0xFF7F50, // rgb(255,127,80)
43  cornflower_blue = 0x6495ED, // rgb(100,149,237)
44  cornsilk = 0xFFF8DC, // rgb(255,248,220)
45  crimson = 0xDC143C, // rgb(220,20,60)
46  cyan = 0x00FFFF, // rgb(0,255,255)
47  dark_blue = 0x00008B, // rgb(0,0,139)
48  dark_cyan = 0x008B8B, // rgb(0,139,139)
49  dark_golden_rod = 0xB8860B, // rgb(184,134,11)
50  dark_gray = 0xA9A9A9, // rgb(169,169,169)
51  dark_green = 0x006400, // rgb(0,100,0)
52  dark_khaki = 0xBDB76B, // rgb(189,183,107)
53  dark_magenta = 0x8B008B, // rgb(139,0,139)
54  dark_olive_green = 0x556B2F, // rgb(85,107,47)
55  dark_orange = 0xFF8C00, // rgb(255,140,0)
56  dark_orchid = 0x9932CC, // rgb(153,50,204)
57  dark_red = 0x8B0000, // rgb(139,0,0)
58  dark_salmon = 0xE9967A, // rgb(233,150,122)
59  dark_sea_green = 0x8FBC8F, // rgb(143,188,143)
60  dark_slate_blue = 0x483D8B, // rgb(72,61,139)
61  dark_slate_gray = 0x2F4F4F, // rgb(47,79,79)
62  dark_turquoise = 0x00CED1, // rgb(0,206,209)
63  dark_violet = 0x9400D3, // rgb(148,0,211)
64  deep_pink = 0xFF1493, // rgb(255,20,147)
65  deep_sky_blue = 0x00BFFF, // rgb(0,191,255)
66  dim_gray = 0x696969, // rgb(105,105,105)
67  dodger_blue = 0x1E90FF, // rgb(30,144,255)
68  fire_brick = 0xB22222, // rgb(178,34,34)
69  floral_white = 0xFFFAF0, // rgb(255,250,240)
70  forest_green = 0x228B22, // rgb(34,139,34)
71  fuchsia = 0xFF00FF, // rgb(255,0,255)
72  gainsboro = 0xDCDCDC, // rgb(220,220,220)
73  ghost_white = 0xF8F8FF, // rgb(248,248,255)
74  gold = 0xFFD700, // rgb(255,215,0)
75  golden_rod = 0xDAA520, // rgb(218,165,32)
76  gray = 0x808080, // rgb(128,128,128)
77  green = 0x008000, // rgb(0,128,0)
78  green_yellow = 0xADFF2F, // rgb(173,255,47)
79  honey_dew = 0xF0FFF0, // rgb(240,255,240)
80  hot_pink = 0xFF69B4, // rgb(255,105,180)
81  indian_red = 0xCD5C5C, // rgb(205,92,92)
82  indigo = 0x4B0082, // rgb(75,0,130)
83  ivory = 0xFFFFF0, // rgb(255,255,240)
84  khaki = 0xF0E68C, // rgb(240,230,140)
85  lavender = 0xE6E6FA, // rgb(230,230,250)
86  lavender_blush = 0xFFF0F5, // rgb(255,240,245)
87  lawn_green = 0x7CFC00, // rgb(124,252,0)
88  lemon_chiffon = 0xFFFACD, // rgb(255,250,205)
89  light_blue = 0xADD8E6, // rgb(173,216,230)
90  light_coral = 0xF08080, // rgb(240,128,128)
91  light_cyan = 0xE0FFFF, // rgb(224,255,255)
92  light_golden_rod_yellow = 0xFAFAD2, // rgb(250,250,210)
93  light_gray = 0xD3D3D3, // rgb(211,211,211)
94  light_green = 0x90EE90, // rgb(144,238,144)
95  light_pink = 0xFFB6C1, // rgb(255,182,193)
96  light_salmon = 0xFFA07A, // rgb(255,160,122)
97  light_sea_green = 0x20B2AA, // rgb(32,178,170)
98  light_sky_blue = 0x87CEFA, // rgb(135,206,250)
99  light_slate_gray = 0x778899, // rgb(119,136,153)
100  light_steel_blue = 0xB0C4DE, // rgb(176,196,222)
101  light_yellow = 0xFFFFE0, // rgb(255,255,224)
102  lime = 0x00FF00, // rgb(0,255,0)
103  lime_green = 0x32CD32, // rgb(50,205,50)
104  linen = 0xFAF0E6, // rgb(250,240,230)
105  magenta = 0xFF00FF, // rgb(255,0,255)
106  maroon = 0x800000, // rgb(128,0,0)
107  medium_aquamarine = 0x66CDAA, // rgb(102,205,170)
108  medium_blue = 0x0000CD, // rgb(0,0,205)
109  medium_orchid = 0xBA55D3, // rgb(186,85,211)
110  medium_purple = 0x9370DB, // rgb(147,112,219)
111  medium_sea_green = 0x3CB371, // rgb(60,179,113)
112  medium_slate_blue = 0x7B68EE, // rgb(123,104,238)
113  medium_spring_green = 0x00FA9A, // rgb(0,250,154)
114  medium_turquoise = 0x48D1CC, // rgb(72,209,204)
115  medium_violet_red = 0xC71585, // rgb(199,21,133)
116  midnight_blue = 0x191970, // rgb(25,25,112)
117  mint_cream = 0xF5FFFA, // rgb(245,255,250)
118  misty_rose = 0xFFE4E1, // rgb(255,228,225)
119  moccasin = 0xFFE4B5, // rgb(255,228,181)
120  navajo_white = 0xFFDEAD, // rgb(255,222,173)
121  navy = 0x000080, // rgb(0,0,128)
122  old_lace = 0xFDF5E6, // rgb(253,245,230)
123  olive = 0x808000, // rgb(128,128,0)
124  olive_drab = 0x6B8E23, // rgb(107,142,35)
125  orange = 0xFFA500, // rgb(255,165,0)
126  orange_red = 0xFF4500, // rgb(255,69,0)
127  orchid = 0xDA70D6, // rgb(218,112,214)
128  pale_golden_rod = 0xEEE8AA, // rgb(238,232,170)
129  pale_green = 0x98FB98, // rgb(152,251,152)
130  pale_turquoise = 0xAFEEEE, // rgb(175,238,238)
131  pale_violet_red = 0xDB7093, // rgb(219,112,147)
132  papaya_whip = 0xFFEFD5, // rgb(255,239,213)
133  peach_puff = 0xFFDAB9, // rgb(255,218,185)
134  peru = 0xCD853F, // rgb(205,133,63)
135  pink = 0xFFC0CB, // rgb(255,192,203)
136  plum = 0xDDA0DD, // rgb(221,160,221)
137  powder_blue = 0xB0E0E6, // rgb(176,224,230)
138  purple = 0x800080, // rgb(128,0,128)
139  rebecca_purple = 0x663399, // rgb(102,51,153)
140  red = 0xFF0000, // rgb(255,0,0)
141  rosy_brown = 0xBC8F8F, // rgb(188,143,143)
142  royal_blue = 0x4169E1, // rgb(65,105,225)
143  saddle_brown = 0x8B4513, // rgb(139,69,19)
144  salmon = 0xFA8072, // rgb(250,128,114)
145  sandy_brown = 0xF4A460, // rgb(244,164,96)
146  sea_green = 0x2E8B57, // rgb(46,139,87)
147  sea_shell = 0xFFF5EE, // rgb(255,245,238)
148  sienna = 0xA0522D, // rgb(160,82,45)
149  silver = 0xC0C0C0, // rgb(192,192,192)
150  sky_blue = 0x87CEEB, // rgb(135,206,235)
151  slate_blue = 0x6A5ACD, // rgb(106,90,205)
152  slate_gray = 0x708090, // rgb(112,128,144)
153  snow = 0xFFFAFA, // rgb(255,250,250)
154  spring_green = 0x00FF7F, // rgb(0,255,127)
155  steel_blue = 0x4682B4, // rgb(70,130,180)
156  tan = 0xD2B48C, // rgb(210,180,140)
157  teal = 0x008080, // rgb(0,128,128)
158  thistle = 0xD8BFD8, // rgb(216,191,216)
159  tomato = 0xFF6347, // rgb(255,99,71)
160  turquoise = 0x40E0D0, // rgb(64,224,208)
161  violet = 0xEE82EE, // rgb(238,130,238)
162  wheat = 0xF5DEB3, // rgb(245,222,179)
163  white = 0xFFFFFF, // rgb(255,255,255)
164  white_smoke = 0xF5F5F5, // rgb(245,245,245)
165  yellow = 0xFFFF00, // rgb(255,255,0)
166  yellow_green = 0x9ACD32 // rgb(154,205,50)
167 }; // enum class color
168 
169 enum class terminal_color : uint8_t {
170  black = 30,
171  red,
172  green,
173  yellow,
174  blue,
175  magenta,
176  cyan,
177  white,
178  bright_black = 90,
179  bright_red,
180  bright_green,
181  bright_yellow,
182  bright_blue,
183  bright_magenta,
184  bright_cyan,
185  bright_white
186 };
187 
188 enum class emphasis : uint8_t {
189  bold = 1,
190  faint = 1 << 1,
191  italic = 1 << 2,
192  underline = 1 << 3,
193  blink = 1 << 4,
194  reverse = 1 << 5,
195  conceal = 1 << 6,
196  strikethrough = 1 << 7,
197 };
198 
199 // rgb is a struct for red, green and blue colors.
200 // Using the name "rgb" makes some editors show the color in a tooltip.
201 struct rgb {
202  FMT_CONSTEXPR rgb() : r(0), g(0), b(0) {}
203  FMT_CONSTEXPR rgb(uint8_t r_, uint8_t g_, uint8_t b_) : r(r_), g(g_), b(b_) {}
204  FMT_CONSTEXPR rgb(uint32_t hex)
205  : r((hex >> 16) & 0xFF), g((hex >> 8) & 0xFF), b(hex & 0xFF) {}
206  FMT_CONSTEXPR rgb(color hex)
207  : r((uint32_t(hex) >> 16) & 0xFF),
208  g((uint32_t(hex) >> 8) & 0xFF),
209  b(uint32_t(hex) & 0xFF) {}
210  uint8_t r;
211  uint8_t g;
212  uint8_t b;
213 };
214 
215 FMT_BEGIN_DETAIL_NAMESPACE
216 
217 // color is a struct of either a rgb color or a terminal color.
218 struct color_type {
219  FMT_CONSTEXPR color_type() noexcept : is_rgb(), value{} {}
220  FMT_CONSTEXPR color_type(color rgb_color) noexcept : is_rgb(true), value{} {
221  value.rgb_color = static_cast<uint32_t>(rgb_color);
222  }
223  FMT_CONSTEXPR color_type(rgb rgb_color) noexcept : is_rgb(true), value{} {
224  value.rgb_color = (static_cast<uint32_t>(rgb_color.r) << 16) |
225  (static_cast<uint32_t>(rgb_color.g) << 8) | rgb_color.b;
226  }
227  FMT_CONSTEXPR color_type(terminal_color term_color) noexcept
228  : is_rgb(), value{} {
229  value.term_color = static_cast<uint8_t>(term_color);
230  }
231  bool is_rgb;
232  union color_union {
233  uint8_t term_color;
234  uint32_t rgb_color;
235  } value;
236 };
237 
238 FMT_END_DETAIL_NAMESPACE
239 
241 class text_style {
242 public:
243  FMT_CONSTEXPR text_style(emphasis em = emphasis()) noexcept
244  : set_foreground_color(), set_background_color(), ems(em) {}
245 
246  FMT_CONSTEXPR text_style &operator|=(const text_style &rhs) {
247  if (!set_foreground_color) {
248  set_foreground_color = rhs.set_foreground_color;
249  foreground_color = rhs.foreground_color;
250  } else if (rhs.set_foreground_color) {
251  if (!foreground_color.is_rgb || !rhs.foreground_color.is_rgb)
252  FMT_THROW(format_error("can't OR a terminal color"));
253  foreground_color.value.rgb_color |= rhs.foreground_color.value.rgb_color;
254  }
255 
256  if (!set_background_color) {
257  set_background_color = rhs.set_background_color;
258  background_color = rhs.background_color;
259  } else if (rhs.set_background_color) {
260  if (!background_color.is_rgb || !rhs.background_color.is_rgb)
261  FMT_THROW(format_error("can't OR a terminal color"));
262  background_color.value.rgb_color |= rhs.background_color.value.rgb_color;
263  }
264 
265  ems = static_cast<emphasis>(static_cast<uint8_t>(ems) |
266  static_cast<uint8_t>(rhs.ems));
267  return *this;
268  }
269 
270  friend FMT_CONSTEXPR text_style operator|(text_style lhs,
271  const text_style &rhs) {
272  return lhs |= rhs;
273  }
274 
275  FMT_CONSTEXPR bool has_foreground() const noexcept {
276  return set_foreground_color;
277  }
278  FMT_CONSTEXPR bool has_background() const noexcept {
279  return set_background_color;
280  }
281  FMT_CONSTEXPR bool has_emphasis() const noexcept {
282  return static_cast<uint8_t>(ems) != 0;
283  }
284  FMT_CONSTEXPR detail::color_type get_foreground() const noexcept {
285  FMT_ASSERT(has_foreground(), "no foreground specified for this style");
286  return foreground_color;
287  }
288  FMT_CONSTEXPR detail::color_type get_background() const noexcept {
289  FMT_ASSERT(has_background(), "no background specified for this style");
290  return background_color;
291  }
292  FMT_CONSTEXPR emphasis get_emphasis() const noexcept {
293  FMT_ASSERT(has_emphasis(), "no emphasis specified for this style");
294  return ems;
295  }
296 
297 private:
298  FMT_CONSTEXPR text_style(bool is_foreground,
299  detail::color_type text_color) noexcept
300  : set_foreground_color(), set_background_color(), ems() {
301  if (is_foreground) {
302  foreground_color = text_color;
303  set_foreground_color = true;
304  } else {
305  background_color = text_color;
306  set_background_color = true;
307  }
308  }
309 
310  friend FMT_CONSTEXPR text_style fg(detail::color_type foreground) noexcept;
311 
312  friend FMT_CONSTEXPR text_style bg(detail::color_type background) noexcept;
313 
314  detail::color_type foreground_color;
315  detail::color_type background_color;
316  bool set_foreground_color;
317  bool set_background_color;
318  emphasis ems;
319 };
320 
322 FMT_CONSTEXPR inline text_style fg(detail::color_type foreground) noexcept {
323  return text_style(true, foreground);
324 }
325 
327 FMT_CONSTEXPR inline text_style bg(detail::color_type background) noexcept {
328  return text_style(false, background);
329 }
330 
331 FMT_CONSTEXPR inline text_style operator|(emphasis lhs, emphasis rhs) noexcept {
332  return text_style(lhs) | rhs;
333 }
334 
335 FMT_BEGIN_DETAIL_NAMESPACE
336 
337 template <typename Char> struct ansi_color_escape {
338  FMT_CONSTEXPR ansi_color_escape(detail::color_type text_color,
339  const char *esc) noexcept {
340  // If we have a terminal color, we need to output another escape code
341  // sequence.
342  if (!text_color.is_rgb) {
343  bool is_background = esc == string_view("\x1b[48;2;");
344  uint32_t value = text_color.value.term_color;
345  // Background ASCII codes are the same as the foreground ones but with
346  // 10 more.
347  if (is_background)
348  value += 10u;
349 
350  size_t index = 0;
351  buffer[index++] = static_cast<Char>('\x1b');
352  buffer[index++] = static_cast<Char>('[');
353 
354  if (value >= 100u) {
355  buffer[index++] = static_cast<Char>('1');
356  value %= 100u;
357  }
358  buffer[index++] = static_cast<Char>('0' + value / 10u);
359  buffer[index++] = static_cast<Char>('0' + value % 10u);
360 
361  buffer[index++] = static_cast<Char>('m');
362  buffer[index++] = static_cast<Char>('\0');
363  return;
364  }
365 
366  for (int i = 0; i < 7; i++) {
367  buffer[i] = static_cast<Char>(esc[i]);
368  }
369  rgb color(text_color.value.rgb_color);
370  to_esc(color.r, buffer + 7, ';');
371  to_esc(color.g, buffer + 11, ';');
372  to_esc(color.b, buffer + 15, 'm');
373  buffer[19] = static_cast<Char>(0);
374  }
375  FMT_CONSTEXPR ansi_color_escape(emphasis em) noexcept {
376  uint8_t em_codes[num_emphases] = {};
377  if (has_emphasis(em, emphasis::bold))
378  em_codes[0] = 1;
379  if (has_emphasis(em, emphasis::faint))
380  em_codes[1] = 2;
381  if (has_emphasis(em, emphasis::italic))
382  em_codes[2] = 3;
383  if (has_emphasis(em, emphasis::underline))
384  em_codes[3] = 4;
385  if (has_emphasis(em, emphasis::blink))
386  em_codes[4] = 5;
387  if (has_emphasis(em, emphasis::reverse))
388  em_codes[5] = 7;
389  if (has_emphasis(em, emphasis::conceal))
390  em_codes[6] = 8;
391  if (has_emphasis(em, emphasis::strikethrough))
392  em_codes[7] = 9;
393 
394  size_t index = 0;
395  for (size_t i = 0; i < num_emphases; ++i) {
396  if (!em_codes[i])
397  continue;
398  buffer[index++] = static_cast<Char>('\x1b');
399  buffer[index++] = static_cast<Char>('[');
400  buffer[index++] = static_cast<Char>('0' + em_codes[i]);
401  buffer[index++] = static_cast<Char>('m');
402  }
403  buffer[index++] = static_cast<Char>(0);
404  }
405  FMT_CONSTEXPR operator const Char *() const noexcept { return buffer; }
406 
407  FMT_CONSTEXPR const Char *begin() const noexcept { return buffer; }
408  FMT_CONSTEXPR_CHAR_TRAITS const Char *end() const noexcept {
409  return buffer + std::char_traits<Char>::length(buffer);
410  }
411 
412 private:
413  static constexpr size_t num_emphases = 8;
414  Char buffer[7u + 3u * num_emphases + 1u];
415 
416  static FMT_CONSTEXPR void to_esc(uint8_t c, Char *out,
417  char delimiter) noexcept {
418  out[0] = static_cast<Char>('0' + c / 100);
419  out[1] = static_cast<Char>('0' + c / 10 % 10);
420  out[2] = static_cast<Char>('0' + c % 10);
421  out[3] = static_cast<Char>(delimiter);
422  }
423  static FMT_CONSTEXPR bool has_emphasis(emphasis em, emphasis mask) noexcept {
424  return static_cast<uint8_t>(em) & static_cast<uint8_t>(mask);
425  }
426 };
427 
428 template <typename Char>
429 FMT_CONSTEXPR ansi_color_escape<Char>
430 make_foreground_color(detail::color_type foreground) noexcept {
431  return ansi_color_escape<Char>(foreground, "\x1b[38;2;");
432 }
433 
434 template <typename Char>
435 FMT_CONSTEXPR ansi_color_escape<Char>
436 make_background_color(detail::color_type background) noexcept {
437  return ansi_color_escape<Char>(background, "\x1b[48;2;");
438 }
439 
440 template <typename Char>
441 FMT_CONSTEXPR ansi_color_escape<Char> make_emphasis(emphasis em) noexcept {
442  return ansi_color_escape<Char>(em);
443 }
444 
445 template <typename Char> inline void fputs(const Char *chars, FILE *stream) {
446  int result = std::fputs(chars, stream);
447  if (result < 0)
448  FMT_THROW(system_error(errno, FMT_STRING("cannot write to file")));
449 }
450 
451 template <> inline void fputs<wchar_t>(const wchar_t *chars, FILE *stream) {
452  int result = std::fputws(chars, stream);
453  if (result < 0)
454  FMT_THROW(system_error(errno, FMT_STRING("cannot write to file")));
455 }
456 
457 template <typename Char> inline void reset_color(FILE *stream) {
458  fputs("\x1b[0m", stream);
459 }
460 
461 template <> inline void reset_color<wchar_t>(FILE *stream) {
462  fputs(L"\x1b[0m", stream);
463 }
464 
465 template <typename Char> inline void reset_color(buffer<Char> &buffer) {
466  auto reset_color = string_view("\x1b[0m");
467  buffer.append(reset_color.begin(), reset_color.end());
468 }
469 
470 template <typename T> struct styled_arg {
471  const T &value;
472  text_style style;
473 };
474 
475 template <typename Char>
476 void vformat_to(buffer<Char> &buf, const text_style &ts,
477  basic_string_view<Char> format_str,
478  basic_format_args<buffer_context<type_identity_t<Char>>> args) {
479  bool has_style = false;
480  if (ts.has_emphasis()) {
481  has_style = true;
482  auto emphasis = detail::make_emphasis<Char>(ts.get_emphasis());
483  buf.append(emphasis.begin(), emphasis.end());
484  }
485  if (ts.has_foreground()) {
486  has_style = true;
487  auto foreground = detail::make_foreground_color<Char>(ts.get_foreground());
488  buf.append(foreground.begin(), foreground.end());
489  }
490  if (ts.has_background()) {
491  has_style = true;
492  auto background = detail::make_background_color<Char>(ts.get_background());
493  buf.append(background.begin(), background.end());
494  }
495  detail::vformat_to(buf, format_str, args, {});
496  if (has_style)
497  detail::reset_color<Char>(buf);
498 }
499 
500 FMT_END_DETAIL_NAMESPACE
501 
502 template <typename S, typename Char = char_t<S>>
503 void vprint(std::FILE *f, const text_style &ts, const S &format,
504  basic_format_args<buffer_context<type_identity_t<Char>>> args) {
506  detail::vformat_to(buf, ts, detail::to_string_view(format), args);
507  if (detail::is_utf8()) {
508  detail::print(f, basic_string_view<Char>(buf.begin(), buf.size()));
509  } else {
510  buf.push_back(Char(0));
511  detail::fputs(buf.data(), f);
512  }
513 }
514 
526 template <typename S, typename... Args,
527  FMT_ENABLE_IF(detail::is_string<S>::value)>
528 void print(std::FILE *f, const text_style &ts, const S &format_str,
529  const Args &...args) {
530  vprint(f, ts, format_str,
531  fmt::make_format_args<buffer_context<char_t<S>>>(args...));
532 }
533 
545 template <typename S, typename... Args,
546  FMT_ENABLE_IF(detail::is_string<S>::value)>
547 void print(const text_style &ts, const S &format_str, const Args &...args) {
548  return print(stdout, ts, format_str, args...);
549 }
550 
551 template <typename S, typename Char = char_t<S>>
552 inline std::basic_string<Char>
553 vformat(const text_style &ts, const S &format_str,
554  basic_format_args<buffer_context<type_identity_t<Char>>> args) {
556  detail::vformat_to(buf, ts, detail::to_string_view(format_str), args);
557  return fmt::to_string(buf);
558 }
559 
572 template <typename S, typename... Args, typename Char = char_t<S>>
573 inline std::basic_string<Char> format(const text_style &ts, const S &format_str,
574  const Args &...args) {
575  return fmt::vformat(ts, detail::to_string_view(format_str),
576  fmt::make_format_args<buffer_context<Char>>(args...));
577 }
578 
582 template <typename OutputIt, typename Char,
583  FMT_ENABLE_IF(detail::is_output_iterator<OutputIt, Char>::value)>
584 OutputIt
585 vformat_to(OutputIt out, const text_style &ts,
586  basic_string_view<Char> format_str,
587  basic_format_args<buffer_context<type_identity_t<Char>>> args) {
588  auto &&buf = detail::get_buffer<Char>(out);
589  detail::vformat_to(buf, ts, format_str, args);
590  return detail::get_iterator(buf, out);
591 }
592 
605 template <typename OutputIt, typename S, typename... Args,
606  bool enable = detail::is_output_iterator<OutputIt, char_t<S>>::value
607  &&detail::is_string<S>::value>
608 inline auto format_to(OutputIt out, const text_style &ts, const S &format_str,
609  Args &&...args) ->
610  typename std::enable_if<enable, OutputIt>::type {
611  return vformat_to(out, ts, detail::to_string_view(format_str),
612  fmt::make_format_args<buffer_context<char_t<S>>>(args...));
613 }
614 
615 template <typename T, typename Char>
616 struct formatter<detail::styled_arg<T>, Char> : formatter<T, Char> {
617  template <typename FormatContext>
618  auto format(const detail::styled_arg<T> &arg, FormatContext &ctx) const
619  -> decltype(ctx.out()) {
620  const auto &ts = arg.style;
621  const auto &value = arg.value;
622  auto out = ctx.out();
623 
624  bool has_style = false;
625  if (ts.has_emphasis()) {
626  has_style = true;
627  auto emphasis = detail::make_emphasis<Char>(ts.get_emphasis());
628  out = std::copy(emphasis.begin(), emphasis.end(), out);
629  }
630  if (ts.has_foreground()) {
631  has_style = true;
632  auto foreground =
633  detail::make_foreground_color<Char>(ts.get_foreground());
634  out = std::copy(foreground.begin(), foreground.end(), out);
635  }
636  if (ts.has_background()) {
637  has_style = true;
638  auto background =
639  detail::make_background_color<Char>(ts.get_background());
640  out = std::copy(background.begin(), background.end(), out);
641  }
642  out = formatter<T, Char>::format(value, ctx);
643  if (has_style) {
644  auto reset_color = string_view("\x1b[0m");
645  out = std::copy(reset_color.begin(), reset_color.end(), out);
646  }
647  return out;
648  }
649 };
650 
663 template <typename T>
664 FMT_CONSTEXPR auto styled(const T &value, text_style ts)
665  -> detail::styled_arg<remove_cvref_t<T>> {
666  return detail::styled_arg<remove_cvref_t<T>>{value, ts};
667 }
668 
669 FMT_MODULE_EXPORT_END
670 FMT_END_NAMESPACE
671 
672 #endif // FMT_COLOR_H_
Definition: core.h:1975
Definition: format.h:907
Definition: core.h:899
void append(const U *begin, const U *end)
Definition: format.h:1041
Definition: color.h:241
friend FMT_CONSTEXPR text_style fg(detail::color_type foreground) noexcept
Definition: color.h:322
friend FMT_CONSTEXPR text_style bg(detail::color_type background) noexcept
Definition: color.h:327
double g(RaB r, PrincipalQN n, DiracQN k, Zeff z, AlphaFS a)
Lower (small) radial component.
Definition: DiracHydrogen.cpp:83
constexpr double c
speed of light in a.u. (=1/alpha)
Definition: PhysConst_constants.hpp:17
std::vector< T > reverse(std::vector< T > in)
Reverses a list.
Definition: Vector.hpp:397