color.h 24 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651
  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_MODULE_EXPORT_BEGIN
  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. FMT_BEGIN_DETAIL_NAMESPACE
  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. FMT_END_DETAIL_NAMESPACE
  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 text_style& operator|=(const text_style& rhs) {
  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. FMT_THROW(format_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. FMT_THROW(format_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 text_style operator|(text_style lhs,
  247. const text_style& rhs) {
  248. return lhs |= rhs;
  249. }
  250. FMT_CONSTEXPR bool has_foreground() const noexcept {
  251. return set_foreground_color;
  252. }
  253. FMT_CONSTEXPR bool has_background() const noexcept {
  254. return set_background_color;
  255. }
  256. FMT_CONSTEXPR bool has_emphasis() const noexcept {
  257. return static_cast<uint8_t>(ems) != 0;
  258. }
  259. FMT_CONSTEXPR detail::color_type get_foreground() const noexcept {
  260. FMT_ASSERT(has_foreground(), "no foreground specified for this style");
  261. return foreground_color;
  262. }
  263. FMT_CONSTEXPR detail::color_type get_background() const noexcept {
  264. FMT_ASSERT(has_background(), "no background specified for this style");
  265. return background_color;
  266. }
  267. FMT_CONSTEXPR emphasis get_emphasis() const noexcept {
  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 text_style fg(detail::color_type foreground) noexcept;
  284. friend FMT_CONSTEXPR text_style bg(detail::color_type background) noexcept;
  285. detail::color_type foreground_color;
  286. detail::color_type background_color;
  287. bool set_foreground_color;
  288. bool set_background_color;
  289. emphasis ems;
  290. };
  291. /** Creates a text style from the foreground (text) color. */
  292. FMT_CONSTEXPR inline text_style fg(detail::color_type foreground) noexcept {
  293. return text_style(true, foreground);
  294. }
  295. /** Creates a text style from the background color. */
  296. FMT_CONSTEXPR inline text_style bg(detail::color_type background) noexcept {
  297. return text_style(false, background);
  298. }
  299. FMT_CONSTEXPR inline text_style operator|(emphasis lhs, emphasis rhs) noexcept {
  300. return text_style(lhs) | rhs;
  301. }
  302. FMT_BEGIN_DETAIL_NAMESPACE
  303. template <typename Char> struct ansi_color_escape {
  304. FMT_CONSTEXPR ansi_color_escape(detail::color_type text_color,
  305. const char* esc) noexcept {
  306. // If we have a terminal color, we need to output another escape code
  307. // sequence.
  308. if (!text_color.is_rgb) {
  309. bool is_background = esc == string_view("\x1b[48;2;");
  310. uint32_t value = text_color.value.term_color;
  311. // Background ASCII codes are the same as the foreground ones but with
  312. // 10 more.
  313. if (is_background) value += 10u;
  314. size_t index = 0;
  315. buffer[index++] = static_cast<Char>('\x1b');
  316. buffer[index++] = static_cast<Char>('[');
  317. if (value >= 100u) {
  318. buffer[index++] = static_cast<Char>('1');
  319. value %= 100u;
  320. }
  321. buffer[index++] = static_cast<Char>('0' + value / 10u);
  322. buffer[index++] = static_cast<Char>('0' + value % 10u);
  323. buffer[index++] = static_cast<Char>('m');
  324. buffer[index++] = static_cast<Char>('\0');
  325. return;
  326. }
  327. for (int i = 0; i < 7; i++) {
  328. buffer[i] = static_cast<Char>(esc[i]);
  329. }
  330. rgb color(text_color.value.rgb_color);
  331. to_esc(color.r, buffer + 7, ';');
  332. to_esc(color.g, buffer + 11, ';');
  333. to_esc(color.b, buffer + 15, 'm');
  334. buffer[19] = static_cast<Char>(0);
  335. }
  336. FMT_CONSTEXPR ansi_color_escape(emphasis em) noexcept {
  337. uint8_t em_codes[num_emphases] = {};
  338. if (has_emphasis(em, emphasis::bold)) em_codes[0] = 1;
  339. if (has_emphasis(em, emphasis::faint)) em_codes[1] = 2;
  340. if (has_emphasis(em, emphasis::italic)) em_codes[2] = 3;
  341. if (has_emphasis(em, emphasis::underline)) em_codes[3] = 4;
  342. if (has_emphasis(em, emphasis::blink)) em_codes[4] = 5;
  343. if (has_emphasis(em, emphasis::reverse)) em_codes[5] = 7;
  344. if (has_emphasis(em, emphasis::conceal)) em_codes[6] = 8;
  345. if (has_emphasis(em, emphasis::strikethrough)) em_codes[7] = 9;
  346. size_t index = 0;
  347. for (size_t i = 0; i < num_emphases; ++i) {
  348. if (!em_codes[i]) continue;
  349. buffer[index++] = static_cast<Char>('\x1b');
  350. buffer[index++] = static_cast<Char>('[');
  351. buffer[index++] = static_cast<Char>('0' + em_codes[i]);
  352. buffer[index++] = static_cast<Char>('m');
  353. }
  354. buffer[index++] = static_cast<Char>(0);
  355. }
  356. FMT_CONSTEXPR operator const Char*() const noexcept { return buffer; }
  357. FMT_CONSTEXPR const Char* begin() const noexcept { return buffer; }
  358. FMT_CONSTEXPR_CHAR_TRAITS const Char* end() const noexcept {
  359. return buffer + std::char_traits<Char>::length(buffer);
  360. }
  361. private:
  362. static constexpr size_t num_emphases = 8;
  363. Char buffer[7u + 3u * num_emphases + 1u];
  364. static FMT_CONSTEXPR void to_esc(uint8_t c, Char* out,
  365. char delimiter) noexcept {
  366. out[0] = static_cast<Char>('0' + c / 100);
  367. out[1] = static_cast<Char>('0' + c / 10 % 10);
  368. out[2] = static_cast<Char>('0' + c % 10);
  369. out[3] = static_cast<Char>(delimiter);
  370. }
  371. static FMT_CONSTEXPR bool has_emphasis(emphasis em, emphasis mask) noexcept {
  372. return static_cast<uint8_t>(em) & static_cast<uint8_t>(mask);
  373. }
  374. };
  375. template <typename Char>
  376. FMT_CONSTEXPR ansi_color_escape<Char> make_foreground_color(
  377. detail::color_type foreground) noexcept {
  378. return ansi_color_escape<Char>(foreground, "\x1b[38;2;");
  379. }
  380. template <typename Char>
  381. FMT_CONSTEXPR ansi_color_escape<Char> make_background_color(
  382. detail::color_type background) noexcept {
  383. return ansi_color_escape<Char>(background, "\x1b[48;2;");
  384. }
  385. template <typename Char>
  386. FMT_CONSTEXPR ansi_color_escape<Char> make_emphasis(emphasis em) noexcept {
  387. return ansi_color_escape<Char>(em);
  388. }
  389. template <typename Char> inline void fputs(const Char* chars, FILE* stream) {
  390. int result = std::fputs(chars, stream);
  391. if (result < 0)
  392. FMT_THROW(system_error(errno, FMT_STRING("cannot write to file")));
  393. }
  394. template <> inline void fputs<wchar_t>(const wchar_t* chars, FILE* stream) {
  395. int result = std::fputws(chars, stream);
  396. if (result < 0)
  397. FMT_THROW(system_error(errno, FMT_STRING("cannot write to file")));
  398. }
  399. template <typename Char> inline void reset_color(FILE* stream) {
  400. fputs("\x1b[0m", stream);
  401. }
  402. template <> inline void reset_color<wchar_t>(FILE* stream) {
  403. fputs(L"\x1b[0m", stream);
  404. }
  405. template <typename Char> inline void reset_color(buffer<Char>& buffer) {
  406. auto reset_color = string_view("\x1b[0m");
  407. buffer.append(reset_color.begin(), reset_color.end());
  408. }
  409. template <typename T> struct styled_arg {
  410. const T& value;
  411. text_style style;
  412. };
  413. template <typename Char>
  414. void vformat_to(buffer<Char>& buf, const text_style& ts,
  415. basic_string_view<Char> format_str,
  416. basic_format_args<buffer_context<type_identity_t<Char>>> args) {
  417. bool has_style = false;
  418. if (ts.has_emphasis()) {
  419. has_style = true;
  420. auto emphasis = detail::make_emphasis<Char>(ts.get_emphasis());
  421. buf.append(emphasis.begin(), emphasis.end());
  422. }
  423. if (ts.has_foreground()) {
  424. has_style = true;
  425. auto foreground = detail::make_foreground_color<Char>(ts.get_foreground());
  426. buf.append(foreground.begin(), foreground.end());
  427. }
  428. if (ts.has_background()) {
  429. has_style = true;
  430. auto background = detail::make_background_color<Char>(ts.get_background());
  431. buf.append(background.begin(), background.end());
  432. }
  433. detail::vformat_to(buf, format_str, args, {});
  434. if (has_style) detail::reset_color<Char>(buf);
  435. }
  436. FMT_END_DETAIL_NAMESPACE
  437. template <typename S, typename Char = char_t<S>>
  438. void vprint(std::FILE* f, const text_style& ts, const S& format,
  439. basic_format_args<buffer_context<type_identity_t<Char>>> args) {
  440. basic_memory_buffer<Char> buf;
  441. detail::vformat_to(buf, ts, detail::to_string_view(format), args);
  442. if (detail::is_utf8()) {
  443. detail::print(f, basic_string_view<Char>(buf.begin(), buf.size()));
  444. } else {
  445. buf.push_back(Char(0));
  446. detail::fputs(buf.data(), f);
  447. }
  448. }
  449. /**
  450. \rst
  451. Formats a string and prints it to the specified file stream using ANSI
  452. escape sequences to specify text formatting.
  453. **Example**::
  454. fmt::print(fmt::emphasis::bold | fg(fmt::color::red),
  455. "Elapsed time: {0:.2f} seconds", 1.23);
  456. \endrst
  457. */
  458. template <typename S, typename... Args,
  459. FMT_ENABLE_IF(detail::is_string<S>::value)>
  460. void print(std::FILE* f, const text_style& ts, const S& format_str,
  461. const Args&... args) {
  462. vprint(f, ts, format_str,
  463. fmt::make_format_args<buffer_context<char_t<S>>>(args...));
  464. }
  465. /**
  466. \rst
  467. Formats a string and prints it to stdout using ANSI escape sequences to
  468. specify text formatting.
  469. **Example**::
  470. fmt::print(fmt::emphasis::bold | fg(fmt::color::red),
  471. "Elapsed time: {0:.2f} seconds", 1.23);
  472. \endrst
  473. */
  474. template <typename S, typename... Args,
  475. FMT_ENABLE_IF(detail::is_string<S>::value)>
  476. void print(const text_style& ts, const S& format_str, const Args&... args) {
  477. return print(stdout, ts, format_str, args...);
  478. }
  479. template <typename S, typename Char = char_t<S>>
  480. inline std::basic_string<Char> vformat(
  481. const text_style& ts, const S& format_str,
  482. basic_format_args<buffer_context<type_identity_t<Char>>> args) {
  483. basic_memory_buffer<Char> buf;
  484. detail::vformat_to(buf, ts, detail::to_string_view(format_str), args);
  485. return fmt::to_string(buf);
  486. }
  487. /**
  488. \rst
  489. Formats arguments and returns the result as a string using ANSI
  490. escape sequences to specify text formatting.
  491. **Example**::
  492. #include <fmt/color.h>
  493. std::string message = fmt::format(fmt::emphasis::bold | fg(fmt::color::red),
  494. "The answer is {}", 42);
  495. \endrst
  496. */
  497. template <typename S, typename... Args, typename Char = char_t<S>>
  498. inline std::basic_string<Char> format(const text_style& ts, const S& format_str,
  499. const Args&... args) {
  500. return fmt::vformat(ts, detail::to_string_view(format_str),
  501. fmt::make_format_args<buffer_context<Char>>(args...));
  502. }
  503. /**
  504. Formats a string with the given text_style and writes the output to ``out``.
  505. */
  506. template <typename OutputIt, typename Char,
  507. FMT_ENABLE_IF(detail::is_output_iterator<OutputIt, Char>::value)>
  508. OutputIt vformat_to(
  509. OutputIt out, const text_style& ts, basic_string_view<Char> format_str,
  510. basic_format_args<buffer_context<type_identity_t<Char>>> args) {
  511. auto&& buf = detail::get_buffer<Char>(out);
  512. detail::vformat_to(buf, ts, format_str, args);
  513. return detail::get_iterator(buf);
  514. }
  515. /**
  516. \rst
  517. Formats arguments with the given text_style, writes the result to the output
  518. iterator ``out`` and returns the iterator past the end of the output range.
  519. **Example**::
  520. std::vector<char> out;
  521. fmt::format_to(std::back_inserter(out),
  522. fmt::emphasis::bold | fg(fmt::color::red), "{}", 42);
  523. \endrst
  524. */
  525. template <typename OutputIt, typename S, typename... Args,
  526. bool enable = detail::is_output_iterator<OutputIt, char_t<S>>::value&&
  527. detail::is_string<S>::value>
  528. inline auto format_to(OutputIt out, const text_style& ts, const S& format_str,
  529. Args&&... args) ->
  530. typename std::enable_if<enable, OutputIt>::type {
  531. return vformat_to(out, ts, detail::to_string_view(format_str),
  532. fmt::make_format_args<buffer_context<char_t<S>>>(args...));
  533. }
  534. template <typename T, typename Char>
  535. struct formatter<detail::styled_arg<T>, Char> : formatter<T, Char> {
  536. template <typename FormatContext>
  537. auto format(const detail::styled_arg<T>& arg, FormatContext& ctx) const
  538. -> decltype(ctx.out()) {
  539. const auto& ts = arg.style;
  540. const auto& value = arg.value;
  541. auto out = ctx.out();
  542. bool has_style = false;
  543. if (ts.has_emphasis()) {
  544. has_style = true;
  545. auto emphasis = detail::make_emphasis<Char>(ts.get_emphasis());
  546. out = std::copy(emphasis.begin(), emphasis.end(), out);
  547. }
  548. if (ts.has_foreground()) {
  549. has_style = true;
  550. auto foreground =
  551. detail::make_foreground_color<Char>(ts.get_foreground());
  552. out = std::copy(foreground.begin(), foreground.end(), out);
  553. }
  554. if (ts.has_background()) {
  555. has_style = true;
  556. auto background =
  557. detail::make_background_color<Char>(ts.get_background());
  558. out = std::copy(background.begin(), background.end(), out);
  559. }
  560. out = formatter<T, Char>::format(value, ctx);
  561. if (has_style) {
  562. auto reset_color = string_view("\x1b[0m");
  563. out = std::copy(reset_color.begin(), reset_color.end(), out);
  564. }
  565. return out;
  566. }
  567. };
  568. /**
  569. \rst
  570. Returns an argument that will be formatted using ANSI escape sequences,
  571. to be used in a formatting function.
  572. **Example**::
  573. fmt::print("Elapsed time: {0:.2f} seconds",
  574. fmt::styled(1.23, fmt::fg(fmt::color::green) |
  575. fmt::bg(fmt::color::blue)));
  576. \endrst
  577. */
  578. template <typename T>
  579. FMT_CONSTEXPR auto styled(const T& value, text_style ts)
  580. -> detail::styled_arg<remove_cvref_t<T>> {
  581. return detail::styled_arg<remove_cvref_t<T>>{value, ts};
  582. }
  583. FMT_MODULE_EXPORT_END
  584. FMT_END_NAMESPACE
  585. #endif // FMT_COLOR_H_