1"""Tools for helping with ANSI color codes.""" 2import sys 3import string 4import warnings 5import builtins 6 7from xonsh.platform import HAS_PYGMENTS 8from xonsh.lazyasd import LazyDict 9from xonsh.color_tools import ( 10 RE_BACKGROUND, 11 BASE_XONSH_COLORS, 12 make_palette, 13 find_closest_color, 14 rgb2short, 15 rgb_to_256, 16) 17 18 19def ansi_partial_color_format(template, style="default", cmap=None, hide=False): 20 """Formats a template string but only with respect to the colors. 21 Another template string is returned, with the color values filled in. 22 23 Parameters 24 ---------- 25 template : str 26 The template string, potentially with color names. 27 style : str, optional 28 Style name to look up color map from. 29 cmap : dict, optional 30 A color map to use, this will prevent the color map from being 31 looked up via the style name. 32 hide : bool, optional 33 Whether to wrap the color codes in the \\001 and \\002 escape 34 codes, so that the color codes are not counted against line 35 length. 36 37 Returns 38 ------- 39 A template string with the color values filled in. 40 """ 41 try: 42 return _ansi_partial_color_format_main( 43 template, style=style, cmap=cmap, hide=hide 44 ) 45 except Exception: 46 return template 47 48 49def _ansi_partial_color_format_main(template, style="default", cmap=None, hide=False): 50 if cmap is not None: 51 pass 52 elif style in ANSI_STYLES: 53 cmap = ANSI_STYLES[style] 54 else: 55 try: # dynamically loading the style 56 cmap = ansi_style_by_name(style) 57 except Exception: 58 msg = "Could not find color style {0!r}, using default." 59 print(msg.format(style), file=sys.stderr) 60 builtins.__xonsh_env__["XONSH_COLOR_STYLE"] = "default" 61 cmap = ANSI_STYLES["default"] 62 formatter = string.Formatter() 63 esc = ("\001" if hide else "") + "\033[" 64 m = "m" + ("\002" if hide else "") 65 bopen = "{" 66 bclose = "}" 67 colon = ":" 68 expl = "!" 69 toks = [] 70 for literal, field, spec, conv in formatter.parse(template): 71 toks.append(literal) 72 if field is None: 73 pass 74 elif field in cmap: 75 toks.extend([esc, cmap[field], m]) 76 elif "#" in field: 77 field = field.lower() 78 pre, _, post = field.partition("#") 79 f_or_b = "38" if RE_BACKGROUND.search(pre) is None else "48" 80 rgb, _, post = post.partition("_") 81 c256, _ = rgb_to_256(rgb) 82 color = f_or_b + ";5;" + c256 83 mods = pre + "_" + post 84 if "underline" in mods: 85 color = "4;" + color 86 if "bold" in mods: 87 color = "1;" + color 88 toks.extend([esc, color, m]) 89 elif field is not None: 90 toks.append(bopen) 91 toks.append(field) 92 if conv is not None and len(conv) > 0: 93 toks.append(expl) 94 toks.append(conv) 95 if spec is not None and len(spec) > 0: 96 toks.append(colon) 97 toks.append(spec) 98 toks.append(bclose) 99 return "".join(toks) 100 101 102def ansi_color_style_names(): 103 """Returns an iterable of all ANSI color style names.""" 104 return ANSI_STYLES.keys() 105 106 107def ansi_color_style(style="default"): 108 """Returns the current color map.""" 109 if style in ANSI_STYLES: 110 cmap = ANSI_STYLES[style] 111 else: 112 msg = "Could not find color style {0!r}, using default.".format(style) 113 warnings.warn(msg, RuntimeWarning) 114 cmap = ANSI_STYLES["default"] 115 return cmap 116 117 118def _ansi_expand_style(cmap): 119 """Expands a style in order to more quickly make color map changes.""" 120 for key, val in list(cmap.items()): 121 if key == "NO_COLOR": 122 continue 123 elif len(val) == 0: 124 cmap["BOLD_" + key] = "1" 125 cmap["UNDERLINE_" + key] = "4" 126 cmap["BOLD_UNDERLINE_" + key] = "1;4" 127 cmap["BACKGROUND_" + key] = val 128 else: 129 cmap["BOLD_" + key] = "1;" + val 130 cmap["UNDERLINE_" + key] = "4;" + val 131 cmap["BOLD_UNDERLINE_" + key] = "1;4;" + val 132 cmap["BACKGROUND_" + key] = val.replace("38", "48", 1) 133 134 135def _bw_style(): 136 style = { 137 "BLACK": "", 138 "BLUE": "", 139 "CYAN": "", 140 "GREEN": "", 141 "INTENSE_BLACK": "", 142 "INTENSE_BLUE": "", 143 "INTENSE_CYAN": "", 144 "INTENSE_GREEN": "", 145 "INTENSE_PURPLE": "", 146 "INTENSE_RED": "", 147 "INTENSE_WHITE": "", 148 "INTENSE_YELLOW": "", 149 "NO_COLOR": "0", 150 "PURPLE": "", 151 "RED": "", 152 "WHITE": "", 153 "YELLOW": "", 154 } 155 _ansi_expand_style(style) 156 return style 157 158 159def _default_style(): 160 style = { 161 # Reset 162 "NO_COLOR": "0", # Text Reset 163 # Regular Colors 164 "BLACK": "0;30", # BLACK 165 "RED": "0;31", # RED 166 "GREEN": "0;32", # GREEN 167 "YELLOW": "0;33", # YELLOW 168 "BLUE": "0;34", # BLUE 169 "PURPLE": "0;35", # PURPLE 170 "CYAN": "0;36", # CYAN 171 "WHITE": "0;37", # WHITE 172 # Bold 173 "BOLD_BLACK": "1;30", # BLACK 174 "BOLD_RED": "1;31", # RED 175 "BOLD_GREEN": "1;32", # GREEN 176 "BOLD_YELLOW": "1;33", # YELLOW 177 "BOLD_BLUE": "1;34", # BLUE 178 "BOLD_PURPLE": "1;35", # PURPLE 179 "BOLD_CYAN": "1;36", # CYAN 180 "BOLD_WHITE": "1;37", # WHITE 181 # Underline 182 "UNDERLINE_BLACK": "4;30", # BLACK 183 "UNDERLINE_RED": "4;31", # RED 184 "UNDERLINE_GREEN": "4;32", # GREEN 185 "UNDERLINE_YELLOW": "4;33", # YELLOW 186 "UNDERLINE_BLUE": "4;34", # BLUE 187 "UNDERLINE_PURPLE": "4;35", # PURPLE 188 "UNDERLINE_CYAN": "4;36", # CYAN 189 "UNDERLINE_WHITE": "4;37", # WHITE 190 # Bold, Underline 191 "BOLD_UNDERLINE_BLACK": "1;4;30", # BLACK 192 "BOLD_UNDERLINE_RED": "1;4;31", # RED 193 "BOLD_UNDERLINE_GREEN": "1;4;32", # GREEN 194 "BOLD_UNDERLINE_YELLOW": "1;4;33", # YELLOW 195 "BOLD_UNDERLINE_BLUE": "1;4;34", # BLUE 196 "BOLD_UNDERLINE_PURPLE": "1;4;35", # PURPLE 197 "BOLD_UNDERLINE_CYAN": "1;4;36", # CYAN 198 "BOLD_UNDERLINE_WHITE": "1;4;37", # WHITE 199 # Background 200 "BACKGROUND_BLACK": "40", # BLACK 201 "BACKGROUND_RED": "41", # RED 202 "BACKGROUND_GREEN": "42", # GREEN 203 "BACKGROUND_YELLOW": "43", # YELLOW 204 "BACKGROUND_BLUE": "44", # BLUE 205 "BACKGROUND_PURPLE": "45", # PURPLE 206 "BACKGROUND_CYAN": "46", # CYAN 207 "BACKGROUND_WHITE": "47", # WHITE 208 # High Intensity 209 "INTENSE_BLACK": "0;90", # BLACK 210 "INTENSE_RED": "0;91", # RED 211 "INTENSE_GREEN": "0;92", # GREEN 212 "INTENSE_YELLOW": "0;93", # YELLOW 213 "INTENSE_BLUE": "0;94", # BLUE 214 "INTENSE_PURPLE": "0;95", # PURPLE 215 "INTENSE_CYAN": "0;96", # CYAN 216 "INTENSE_WHITE": "0;97", # WHITE 217 # Bold High Intensity 218 "BOLD_INTENSE_BLACK": "1;90", # BLACK 219 "BOLD_INTENSE_RED": "1;91", # RED 220 "BOLD_INTENSE_GREEN": "1;92", # GREEN 221 "BOLD_INTENSE_YELLOW": "1;93", # YELLOW 222 "BOLD_INTENSE_BLUE": "1;94", # BLUE 223 "BOLD_INTENSE_PURPLE": "1;95", # PURPLE 224 "BOLD_INTENSE_CYAN": "1;96", # CYAN 225 "BOLD_INTENSE_WHITE": "1;97", # WHITE 226 # Underline High Intensity 227 "UNDERLINE_INTENSE_BLACK": "4;90", # BLACK 228 "UNDERLINE_INTENSE_RED": "4;91", # RED 229 "UNDERLINE_INTENSE_GREEN": "4;92", # GREEN 230 "UNDERLINE_INTENSE_YELLOW": "4;93", # YELLOW 231 "UNDERLINE_INTENSE_BLUE": "4;94", # BLUE 232 "UNDERLINE_INTENSE_PURPLE": "4;95", # PURPLE 233 "UNDERLINE_INTENSE_CYAN": "4;96", # CYAN 234 "UNDERLINE_INTENSE_WHITE": "4;97", # WHITE 235 # Bold Underline High Intensity 236 "BOLD_UNDERLINE_INTENSE_BLACK": "1;4;90", # BLACK 237 "BOLD_UNDERLINE_INTENSE_RED": "1;4;91", # RED 238 "BOLD_UNDERLINE_INTENSE_GREEN": "1;4;92", # GREEN 239 "BOLD_UNDERLINE_INTENSE_YELLOW": "1;4;93", # YELLOW 240 "BOLD_UNDERLINE_INTENSE_BLUE": "1;4;94", # BLUE 241 "BOLD_UNDERLINE_INTENSE_PURPLE": "1;4;95", # PURPLE 242 "BOLD_UNDERLINE_INTENSE_CYAN": "1;4;96", # CYAN 243 "BOLD_UNDERLINE_INTENSE_WHITE": "1;4;97", # WHITE 244 # High Intensity backgrounds 245 "BACKGROUND_INTENSE_BLACK": "0;100", # BLACK 246 "BACKGROUND_INTENSE_RED": "0;101", # RED 247 "BACKGROUND_INTENSE_GREEN": "0;102", # GREEN 248 "BACKGROUND_INTENSE_YELLOW": "0;103", # YELLOW 249 "BACKGROUND_INTENSE_BLUE": "0;104", # BLUE 250 "BACKGROUND_INTENSE_PURPLE": "0;105", # PURPLE 251 "BACKGROUND_INTENSE_CYAN": "0;106", # CYAN 252 "BACKGROUND_INTENSE_WHITE": "0;107", # WHITE 253 } 254 return style 255 256 257def _monokai_style(): 258 style = { 259 "NO_COLOR": "0", 260 "BLACK": "38;5;16", 261 "BLUE": "38;5;63", 262 "CYAN": "38;5;81", 263 "GREEN": "38;5;40", 264 "PURPLE": "38;5;89", 265 "RED": "38;5;124", 266 "WHITE": "38;5;188", 267 "YELLOW": "38;5;184", 268 "INTENSE_BLACK": "38;5;59", 269 "INTENSE_BLUE": "38;5;20", 270 "INTENSE_CYAN": "38;5;44", 271 "INTENSE_GREEN": "38;5;148", 272 "INTENSE_PURPLE": "38;5;141", 273 "INTENSE_RED": "38;5;197", 274 "INTENSE_WHITE": "38;5;15", 275 "INTENSE_YELLOW": "38;5;186", 276 } 277 _ansi_expand_style(style) 278 return style 279 280 281#################################### 282# Auto-generated below this line # 283#################################### 284 285 286def _algol_style(): 287 style = { 288 "BLACK": "38;5;59", 289 "BLUE": "38;5;59", 290 "CYAN": "38;5;59", 291 "GREEN": "38;5;59", 292 "INTENSE_BLACK": "38;5;59", 293 "INTENSE_BLUE": "38;5;102", 294 "INTENSE_CYAN": "38;5;102", 295 "INTENSE_GREEN": "38;5;102", 296 "INTENSE_PURPLE": "38;5;102", 297 "INTENSE_RED": "38;5;09", 298 "INTENSE_WHITE": "38;5;102", 299 "INTENSE_YELLOW": "38;5;102", 300 "NO_COLOR": "0", 301 "PURPLE": "38;5;59", 302 "RED": "38;5;09", 303 "WHITE": "38;5;102", 304 "YELLOW": "38;5;09", 305 } 306 _ansi_expand_style(style) 307 return style 308 309 310def _algol_nu_style(): 311 style = { 312 "BLACK": "38;5;59", 313 "BLUE": "38;5;59", 314 "CYAN": "38;5;59", 315 "GREEN": "38;5;59", 316 "INTENSE_BLACK": "38;5;59", 317 "INTENSE_BLUE": "38;5;102", 318 "INTENSE_CYAN": "38;5;102", 319 "INTENSE_GREEN": "38;5;102", 320 "INTENSE_PURPLE": "38;5;102", 321 "INTENSE_RED": "38;5;09", 322 "INTENSE_WHITE": "38;5;102", 323 "INTENSE_YELLOW": "38;5;102", 324 "NO_COLOR": "0", 325 "PURPLE": "38;5;59", 326 "RED": "38;5;09", 327 "WHITE": "38;5;102", 328 "YELLOW": "38;5;09", 329 } 330 _ansi_expand_style(style) 331 return style 332 333 334def _autumn_style(): 335 style = { 336 "BLACK": "38;5;18", 337 "BLUE": "38;5;19", 338 "CYAN": "38;5;37", 339 "GREEN": "38;5;34", 340 "INTENSE_BLACK": "38;5;59", 341 "INTENSE_BLUE": "38;5;33", 342 "INTENSE_CYAN": "38;5;33", 343 "INTENSE_GREEN": "38;5;64", 344 "INTENSE_PURPLE": "38;5;217", 345 "INTENSE_RED": "38;5;130", 346 "INTENSE_WHITE": "38;5;145", 347 "INTENSE_YELLOW": "38;5;217", 348 "NO_COLOR": "0", 349 "PURPLE": "38;5;90", 350 "RED": "38;5;124", 351 "WHITE": "38;5;145", 352 "YELLOW": "38;5;130", 353 } 354 _ansi_expand_style(style) 355 return style 356 357 358def _borland_style(): 359 style = { 360 "BLACK": "38;5;16", 361 "BLUE": "38;5;18", 362 "CYAN": "38;5;30", 363 "GREEN": "38;5;28", 364 "INTENSE_BLACK": "38;5;59", 365 "INTENSE_BLUE": "38;5;21", 366 "INTENSE_CYAN": "38;5;194", 367 "INTENSE_GREEN": "38;5;102", 368 "INTENSE_PURPLE": "38;5;188", 369 "INTENSE_RED": "38;5;09", 370 "INTENSE_WHITE": "38;5;224", 371 "INTENSE_YELLOW": "38;5;188", 372 "NO_COLOR": "0", 373 "PURPLE": "38;5;90", 374 "RED": "38;5;124", 375 "WHITE": "38;5;145", 376 "YELLOW": "38;5;124", 377 } 378 _ansi_expand_style(style) 379 return style 380 381 382def _colorful_style(): 383 style = { 384 "BLACK": "38;5;16", 385 "BLUE": "38;5;20", 386 "CYAN": "38;5;31", 387 "GREEN": "38;5;34", 388 "INTENSE_BLACK": "38;5;59", 389 "INTENSE_BLUE": "38;5;61", 390 "INTENSE_CYAN": "38;5;145", 391 "INTENSE_GREEN": "38;5;102", 392 "INTENSE_PURPLE": "38;5;217", 393 "INTENSE_RED": "38;5;166", 394 "INTENSE_WHITE": "38;5;15", 395 "INTENSE_YELLOW": "38;5;217", 396 "NO_COLOR": "0", 397 "PURPLE": "38;5;90", 398 "RED": "38;5;124", 399 "WHITE": "38;5;145", 400 "YELLOW": "38;5;130", 401 } 402 _ansi_expand_style(style) 403 return style 404 405 406def _emacs_style(): 407 style = { 408 "BLACK": "38;5;28", 409 "BLUE": "38;5;18", 410 "CYAN": "38;5;26", 411 "GREEN": "38;5;34", 412 "INTENSE_BLACK": "38;5;59", 413 "INTENSE_BLUE": "38;5;26", 414 "INTENSE_CYAN": "38;5;145", 415 "INTENSE_GREEN": "38;5;34", 416 "INTENSE_PURPLE": "38;5;129", 417 "INTENSE_RED": "38;5;167", 418 "INTENSE_WHITE": "38;5;145", 419 "INTENSE_YELLOW": "38;5;145", 420 "NO_COLOR": "0", 421 "PURPLE": "38;5;90", 422 "RED": "38;5;124", 423 "WHITE": "38;5;145", 424 "YELLOW": "38;5;130", 425 } 426 _ansi_expand_style(style) 427 return style 428 429 430def _friendly_style(): 431 style = { 432 "BLACK": "38;5;22", 433 "BLUE": "38;5;18", 434 "CYAN": "38;5;31", 435 "GREEN": "38;5;34", 436 "INTENSE_BLACK": "38;5;59", 437 "INTENSE_BLUE": "38;5;74", 438 "INTENSE_CYAN": "38;5;74", 439 "INTENSE_GREEN": "38;5;71", 440 "INTENSE_PURPLE": "38;5;134", 441 "INTENSE_RED": "38;5;167", 442 "INTENSE_WHITE": "38;5;15", 443 "INTENSE_YELLOW": "38;5;145", 444 "NO_COLOR": "0", 445 "PURPLE": "38;5;90", 446 "RED": "38;5;124", 447 "WHITE": "38;5;145", 448 "YELLOW": "38;5;166", 449 } 450 _ansi_expand_style(style) 451 return style 452 453 454def _fruity_style(): 455 style = { 456 "BLACK": "38;5;16", 457 "BLUE": "38;5;32", 458 "CYAN": "38;5;32", 459 "GREEN": "38;5;28", 460 "INTENSE_BLACK": "38;5;59", 461 "INTENSE_BLUE": "38;5;33", 462 "INTENSE_CYAN": "38;5;33", 463 "INTENSE_GREEN": "38;5;102", 464 "INTENSE_PURPLE": "38;5;198", 465 "INTENSE_RED": "38;5;202", 466 "INTENSE_WHITE": "38;5;15", 467 "INTENSE_YELLOW": "38;5;187", 468 "NO_COLOR": "0", 469 "PURPLE": "38;5;198", 470 "RED": "38;5;09", 471 "WHITE": "38;5;187", 472 "YELLOW": "38;5;202", 473 } 474 _ansi_expand_style(style) 475 return style 476 477 478def _igor_style(): 479 style = { 480 "BLACK": "38;5;34", 481 "BLUE": "38;5;21", 482 "CYAN": "38;5;30", 483 "GREEN": "38;5;34", 484 "INTENSE_BLACK": "38;5;30", 485 "INTENSE_BLUE": "38;5;21", 486 "INTENSE_CYAN": "38;5;30", 487 "INTENSE_GREEN": "38;5;34", 488 "INTENSE_PURPLE": "38;5;163", 489 "INTENSE_RED": "38;5;166", 490 "INTENSE_WHITE": "38;5;163", 491 "INTENSE_YELLOW": "38;5;166", 492 "NO_COLOR": "0", 493 "PURPLE": "38;5;163", 494 "RED": "38;5;166", 495 "WHITE": "38;5;163", 496 "YELLOW": "38;5;166", 497 } 498 _ansi_expand_style(style) 499 return style 500 501 502def _lovelace_style(): 503 style = { 504 "BLACK": "38;5;59", 505 "BLUE": "38;5;25", 506 "CYAN": "38;5;29", 507 "GREEN": "38;5;65", 508 "INTENSE_BLACK": "38;5;59", 509 "INTENSE_BLUE": "38;5;25", 510 "INTENSE_CYAN": "38;5;102", 511 "INTENSE_GREEN": "38;5;29", 512 "INTENSE_PURPLE": "38;5;133", 513 "INTENSE_RED": "38;5;131", 514 "INTENSE_WHITE": "38;5;102", 515 "INTENSE_YELLOW": "38;5;136", 516 "NO_COLOR": "0", 517 "PURPLE": "38;5;133", 518 "RED": "38;5;124", 519 "WHITE": "38;5;102", 520 "YELLOW": "38;5;130", 521 } 522 _ansi_expand_style(style) 523 return style 524 525 526def _manni_style(): 527 style = { 528 "BLACK": "38;5;16", 529 "BLUE": "38;5;18", 530 "CYAN": "38;5;30", 531 "GREEN": "38;5;40", 532 "INTENSE_BLACK": "38;5;59", 533 "INTENSE_BLUE": "38;5;105", 534 "INTENSE_CYAN": "38;5;45", 535 "INTENSE_GREEN": "38;5;113", 536 "INTENSE_PURPLE": "38;5;165", 537 "INTENSE_RED": "38;5;202", 538 "INTENSE_WHITE": "38;5;224", 539 "INTENSE_YELLOW": "38;5;221", 540 "NO_COLOR": "0", 541 "PURPLE": "38;5;165", 542 "RED": "38;5;124", 543 "WHITE": "38;5;145", 544 "YELLOW": "38;5;166", 545 } 546 _ansi_expand_style(style) 547 return style 548 549 550def _murphy_style(): 551 style = { 552 "BLACK": "38;5;16", 553 "BLUE": "38;5;18", 554 "CYAN": "38;5;31", 555 "GREEN": "38;5;34", 556 "INTENSE_BLACK": "38;5;59", 557 "INTENSE_BLUE": "38;5;63", 558 "INTENSE_CYAN": "38;5;86", 559 "INTENSE_GREEN": "38;5;86", 560 "INTENSE_PURPLE": "38;5;213", 561 "INTENSE_RED": "38;5;209", 562 "INTENSE_WHITE": "38;5;15", 563 "INTENSE_YELLOW": "38;5;222", 564 "NO_COLOR": "0", 565 "PURPLE": "38;5;90", 566 "RED": "38;5;124", 567 "WHITE": "38;5;145", 568 "YELLOW": "38;5;166", 569 } 570 _ansi_expand_style(style) 571 return style 572 573 574def _native_style(): 575 style = { 576 "BLACK": "38;5;52", 577 "BLUE": "38;5;67", 578 "CYAN": "38;5;31", 579 "GREEN": "38;5;64", 580 "INTENSE_BLACK": "38;5;59", 581 "INTENSE_BLUE": "38;5;68", 582 "INTENSE_CYAN": "38;5;87", 583 "INTENSE_GREEN": "38;5;70", 584 "INTENSE_PURPLE": "38;5;188", 585 "INTENSE_RED": "38;5;160", 586 "INTENSE_WHITE": "38;5;15", 587 "INTENSE_YELLOW": "38;5;214", 588 "NO_COLOR": "0", 589 "PURPLE": "38;5;59", 590 "RED": "38;5;124", 591 "WHITE": "38;5;145", 592 "YELLOW": "38;5;124", 593 } 594 _ansi_expand_style(style) 595 return style 596 597 598def _paraiso_dark_style(): 599 style = { 600 "BLACK": "38;5;95", 601 "BLUE": "38;5;97", 602 "CYAN": "38;5;39", 603 "GREEN": "38;5;72", 604 "INTENSE_BLACK": "38;5;95", 605 "INTENSE_BLUE": "38;5;97", 606 "INTENSE_CYAN": "38;5;79", 607 "INTENSE_GREEN": "38;5;72", 608 "INTENSE_PURPLE": "38;5;188", 609 "INTENSE_RED": "38;5;203", 610 "INTENSE_WHITE": "38;5;188", 611 "INTENSE_YELLOW": "38;5;220", 612 "NO_COLOR": "0", 613 "PURPLE": "38;5;97", 614 "RED": "38;5;203", 615 "WHITE": "38;5;79", 616 "YELLOW": "38;5;214", 617 } 618 _ansi_expand_style(style) 619 return style 620 621 622def _paraiso_light_style(): 623 style = { 624 "BLACK": "38;5;16", 625 "BLUE": "38;5;16", 626 "CYAN": "38;5;39", 627 "GREEN": "38;5;72", 628 "INTENSE_BLACK": "38;5;16", 629 "INTENSE_BLUE": "38;5;97", 630 "INTENSE_CYAN": "38;5;79", 631 "INTENSE_GREEN": "38;5;72", 632 "INTENSE_PURPLE": "38;5;97", 633 "INTENSE_RED": "38;5;203", 634 "INTENSE_WHITE": "38;5;79", 635 "INTENSE_YELLOW": "38;5;220", 636 "NO_COLOR": "0", 637 "PURPLE": "38;5;97", 638 "RED": "38;5;16", 639 "WHITE": "38;5;102", 640 "YELLOW": "38;5;214", 641 } 642 _ansi_expand_style(style) 643 return style 644 645 646def _pastie_style(): 647 style = { 648 "BLACK": "38;5;16", 649 "BLUE": "38;5;20", 650 "CYAN": "38;5;25", 651 "GREEN": "38;5;28", 652 "INTENSE_BLACK": "38;5;59", 653 "INTENSE_BLUE": "38;5;61", 654 "INTENSE_CYAN": "38;5;194", 655 "INTENSE_GREEN": "38;5;34", 656 "INTENSE_PURPLE": "38;5;188", 657 "INTENSE_RED": "38;5;172", 658 "INTENSE_WHITE": "38;5;15", 659 "INTENSE_YELLOW": "38;5;188", 660 "NO_COLOR": "0", 661 "PURPLE": "38;5;125", 662 "RED": "38;5;124", 663 "WHITE": "38;5;145", 664 "YELLOW": "38;5;130", 665 } 666 _ansi_expand_style(style) 667 return style 668 669 670def _perldoc_style(): 671 style = { 672 "BLACK": "38;5;18", 673 "BLUE": "38;5;18", 674 "CYAN": "38;5;31", 675 "GREEN": "38;5;34", 676 "INTENSE_BLACK": "38;5;59", 677 "INTENSE_BLUE": "38;5;134", 678 "INTENSE_CYAN": "38;5;145", 679 "INTENSE_GREEN": "38;5;28", 680 "INTENSE_PURPLE": "38;5;134", 681 "INTENSE_RED": "38;5;167", 682 "INTENSE_WHITE": "38;5;188", 683 "INTENSE_YELLOW": "38;5;188", 684 "NO_COLOR": "0", 685 "PURPLE": "38;5;90", 686 "RED": "38;5;124", 687 "WHITE": "38;5;145", 688 "YELLOW": "38;5;166", 689 } 690 _ansi_expand_style(style) 691 return style 692 693 694def _rrt_style(): 695 style = { 696 "BLACK": "38;5;09", 697 "BLUE": "38;5;117", 698 "CYAN": "38;5;117", 699 "GREEN": "38;5;46", 700 "INTENSE_BLACK": "38;5;117", 701 "INTENSE_BLUE": "38;5;117", 702 "INTENSE_CYAN": "38;5;122", 703 "INTENSE_GREEN": "38;5;46", 704 "INTENSE_PURPLE": "38;5;213", 705 "INTENSE_RED": "38;5;09", 706 "INTENSE_WHITE": "38;5;188", 707 "INTENSE_YELLOW": "38;5;222", 708 "NO_COLOR": "0", 709 "PURPLE": "38;5;213", 710 "RED": "38;5;09", 711 "WHITE": "38;5;117", 712 "YELLOW": "38;5;09", 713 } 714 _ansi_expand_style(style) 715 return style 716 717 718def _tango_style(): 719 style = { 720 "BLACK": "38;5;16", 721 "BLUE": "38;5;20", 722 "CYAN": "38;5;61", 723 "GREEN": "38;5;34", 724 "INTENSE_BLACK": "38;5;24", 725 "INTENSE_BLUE": "38;5;62", 726 "INTENSE_CYAN": "38;5;15", 727 "INTENSE_GREEN": "38;5;64", 728 "INTENSE_PURPLE": "38;5;15", 729 "INTENSE_RED": "38;5;09", 730 "INTENSE_WHITE": "38;5;15", 731 "INTENSE_YELLOW": "38;5;178", 732 "NO_COLOR": "0", 733 "PURPLE": "38;5;90", 734 "RED": "38;5;124", 735 "WHITE": "38;5;15", 736 "YELLOW": "38;5;94", 737 } 738 _ansi_expand_style(style) 739 return style 740 741 742def _trac_style(): 743 style = { 744 "BLACK": "38;5;16", 745 "BLUE": "38;5;18", 746 "CYAN": "38;5;30", 747 "GREEN": "38;5;100", 748 "INTENSE_BLACK": "38;5;59", 749 "INTENSE_BLUE": "38;5;60", 750 "INTENSE_CYAN": "38;5;194", 751 "INTENSE_GREEN": "38;5;102", 752 "INTENSE_PURPLE": "38;5;188", 753 "INTENSE_RED": "38;5;137", 754 "INTENSE_WHITE": "38;5;224", 755 "INTENSE_YELLOW": "38;5;188", 756 "NO_COLOR": "0", 757 "PURPLE": "38;5;90", 758 "RED": "38;5;124", 759 "WHITE": "38;5;145", 760 "YELLOW": "38;5;100", 761 } 762 _ansi_expand_style(style) 763 return style 764 765 766def _vim_style(): 767 style = { 768 "BLACK": "38;5;18", 769 "BLUE": "38;5;18", 770 "CYAN": "38;5;44", 771 "GREEN": "38;5;40", 772 "INTENSE_BLACK": "38;5;60", 773 "INTENSE_BLUE": "38;5;68", 774 "INTENSE_CYAN": "38;5;44", 775 "INTENSE_GREEN": "38;5;40", 776 "INTENSE_PURPLE": "38;5;164", 777 "INTENSE_RED": "38;5;09", 778 "INTENSE_WHITE": "38;5;188", 779 "INTENSE_YELLOW": "38;5;184", 780 "NO_COLOR": "0", 781 "PURPLE": "38;5;164", 782 "RED": "38;5;160", 783 "WHITE": "38;5;188", 784 "YELLOW": "38;5;160", 785 } 786 _ansi_expand_style(style) 787 return style 788 789 790def _vs_style(): 791 style = { 792 "BLACK": "38;5;28", 793 "BLUE": "38;5;21", 794 "CYAN": "38;5;31", 795 "GREEN": "38;5;28", 796 "INTENSE_BLACK": "38;5;31", 797 "INTENSE_BLUE": "38;5;31", 798 "INTENSE_CYAN": "38;5;31", 799 "INTENSE_GREEN": "38;5;31", 800 "INTENSE_PURPLE": "38;5;31", 801 "INTENSE_RED": "38;5;09", 802 "INTENSE_WHITE": "38;5;31", 803 "INTENSE_YELLOW": "38;5;31", 804 "NO_COLOR": "0", 805 "PURPLE": "38;5;124", 806 "RED": "38;5;124", 807 "WHITE": "38;5;31", 808 "YELLOW": "38;5;124", 809 } 810 _ansi_expand_style(style) 811 return style 812 813 814def _xcode_style(): 815 style = { 816 "BLACK": "38;5;16", 817 "BLUE": "38;5;20", 818 "CYAN": "38;5;60", 819 "GREEN": "38;5;28", 820 "INTENSE_BLACK": "38;5;60", 821 "INTENSE_BLUE": "38;5;20", 822 "INTENSE_CYAN": "38;5;60", 823 "INTENSE_GREEN": "38;5;60", 824 "INTENSE_PURPLE": "38;5;126", 825 "INTENSE_RED": "38;5;160", 826 "INTENSE_WHITE": "38;5;60", 827 "INTENSE_YELLOW": "38;5;94", 828 "NO_COLOR": "0", 829 "PURPLE": "38;5;126", 830 "RED": "38;5;160", 831 "WHITE": "38;5;60", 832 "YELLOW": "38;5;94", 833 } 834 _ansi_expand_style(style) 835 return style 836 837 838ANSI_STYLES = LazyDict( 839 { 840 "algol": _algol_style, 841 "algol_nu": _algol_nu_style, 842 "autumn": _autumn_style, 843 "borland": _borland_style, 844 "bw": _bw_style, 845 "colorful": _colorful_style, 846 "default": _default_style, 847 "emacs": _emacs_style, 848 "friendly": _friendly_style, 849 "fruity": _fruity_style, 850 "igor": _igor_style, 851 "lovelace": _lovelace_style, 852 "manni": _manni_style, 853 "monokai": _monokai_style, 854 "murphy": _murphy_style, 855 "native": _native_style, 856 "paraiso-dark": _paraiso_dark_style, 857 "paraiso-light": _paraiso_light_style, 858 "pastie": _pastie_style, 859 "perldoc": _perldoc_style, 860 "rrt": _rrt_style, 861 "tango": _tango_style, 862 "trac": _trac_style, 863 "vim": _vim_style, 864 "vs": _vs_style, 865 "xcode": _xcode_style, 866 }, 867 globals(), 868 "ANSI_STYLES", 869) 870 871del ( 872 _algol_style, 873 _algol_nu_style, 874 _autumn_style, 875 _borland_style, 876 _bw_style, 877 _colorful_style, 878 _default_style, 879 _emacs_style, 880 _friendly_style, 881 _fruity_style, 882 _igor_style, 883 _lovelace_style, 884 _manni_style, 885 _monokai_style, 886 _murphy_style, 887 _native_style, 888 _paraiso_dark_style, 889 _paraiso_light_style, 890 _pastie_style, 891 _perldoc_style, 892 _rrt_style, 893 _tango_style, 894 _trac_style, 895 _vim_style, 896 _vs_style, 897 _xcode_style, 898) 899 900 901# 902# Dynamically generated styles 903# 904def make_ansi_style(palette): 905 """Makes an ANSI color style from a color palette""" 906 style = {"NO_COLOR": "0"} 907 for name, t in BASE_XONSH_COLORS.items(): 908 closest = find_closest_color(t, palette) 909 if len(closest) == 3: 910 closest = "".join([a * 2 for a in closest]) 911 short = rgb2short(closest)[0] 912 style[name] = "38;5;" + short 913 style["BOLD_" + name] = "1;38;5;" + short 914 style["UNDERLINE_" + name] = "4;38;5;" + short 915 style["BOLD_UNDERLINE_" + name] = "1;4;38;5;" + short 916 style["BACKGROUND_" + name] = "48;5;" + short 917 return style 918 919 920def ansi_style_by_name(name): 921 """Gets or makes an ANSI color style by name. If the styles does not 922 exist, it will look for a style using the pygments name. 923 """ 924 if name in ANSI_STYLES: 925 return ANSI_STYLES[name] 926 elif not HAS_PYGMENTS: 927 raise KeyError("could not find style {0!r}".format(name)) 928 from xonsh.pygments_cache import get_style_by_name 929 930 pstyle = get_style_by_name(name) 931 palette = make_palette(pstyle.styles.values()) 932 astyle = make_ansi_style(palette) 933 ANSI_STYLES[name] = astyle 934 return astyle 935