forked from donnemartin/gitsome
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathpyghooks.py
1456 lines (1314 loc) · 42.9 KB
/
pyghooks.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
# -*- coding: utf-8 -*-
"""Hooks for pygments syntax highlighting."""
import os
import re
import sys
import builtins
from collections import ChainMap
from collections.abc import MutableMapping
from pygments.lexer import inherit, bygroups, include
from pygments.lexers.agile import PythonLexer
from pygments.token import (
Keyword,
Name,
Comment,
String,
Error,
Number,
Operator,
Generic,
Whitespace,
Token,
Punctuation,
Text,
)
from pygments.style import Style
import pygments.util
from xonsh.commands_cache import CommandsCache
from xonsh.lazyasd import LazyObject, LazyDict, lazyobject
from xonsh.tools import (
ON_WINDOWS,
intensify_colors_for_cmd_exe,
ansicolors_to_ptk1_names,
ANSICOLOR_NAMES_MAP,
PTK_NEW_OLD_COLOR_MAP,
hardcode_colors_for_win10,
FORMATTER,
)
from xonsh.color_tools import (
RE_BACKGROUND,
BASE_XONSH_COLORS,
make_palette,
find_closest_color,
)
from xonsh.style_tools import norm_name
from xonsh.lazyimps import terminal256
from xonsh.platform import (
os_environ,
win_ansi_support,
ptk_version_info,
pygments_version_info,
)
from xonsh.pygments_cache import get_style_by_name
def _command_is_valid(cmd):
try:
cmd_abspath = os.path.abspath(os.path.expanduser(cmd))
except (FileNotFoundError, OSError):
return False
return cmd in builtins.__xonsh__.commands_cache or (
os.path.isfile(cmd_abspath) and os.access(cmd_abspath, os.X_OK)
)
def _command_is_autocd(cmd):
if not builtins.__xonsh__.env.get("AUTO_CD", False):
return False
try:
cmd_abspath = os.path.abspath(os.path.expanduser(cmd))
except (FileNotFoundError, OSError):
return False
return os.path.isdir(cmd_abspath)
def subproc_cmd_callback(_, match):
"""Yield Builtin token if match contains valid command,
otherwise fallback to fallback lexer.
"""
cmd = match.group()
yield match.start(), Name.Builtin if _command_is_valid(cmd) else Error, cmd
def subproc_arg_callback(_, match):
"""Check if match contains valid path"""
text = match.group()
try:
ispath = os.path.exists(os.path.expanduser(text))
except (FileNotFoundError, OSError):
ispath = False
yield (match.start(), Name.Constant if ispath else Text, text)
COMMAND_TOKEN_RE = r'[^=\s\[\]{}()$"\'`<&|;!]+(?=\s|$|\)|\]|\}|!)'
class XonshLexer(PythonLexer):
"""Xonsh console lexer for pygments."""
name = "Xonsh lexer"
aliases = ["xonsh", "xsh"]
filenames = ["*.xsh", "*xonshrc"]
def __init__(self, *args, **kwargs):
# If the lexer is loaded as a pygment plugin, we have to mock
# __xonsh__.env and __xonsh__.commands_cache
if not hasattr(builtins, "__xonsh__"):
from argparse import Namespace
setattr(builtins, "__xonsh__", Namespace())
if not hasattr(builtins.__xonsh__, "env"):
setattr(builtins.__xonsh__, "env", {})
if ON_WINDOWS:
pathext = os_environ.get("PATHEXT", [".EXE", ".BAT", ".CMD"])
builtins.__xonsh__.env["PATHEXT"] = pathext.split(os.pathsep)
if not hasattr(builtins.__xonsh__, "commands_cache"):
setattr(builtins.__xonsh__, "commands_cache", CommandsCache())
_ = builtins.__xonsh__.commands_cache.all_commands # NOQA
super().__init__(*args, **kwargs)
tokens = {
"mode_switch_brackets": [
(r"(\$)(\{)", bygroups(Keyword, Punctuation), "py_curly_bracket"),
(r"(@)(\()", bygroups(Keyword, Punctuation), "py_bracket"),
(
r"([\!\$])(\()",
bygroups(Keyword, Punctuation),
("subproc_bracket", "subproc_start"),
),
(
r"(@\$)(\()",
bygroups(Keyword, Punctuation),
("subproc_bracket", "subproc_start"),
),
(
r"([\!\$])(\[)",
bygroups(Keyword, Punctuation),
("subproc_square_bracket", "subproc_start"),
),
(r"(g?)(`)", bygroups(String.Affix, String.Backtick), "backtick_re"),
],
"subproc_bracket": [(r"\)", Punctuation, "#pop"), include("subproc")],
"subproc_square_bracket": [(r"\]", Punctuation, "#pop"), include("subproc")],
"py_bracket": [(r"\)", Punctuation, "#pop"), include("root")],
"py_curly_bracket": [(r"\}", Punctuation, "#pop"), include("root")],
"backtick_re": [
(r"[\.\^\$\*\+\?\[\]\|]", String.Regex),
(r"({[0-9]+}|{[0-9]+,[0-9]+})\??", String.Regex),
(r"\\([0-9]+|[AbBdDsSwWZabfnrtuUvx\\])", String.Escape),
(r"`", String.Backtick, "#pop"),
(r"[^`\.\^\$\*\+\?\[\]\|]+", String.Backtick),
],
"root": [
(r"\?", Keyword),
(r"(?<=\w)!", Keyword),
(r"\$\w+", Name.Variable),
(r"\(", Punctuation, "py_bracket"),
(r"\{", Punctuation, "py_curly_bracket"),
include("mode_switch_brackets"),
inherit,
],
"subproc_start": [
(r"\s+", Whitespace),
(COMMAND_TOKEN_RE, subproc_cmd_callback, "#pop"),
(r"", Whitespace, "#pop"),
],
"subproc": [
include("mode_switch_brackets"),
(r"&&|\|\|", Operator, "subproc_start"),
(r'"(\\\\|\\[0-7]+|\\.|[^"\\])*"', String.Double),
(r"'(\\\\|\\[0-7]+|\\.|[^'\\])*'", String.Single),
(r"(?<=\w|\s)!", Keyword, "subproc_macro"),
(r"^!", Keyword, "subproc_macro"),
(r";", Punctuation, "subproc_start"),
(r"&|=", Punctuation),
(r"\|", Punctuation, "subproc_start"),
(r"\s+", Text),
(r'[^=\s\[\]{}()$"\'`<&|;]+', subproc_arg_callback),
(r"<", Text),
(r"\$\w+", Name.Variable),
],
"subproc_macro": [
(r"(\s*)([^\n]+)", bygroups(Whitespace, String)),
(r"", Whitespace, "#pop"),
],
}
def get_tokens_unprocessed(self, text):
"""Check first command, then call super.get_tokens_unprocessed
with root or subproc state"""
start = 0
state = ("root",)
m = re.match(r"(\s*)({})".format(COMMAND_TOKEN_RE), text)
if m is not None:
yield m.start(1), Whitespace, m.group(1)
cmd = m.group(2)
cmd_is_valid = _command_is_valid(cmd)
cmd_is_autocd = _command_is_autocd(cmd)
if cmd_is_valid or cmd_is_autocd:
yield (m.start(2), Name.Builtin if cmd_is_valid else Name.Constant, cmd)
start = m.end(2)
state = ("subproc",)
for i, t, v in super().get_tokens_unprocessed(text[start:], state):
yield i + start, t, v
class XonshConsoleLexer(XonshLexer):
"""Xonsh console lexer for pygments."""
name = "Xonsh console lexer"
aliases = ["xonshcon"]
filenames = []
tokens = {
"root": [
(r"^(>>>|\.\.\.) ", Generic.Prompt),
(r"\n(>>>|\.\.\.)", Generic.Prompt),
(r"\n(?![>.][>.][>.] )([^\n]*)", Generic.Output),
(r"\n(?![>.][>.][>.] )(.*?)$", Generic.Output),
inherit,
]
}
#
# Colors and Styles
#
Color = Token.Color # alias to new color token namespace
def color_by_name(name, fg=None, bg=None):
"""Converts a color name to a color token, foreground name,
and background name. Will take into consideration current foreground
and background colors, if provided.
Parameters
----------
name : str
Color name.
fg : str, optional
Foreground color name.
bg : str, optional
Background color name.
Returns
-------
tok : Token
Pygments Token.Color subclass
fg : str or None
New computed foreground color name.
bg : str or None
New computed background color name.
"""
name = name.upper()
if name == "NO_COLOR":
return Color.NO_COLOR, None, None
m = RE_BACKGROUND.search(name)
if m is None: # must be foreground color
fg = norm_name(name)
else:
bg = norm_name(name)
# assemble token
if fg is None and bg is None:
tokname = "NO_COLOR"
elif fg is None:
tokname = bg
elif bg is None:
tokname = fg
else:
tokname = fg + "__" + bg
tok = getattr(Color, tokname)
return tok, fg, bg
def code_by_name(name, styles):
"""Converts a token name into a pygments-style color code.
Parameters
----------
name : str
Color token name.
styles : Mapping
Mapping for looking up non-hex colors
Returns
-------
code : str
Pygments style color code.
"""
fg, _, bg = name.lower().partition("__")
if fg.startswith("background_"):
fg, bg = bg, fg
codes = []
# foreground color
if len(fg) == 0:
pass
elif "hex" in fg:
for p in fg.split("_"):
codes.append("#" + p[3:] if p.startswith("hex") else p)
else:
fgtok = getattr(Color, fg.upper())
if fgtok in styles:
codes.append(styles[fgtok])
else:
codes += fg.split("_")
# background color
if len(bg) == 0:
pass
elif bg.startswith("background_hex"):
codes.append("bg:#" + bg[14:])
else:
bgtok = getattr(Color, bg.upper())
if bgtok in styles:
codes.append(styles[bgtok])
else:
codes.append(bg.replace("background_", "bg:"))
code = " ".join(codes)
return code
def partial_color_tokenize(template):
"""Tokenizes a template string containing colors. Will return a list
of tuples mapping the token to the string which has that color.
These sub-strings maybe templates themselves.
"""
if builtins.__xonsh__.shell is not None:
styles = __xonsh__.shell.shell.styler.styles
else:
styles = None
color = Color.NO_COLOR
try:
toks, color = _partial_color_tokenize_main(template, styles)
except Exception:
toks = [(Color.NO_COLOR, template)]
if styles is not None:
styles[color] # ensure color is available
return toks
def _partial_color_tokenize_main(template, styles):
bopen = "{"
bclose = "}"
colon = ":"
expl = "!"
color = Color.NO_COLOR
fg = bg = None
value = ""
toks = []
for literal, field, spec, conv in FORMATTER.parse(template):
if field is None:
value += literal
elif field in KNOWN_COLORS or "#" in field:
value += literal
next_color, fg, bg = color_by_name(field, fg, bg)
if next_color is not color:
if len(value) > 0:
toks.append((color, value))
if styles is not None:
styles[color] # ensure color is available
color = next_color
value = ""
elif field is not None:
parts = [literal, bopen, field]
if conv is not None and len(conv) > 0:
parts.append(expl)
parts.append(conv)
if spec is not None and len(spec) > 0:
parts.append(colon)
parts.append(spec)
parts.append(bclose)
value += "".join(parts)
else:
value += literal
toks.append((color, value))
return toks, color
class CompoundColorMap(MutableMapping):
"""Looks up color tokens by name, potentially generating the value
from the lookup.
"""
def __init__(self, styles, *args, **kwargs):
self.styles = styles
self.colors = dict(*args, **kwargs)
def __getitem__(self, key):
if key in self.colors:
return self.colors[key]
if key in self.styles:
value = self.styles[key]
self[key] = value
return value
if key is Color:
raise KeyError
pre, _, name = str(key).rpartition(".")
if pre != "Token.Color":
raise KeyError
value = code_by_name(name, self.styles)
self[key] = value
return value
def __setitem__(self, key, value):
self.colors[key] = value
def __delitem__(self, key):
del self.colors[key]
def __iter__(self):
yield from self.colors.keys()
def __len__(self):
return len(self.colors)
class XonshStyle(Style):
"""A xonsh pygments style that will dispatch to the correct color map
by using a ChainMap. The style_name property may be used to reset
the current style.
"""
def __init__(self, style_name="default"):
"""
Parameters
----------
style_name : str, optional
The style name to initialize with.
"""
self.trap = {} # for trapping custom colors set by user
self._smap = {}
self._style_name = ""
self.style_name = style_name
super().__init__()
@property
def style_name(self):
return self._style_name
@style_name.setter
def style_name(self, value):
if self._style_name == value:
return
if value not in STYLES:
try: # loading style dynamically
pygments_style_by_name(value)
except Exception:
print(
"Could not find style {0!r}, using default".format(value),
file=sys.stderr,
)
value = "default"
builtins.__xonsh__.env["XONSH_COLOR_STYLE"] = value
cmap = STYLES[value]
if value == "default":
self._smap = XONSH_BASE_STYLE.copy()
else:
try:
self._smap = get_style_by_name(value)().styles.copy()
except (ImportError, pygments.util.ClassNotFound):
self._smap = XONSH_BASE_STYLE.copy()
compound = CompoundColorMap(ChainMap(self.trap, cmap, PTK_STYLE, self._smap))
self.styles = ChainMap(self.trap, cmap, PTK_STYLE, self._smap, compound)
self._style_name = value
# Convert new ansicolor names to old PTK1 names
# Can be remvoed when PTK1 support is dropped.
if builtins.__xonsh__.shell.shell_type != "prompt_toolkit2":
for smap in [self.trap, cmap, PTK_STYLE, self._smap]:
smap.update(ansicolors_to_ptk1_names(smap))
if ON_WINDOWS and "prompt_toolkit" in builtins.__xonsh__.shell.shell_type:
self.enhance_colors_for_cmd_exe()
@style_name.deleter
def style_name(self):
self._style_name = ""
def enhance_colors_for_cmd_exe(self):
""" Enhance colors when using cmd.exe on windows.
When using the default style all blue and dark red colors
are changed to CYAN and intense red.
"""
env = builtins.__xonsh__.env
# Ensure we are not using ConEmu or Visual Stuio Code
if "CONEMUANSI" in env or "VSCODE_PID" in env:
return
if env.get("INTENSIFY_COLORS_ON_WIN", False):
if win_ansi_support():
newcolors = hardcode_colors_for_win10(self.styles)
else:
newcolors = intensify_colors_for_cmd_exe(self.styles)
self.trap.update(newcolors)
def xonsh_style_proxy(styler):
"""Factory for a proxy class to a xonsh style."""
# Monky patch pygments' list of known ansi colors
# with the new ansi color names used by PTK2
# Can be removed once pygment names get fixed.
pygments.style.ansicolors.update(ANSICOLOR_NAMES_MAP)
class XonshStyleProxy(Style):
"""Simple proxy class to fool prompt toolkit."""
target = styler
styles = styler.styles
def __new__(cls, *args, **kwargs):
return cls.target
return XonshStyleProxy
PTK_STYLE = LazyObject(
lambda: {
Token.Menu.Completions: "bg:ansigray ansiblack",
Token.Menu.Completions.Completion: "",
Token.Menu.Completions.Completion.Current: "bg:ansibrightblack ansiwhite",
Token.Scrollbar: "bg:ansibrightblack",
Token.Scrollbar.Button: "bg:ansiblack",
Token.Scrollbar.Arrow: "bg:ansiblack ansiwhite bold",
Token.AutoSuggestion: "ansibrightblack",
Token.Aborted: "ansibrightblack",
},
globals(),
"PTK_STYLE",
)
XONSH_BASE_STYLE = LazyObject(
lambda: {
Whitespace: "ansigray",
Comment: "underline ansicyan",
Comment.Preproc: "underline ansiyellow",
Keyword: "bold ansigreen",
Keyword.Pseudo: "nobold",
Keyword.Type: "nobold ansired",
Operator: "ansibrightblack",
Operator.Word: "bold ansimagenta",
Name.Builtin: "ansigreen",
Name.Function: "ansibrightblue",
Name.Class: "bold ansibrightblue",
Name.Namespace: "bold ansibrightblue",
Name.Exception: "bold ansibrightred",
Name.Variable: "ansiblue",
Name.Constant: "ansired",
Name.Label: "ansibrightyellow",
Name.Entity: "bold ansigray",
Name.Attribute: "ansibrightyellow",
Name.Tag: "bold ansigreen",
Name.Decorator: "ansibrightmagenta",
String: "ansibrightred",
String.Doc: "underline",
String.Interpol: "bold ansimagenta",
String.Escape: "bold ansiyellow",
String.Regex: "ansimagenta",
String.Symbol: "ansiyellow",
String.Other: "ansigreen",
Number: "ansibrightblack",
Generic.Heading: "bold ansiblue",
Generic.Subheading: "bold ansimagenta",
Generic.Deleted: "ansired",
Generic.Inserted: "ansibrightgreen",
Generic.Error: "bold ansibrightred",
Generic.Emph: "underline",
Generic.Prompt: "bold ansiblue",
Generic.Output: "ansiblue",
Generic.Traceback: "ansiblue",
Error: "ansibrightred",
},
globals(),
"XONSH_BASE_STYLE",
)
KNOWN_COLORS = LazyObject(
lambda: frozenset(
[
"BACKGROUND_BLACK",
"BACKGROUND_BLUE",
"BACKGROUND_CYAN",
"BACKGROUND_GREEN",
"BACKGROUND_INTENSE_BLACK",
"BACKGROUND_INTENSE_BLUE",
"BACKGROUND_INTENSE_CYAN",
"BACKGROUND_INTENSE_GREEN",
"BACKGROUND_INTENSE_PURPLE",
"BACKGROUND_INTENSE_RED",
"BACKGROUND_INTENSE_WHITE",
"BACKGROUND_INTENSE_YELLOW",
"BACKGROUND_PURPLE",
"BACKGROUND_RED",
"BACKGROUND_WHITE",
"BACKGROUND_YELLOW",
"BLACK",
"BLUE",
"BOLD_BLACK",
"BOLD_BLUE",
"BOLD_CYAN",
"BOLD_GREEN",
"BOLD_INTENSE_BLACK",
"BOLD_INTENSE_BLUE",
"BOLD_INTENSE_CYAN",
"BOLD_INTENSE_GREEN",
"BOLD_INTENSE_PURPLE",
"BOLD_INTENSE_RED",
"BOLD_INTENSE_WHITE",
"BOLD_INTENSE_YELLOW",
"BOLD_PURPLE",
"BOLD_RED",
"BOLD_UNDERLINE_BLACK",
"BOLD_UNDERLINE_BLUE",
"BOLD_UNDERLINE_CYAN",
"BOLD_UNDERLINE_GREEN",
"BOLD_UNDERLINE_INTENSE_BLACK",
"BOLD_UNDERLINE_INTENSE_BLUE",
"BOLD_UNDERLINE_INTENSE_CYAN",
"BOLD_UNDERLINE_INTENSE_GREEN",
"BOLD_UNDERLINE_INTENSE_PURPLE",
"BOLD_UNDERLINE_INTENSE_RED",
"BOLD_UNDERLINE_INTENSE_WHITE",
"BOLD_UNDERLINE_INTENSE_YELLOW",
"BOLD_UNDERLINE_PURPLE",
"BOLD_UNDERLINE_RED",
"BOLD_UNDERLINE_WHITE",
"BOLD_UNDERLINE_YELLOW",
"BOLD_WHITE",
"BOLD_YELLOW",
"CYAN",
"GREEN",
"INTENSE_BLACK",
"INTENSE_BLUE",
"INTENSE_CYAN",
"INTENSE_GREEN",
"INTENSE_PURPLE",
"INTENSE_RED",
"INTENSE_WHITE",
"INTENSE_YELLOW",
"NO_COLOR",
"PURPLE",
"RED",
"UNDERLINE_BLACK",
"UNDERLINE_BLUE",
"UNDERLINE_CYAN",
"UNDERLINE_GREEN",
"UNDERLINE_INTENSE_BLACK",
"UNDERLINE_INTENSE_BLUE",
"UNDERLINE_INTENSE_CYAN",
"UNDERLINE_INTENSE_GREEN",
"UNDERLINE_INTENSE_PURPLE",
"UNDERLINE_INTENSE_RED",
"UNDERLINE_INTENSE_WHITE",
"UNDERLINE_INTENSE_YELLOW",
"UNDERLINE_PURPLE",
"UNDERLINE_RED",
"UNDERLINE_WHITE",
"UNDERLINE_YELLOW",
"WHITE",
"YELLOW",
]
),
globals(),
"KNOWN_COLORS",
)
def _expand_style(cmap):
"""Expands a style in order to more quickly make color map changes."""
for key, val in list(cmap.items()):
if key is Color.NO_COLOR:
continue
_, _, key = str(key).rpartition(".")
cmap[getattr(Color, "BOLD_" + key)] = "bold " + val
cmap[getattr(Color, "UNDERLINE_" + key)] = "underline " + val
cmap[getattr(Color, "BOLD_UNDERLINE_" + key)] = "bold underline " + val
if val == "noinherit":
cmap[getattr(Color, "BACKGROUND_" + key)] = val
else:
cmap[getattr(Color, "BACKGROUND_" + key)] = "bg:" + val
def _bw_style():
style = {
Color.BLACK: "noinherit",
Color.BLUE: "noinherit",
Color.CYAN: "noinherit",
Color.GREEN: "noinherit",
Color.INTENSE_BLACK: "noinherit",
Color.INTENSE_BLUE: "noinherit",
Color.INTENSE_CYAN: "noinherit",
Color.INTENSE_GREEN: "noinherit",
Color.INTENSE_PURPLE: "noinherit",
Color.INTENSE_RED: "noinherit",
Color.INTENSE_WHITE: "noinherit",
Color.INTENSE_YELLOW: "noinherit",
Color.NO_COLOR: "noinherit",
Color.PURPLE: "noinherit",
Color.RED: "noinherit",
Color.WHITE: "noinherit",
Color.YELLOW: "noinherit",
}
_expand_style(style)
return style
def _default_style():
style = {
Color.BLACK: "ansiblack",
Color.BLUE: "ansiblue",
Color.CYAN: "ansicyan",
Color.GREEN: "ansigreen",
Color.INTENSE_BLACK: "ansibrightblack",
Color.INTENSE_BLUE: "ansibrightblue",
Color.INTENSE_CYAN: "ansibrightcyan",
Color.INTENSE_GREEN: "ansibrightgreen",
Color.INTENSE_PURPLE: "ansibrightmagenta",
Color.INTENSE_RED: "ansibrightred",
Color.INTENSE_WHITE: "ansiwhite",
Color.INTENSE_YELLOW: "ansibrightyellow",
Color.NO_COLOR: "noinherit",
Color.PURPLE: "ansimagenta",
Color.RED: "ansired",
Color.WHITE: "ansigray",
Color.YELLOW: "ansiyellow",
}
_expand_style(style)
return style
def _monokai_style():
style = {
Color.BLACK: "#1e0010",
Color.BLUE: "#6666ef",
Color.CYAN: "#66d9ef",
Color.GREEN: "#2ee22e",
Color.INTENSE_BLACK: "#5e5e5e",
Color.INTENSE_BLUE: "#2626d7",
Color.INTENSE_CYAN: "#2ed9d9",
Color.INTENSE_GREEN: "#a6e22e",
Color.INTENSE_PURPLE: "#ae81ff",
Color.INTENSE_RED: "#f92672",
Color.INTENSE_WHITE: "#f8f8f2",
Color.INTENSE_YELLOW: "#e6db74",
Color.NO_COLOR: "noinherit",
Color.PURPLE: "#960050",
Color.RED: "#AF0000",
Color.WHITE: "#d7d7d7",
Color.YELLOW: "#e2e22e",
}
_expand_style(style)
return style
######################################
# Auto-generated below this line #
######################################
def _algol_style():
style = {
Color.BLACK: "#666",
Color.BLUE: "#666",
Color.CYAN: "#666",
Color.GREEN: "#666",
Color.INTENSE_BLACK: "#666",
Color.INTENSE_BLUE: "#888",
Color.INTENSE_CYAN: "#888",
Color.INTENSE_GREEN: "#888",
Color.INTENSE_PURPLE: "#888",
Color.INTENSE_RED: "#FF0000",
Color.INTENSE_WHITE: "#888",
Color.INTENSE_YELLOW: "#888",
Color.NO_COLOR: "noinherit",
Color.PURPLE: "#666",
Color.RED: "#FF0000",
Color.WHITE: "#888",
Color.YELLOW: "#FF0000",
}
_expand_style(style)
return style
def _algol_nu_style():
style = {
Color.BLACK: "#666",
Color.BLUE: "#666",
Color.CYAN: "#666",
Color.GREEN: "#666",
Color.INTENSE_BLACK: "#666",
Color.INTENSE_BLUE: "#888",
Color.INTENSE_CYAN: "#888",
Color.INTENSE_GREEN: "#888",
Color.INTENSE_PURPLE: "#888",
Color.INTENSE_RED: "#FF0000",
Color.INTENSE_WHITE: "#888",
Color.INTENSE_YELLOW: "#888",
Color.NO_COLOR: "noinherit",
Color.PURPLE: "#666",
Color.RED: "#FF0000",
Color.WHITE: "#888",
Color.YELLOW: "#FF0000",
}
_expand_style(style)
return style
def _autumn_style():
style = {
Color.BLACK: "#000080",
Color.BLUE: "#0000aa",
Color.CYAN: "#00aaaa",
Color.GREEN: "#00aa00",
Color.INTENSE_BLACK: "#555555",
Color.INTENSE_BLUE: "#1e90ff",
Color.INTENSE_CYAN: "#1e90ff",
Color.INTENSE_GREEN: "#4c8317",
Color.INTENSE_PURPLE: "#FAA",
Color.INTENSE_RED: "#aa5500",
Color.INTENSE_WHITE: "#bbbbbb",
Color.INTENSE_YELLOW: "#FAA",
Color.NO_COLOR: "noinherit",
Color.PURPLE: "#800080",
Color.RED: "#aa0000",
Color.WHITE: "#aaaaaa",
Color.YELLOW: "#aa5500",
}
_expand_style(style)
return style
def _borland_style():
style = {
Color.BLACK: "#000000",
Color.BLUE: "#000080",
Color.CYAN: "#008080",
Color.GREEN: "#008800",
Color.INTENSE_BLACK: "#555555",
Color.INTENSE_BLUE: "#0000FF",
Color.INTENSE_CYAN: "#ddffdd",
Color.INTENSE_GREEN: "#888888",
Color.INTENSE_PURPLE: "#e3d2d2",
Color.INTENSE_RED: "#FF0000",
Color.INTENSE_WHITE: "#ffdddd",
Color.INTENSE_YELLOW: "#e3d2d2",
Color.NO_COLOR: "noinherit",
Color.PURPLE: "#800080",
Color.RED: "#aa0000",
Color.WHITE: "#aaaaaa",
Color.YELLOW: "#a61717",
}
_expand_style(style)
return style
def _colorful_style():
style = {
Color.BLACK: "#000",
Color.BLUE: "#00C",
Color.CYAN: "#0e84b5",
Color.GREEN: "#00A000",
Color.INTENSE_BLACK: "#555",
Color.INTENSE_BLUE: "#33B",
Color.INTENSE_CYAN: "#bbbbbb",
Color.INTENSE_GREEN: "#888",
Color.INTENSE_PURPLE: "#FAA",
Color.INTENSE_RED: "#D42",
Color.INTENSE_WHITE: "#fff0ff",
Color.INTENSE_YELLOW: "#FAA",
Color.NO_COLOR: "noinherit",
Color.PURPLE: "#800080",
Color.RED: "#A00000",
Color.WHITE: "#bbbbbb",
Color.YELLOW: "#A60",
}
_expand_style(style)
return style
def _emacs_style():
style = {
Color.BLACK: "#008000",
Color.BLUE: "#000080",
Color.CYAN: "#04D",
Color.GREEN: "#00A000",
Color.INTENSE_BLACK: "#666666",
Color.INTENSE_BLUE: "#04D",
Color.INTENSE_CYAN: "#bbbbbb",
Color.INTENSE_GREEN: "#00BB00",
Color.INTENSE_PURPLE: "#AA22FF",
Color.INTENSE_RED: "#D2413A",
Color.INTENSE_WHITE: "#bbbbbb",
Color.INTENSE_YELLOW: "#bbbbbb",
Color.NO_COLOR: "noinherit",
Color.PURPLE: "#800080",
Color.RED: "#A00000",
Color.WHITE: "#bbbbbb",
Color.YELLOW: "#BB6622",
}
_expand_style(style)
return style
def _friendly_style():
style = {
Color.BLACK: "#007020",
Color.BLUE: "#000080",
Color.CYAN: "#0e84b5",
Color.GREEN: "#00A000",
Color.INTENSE_BLACK: "#555555",
Color.INTENSE_BLUE: "#70a0d0",
Color.INTENSE_CYAN: "#60add5",
Color.INTENSE_GREEN: "#40a070",
Color.INTENSE_PURPLE: "#bb60d5",
Color.INTENSE_RED: "#d55537",
Color.INTENSE_WHITE: "#fff0f0",
Color.INTENSE_YELLOW: "#bbbbbb",
Color.NO_COLOR: "noinherit",
Color.PURPLE: "#800080",
Color.RED: "#A00000",
Color.WHITE: "#bbbbbb",
Color.YELLOW: "#c65d09",
}
_expand_style(style)
return style
def _fruity_style():
style = {
Color.BLACK: "#0f140f",
Color.BLUE: "#0086d2",
Color.CYAN: "#0086d2",
Color.GREEN: "#008800",
Color.INTENSE_BLACK: "#444444",
Color.INTENSE_BLUE: "#0086f7",
Color.INTENSE_CYAN: "#0086f7",
Color.INTENSE_GREEN: "#888888",
Color.INTENSE_PURPLE: "#ff0086",
Color.INTENSE_RED: "#fb660a",
Color.INTENSE_WHITE: "#ffffff",
Color.INTENSE_YELLOW: "#cdcaa9",
Color.NO_COLOR: "noinherit",
Color.PURPLE: "#ff0086",
Color.RED: "#ff0007",
Color.WHITE: "#cdcaa9",
Color.YELLOW: "#fb660a",
}
_expand_style(style)
return style
def _igor_style():
style = {
Color.BLACK: "#009C00",
Color.BLUE: "#0000FF",
Color.CYAN: "#007575",
Color.GREEN: "#009C00",
Color.INTENSE_BLACK: "#007575",
Color.INTENSE_BLUE: "#0000FF",
Color.INTENSE_CYAN: "#007575",
Color.INTENSE_GREEN: "#009C00",
Color.INTENSE_PURPLE: "#CC00A3",
Color.INTENSE_RED: "#C34E00",
Color.INTENSE_WHITE: "#CC00A3",
Color.INTENSE_YELLOW: "#C34E00",
Color.NO_COLOR: "noinherit",
Color.PURPLE: "#CC00A3",
Color.RED: "#C34E00",
Color.WHITE: "#CC00A3",
Color.YELLOW: "#C34E00",
}
_expand_style(style)
return style
def _lovelace_style():
style = {
Color.BLACK: "#444444",
Color.BLUE: "#2838b0",
Color.CYAN: "#289870",
Color.GREEN: "#388038",
Color.INTENSE_BLACK: "#666666",
Color.INTENSE_BLUE: "#2838b0",
Color.INTENSE_CYAN: "#888888",
Color.INTENSE_GREEN: "#289870",
Color.INTENSE_PURPLE: "#a848a8",
Color.INTENSE_RED: "#b83838",
Color.INTENSE_WHITE: "#888888",
Color.INTENSE_YELLOW: "#a89028",
Color.NO_COLOR: "noinherit",
Color.PURPLE: "#a848a8",
Color.RED: "#c02828",
Color.WHITE: "#888888",
Color.YELLOW: "#b85820",
}
_expand_style(style)
return style