color.h 23 KB

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