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