forked from Robpol86/colorclass
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcolorclass.py
881 lines (695 loc) · 35 KB
/
colorclass.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
"""Colorful worry-free console applications for Linux, Mac OS X, and Windows.
Supported natively on Linux and Mac OSX (Just Works), and on Windows it works the same if Windows.enable() is called.
Gives you expected and sane results from methods like len() and .capitalize().
https://github.com/Robpol86/colorclass
https://pypi.python.org/pypi/colorclass
"""
import atexit
import ctypes
import os
import re
import sys
from collections import Mapping
if os.name == 'nt':
import ctypes.wintypes
__author__ = '@Robpol86'
__license__ = 'MIT'
__version__ = '1.2.0'
_BASE_CODES = {
'/all': 0, 'b': 1, 'f': 2, 'i': 3, 'u': 4, 'flash': 5, 'outline': 6, 'negative': 7, 'invis': 8, 'strike': 9,
'/b': 22, '/f': 22, '/i': 23, '/u': 24, '/flash': 25, '/outline': 26, '/negative': 27, '/invis': 28,
'/strike': 29, '/fg': 39, '/bg': 49,
'black': 30, 'red': 31, 'green': 32, 'yellow': 33, 'blue': 34, 'magenta': 35, 'cyan': 36, 'white': 37,
'bgblack': 40, 'bgred': 41, 'bggreen': 42, 'bgyellow': 43, 'bgblue': 44, 'bgmagenta': 45, 'bgcyan': 46,
'bgwhite': 47,
'hiblack': 90, 'hired': 91, 'higreen': 92, 'hiyellow': 93, 'hiblue': 94, 'himagenta': 95, 'hicyan': 96,
'hiwhite': 97,
'hibgblack': 100, 'hibgred': 101, 'hibggreen': 102, 'hibgyellow': 103, 'hibgblue': 104, 'hibgmagenta': 105,
'hibgcyan': 106, 'hibgwhite': 107,
'autored': None, 'autoblack': None, 'automagenta': None, 'autowhite': None, 'autoblue': None, 'autoyellow': None,
'autogreen': None, 'autocyan': None,
'autobgred': None, 'autobgblack': None, 'autobgmagenta': None, 'autobgwhite': None, 'autobgblue': None,
'autobgyellow': None, 'autobggreen': None, 'autobgcyan': None,
'/black': 39, '/red': 39, '/green': 39, '/yellow': 39, '/blue': 39, '/magenta': 39, '/cyan': 39, '/white': 39,
'/hiblack': 39, '/hired': 39, '/higreen': 39, '/hiyellow': 39, '/hiblue': 39, '/himagenta': 39, '/hicyan': 39,
'/hiwhite': 39,
'/bgblack': 49, '/bgred': 49, '/bggreen': 49, '/bgyellow': 49, '/bgblue': 49, '/bgmagenta': 49, '/bgcyan': 49,
'/bgwhite': 49, '/hibgblack': 49, '/hibgred': 49, '/hibggreen': 49, '/hibgyellow': 49, '/hibgblue': 49,
'/hibgmagenta': 49, '/hibgcyan': 49, '/hibgwhite': 49,
'/autored': 39, '/autoblack': 39, '/automagenta': 39, '/autowhite': 39, '/autoblue': 39, '/autoyellow': 39,
'/autogreen': 39, '/autocyan': 39,
'/autobgred': 49, '/autobgblack': 49, '/autobgmagenta': 49, '/autobgwhite': 49, '/autobgblue': 49,
'/autobgyellow': 49, '/autobggreen': 49, '/autobgcyan': 49,
}
_WINDOWS_CODES = {
'/all': -33, '/fg': -39, '/bg': -49,
'black': 0, 'red': 4, 'green': 2, 'yellow': 6, 'blue': 1, 'magenta': 5, 'cyan': 3, 'white': 7,
'bgblack': -8, 'bgred': 64, 'bggreen': 32, 'bgyellow': 96, 'bgblue': 16, 'bgmagenta': 80, 'bgcyan': 48,
'bgwhite': 112,
'hiblack': 8, 'hired': 12, 'higreen': 10, 'hiyellow': 14, 'hiblue': 9, 'himagenta': 13, 'hicyan': 11, 'hiwhite': 15,
'hibgblack': 128, 'hibgred': 192, 'hibggreen': 160, 'hibgyellow': 224, 'hibgblue': 144, 'hibgmagenta': 208,
'hibgcyan': 176, 'hibgwhite': 240,
'/black': -39, '/red': -39, '/green': -39, '/yellow': -39, '/blue': -39, '/magenta': -39, '/cyan': -39,
'/white': -39, '/hiblack': -39, '/hired': -39, '/higreen': -39, '/hiyellow': -39, '/hiblue': -39, '/himagenta': -39,
'/hicyan': -39, '/hiwhite': -39,
'/bgblack': -49, '/bgred': -49, '/bggreen': -49, '/bgyellow': -49, '/bgblue': -49, '/bgmagenta': -49,
'/bgcyan': -49, '/bgwhite': -49, '/hibgblack': -49, '/hibgred': -49, '/hibggreen': -49, '/hibgyellow': -49,
'/hibgblue': -49, '/hibgmagenta': -49, '/hibgcyan': -49, '/hibgwhite': -49,
}
_RE_GROUP_SEARCH = re.compile(r'(?:\033\[[\d;]+m)+')
_RE_NUMBER_SEARCH = re.compile(r'\033\[([\d;]+)m')
_RE_SPLIT = re.compile(r'(\033\[[\d;]+m)')
PARENT_CLASS = type(u'')
class _AutoCodes(Mapping):
"""Read-only subclass of dict, resolves closing tags (based on colorclass.CODES) and automatic colors."""
DISABLE_COLORS = False
LIGHT_BACKGROUND = False
def __init__(self):
self.__dict = _BASE_CODES.copy()
def __getitem__(self, item):
if item == 'autoblack':
answer = self.autoblack
elif item == 'autored':
answer = self.autored
elif item == 'autogreen':
answer = self.autogreen
elif item == 'autoyellow':
answer = self.autoyellow
elif item == 'autoblue':
answer = self.autoblue
elif item == 'automagenta':
answer = self.automagenta
elif item == 'autocyan':
answer = self.autocyan
elif item == 'autowhite':
answer = self.autowhite
elif item == 'autobgblack':
answer = self.autobgblack
elif item == 'autobgred':
answer = self.autobgred
elif item == 'autobggreen':
answer = self.autobggreen
elif item == 'autobgyellow':
answer = self.autobgyellow
elif item == 'autobgblue':
answer = self.autobgblue
elif item == 'autobgmagenta':
answer = self.autobgmagenta
elif item == 'autobgcyan':
answer = self.autobgcyan
elif item == 'autobgwhite':
answer = self.autobgwhite
else:
answer = self.__dict[item]
return answer
def __iter__(self):
return iter(self.__dict)
def __len__(self):
return len(self.__dict)
@property
def autoblack(self):
"""Return automatic black foreground color depending on background color."""
return self.__dict['black' if _AutoCodes.LIGHT_BACKGROUND else 'hiblack']
@property
def autored(self):
"""Return automatic red foreground color depending on background color."""
return self.__dict['red' if _AutoCodes.LIGHT_BACKGROUND else 'hired']
@property
def autogreen(self):
"""Return automatic green foreground color depending on background color."""
return self.__dict['green' if _AutoCodes.LIGHT_BACKGROUND else 'higreen']
@property
def autoyellow(self):
"""Return automatic yellow foreground color depending on background color."""
return self.__dict['yellow' if _AutoCodes.LIGHT_BACKGROUND else 'hiyellow']
@property
def autoblue(self):
"""Return automatic blue foreground color depending on background color."""
return self.__dict['blue' if _AutoCodes.LIGHT_BACKGROUND else 'hiblue']
@property
def automagenta(self):
"""Return automatic magenta foreground color depending on background color."""
return self.__dict['magenta' if _AutoCodes.LIGHT_BACKGROUND else 'himagenta']
@property
def autocyan(self):
"""Return automatic cyan foreground color depending on background color."""
return self.__dict['cyan' if _AutoCodes.LIGHT_BACKGROUND else 'hicyan']
@property
def autowhite(self):
"""Return automatic white foreground color depending on background color."""
return self.__dict['white' if _AutoCodes.LIGHT_BACKGROUND else 'hiwhite']
@property
def autobgblack(self):
"""Return automatic black background color depending on background color."""
return self.__dict['bgblack' if _AutoCodes.LIGHT_BACKGROUND else 'hibgblack']
@property
def autobgred(self):
"""Return automatic red background color depending on background color."""
return self.__dict['bgred' if _AutoCodes.LIGHT_BACKGROUND else 'hibgred']
@property
def autobggreen(self):
"""Return automatic green background color depending on background color."""
return self.__dict['bggreen' if _AutoCodes.LIGHT_BACKGROUND else 'hibggreen']
@property
def autobgyellow(self):
"""Return automatic yellow background color depending on background color."""
return self.__dict['bgyellow' if _AutoCodes.LIGHT_BACKGROUND else 'hibgyellow']
@property
def autobgblue(self):
"""Return automatic blue background color depending on background color."""
return self.__dict['bgblue' if _AutoCodes.LIGHT_BACKGROUND else 'hibgblue']
@property
def autobgmagenta(self):
"""Return automatic magenta background color depending on background color."""
return self.__dict['bgmagenta' if _AutoCodes.LIGHT_BACKGROUND else 'hibgmagenta']
@property
def autobgcyan(self):
"""Return automatic cyan background color depending on background color."""
return self.__dict['bgcyan' if _AutoCodes.LIGHT_BACKGROUND else 'hibgcyan']
@property
def autobgwhite(self):
"""Return automatic white background color depending on background color."""
return self.__dict['bgwhite' if _AutoCodes.LIGHT_BACKGROUND else 'hibgwhite']
def _pad_input(incoming):
"""Avoid IndexError and KeyError by ignoring un-related fields.
Example: '{0}{autored}' becomes '{{0}}{autored}'.
Positional arguments:
incoming -- the input unicode value.
Returns:
Padded unicode value.
"""
incoming_expanded = incoming.replace('{', '{{').replace('}', '}}')
for key in _BASE_CODES:
before, after = '{{%s}}' % key, '{%s}' % key
if before in incoming_expanded:
incoming_expanded = incoming_expanded.replace(before, after)
return incoming_expanded
def _parse_input(incoming):
"""Perform the actual conversion of tags to ANSI escaped codes.
Provides a version of the input without any colors for len() and other methods.
Positional arguments:
incoming -- the input unicode value.
Returns:
2-item tuple. First item is the parsed output. Second item is a version of the input without any colors.
"""
codes = dict((k, v) for k, v in _AutoCodes().items() if '{%s}' % k in incoming)
color_codes = dict((k, '' if _AutoCodes.DISABLE_COLORS else '\033[{0}m'.format(v)) for k, v in codes.items())
incoming_padded = _pad_input(incoming)
output_colors = incoming_padded.format(**color_codes)
# Simplify: '{b}{red}' -> '\033[1m\033[31m' -> '\033[1;31m'
groups = sorted(set(_RE_GROUP_SEARCH.findall(output_colors)), key=len, reverse=True) # Get codes, grouped adjacent.
groups_simplified = [[x for n in _RE_NUMBER_SEARCH.findall(i) for x in n.split(';')] for i in groups]
groups_compiled = ['\033[{0}m'.format(';'.join(g)) for g in groups_simplified] # Final codes.
assert len(groups_compiled) == len(groups) # For testing.
output_colors_simplified = output_colors
for i in range(len(groups)):
output_colors_simplified = output_colors_simplified.replace(groups[i], groups_compiled[i])
output_no_colors = _RE_SPLIT.sub('', output_colors_simplified)
# Strip any remaining color codes.
if _AutoCodes.DISABLE_COLORS:
output_colors_simplified = _RE_NUMBER_SEARCH.sub('', output_colors_simplified)
return output_colors_simplified, output_no_colors
def disable_all_colors():
"""Disable all colors. Strips any color tags or codes."""
_AutoCodes.DISABLE_COLORS = True
def set_light_background():
"""Choose dark colors for all 'auto'-prefixed codes for readability on light backgrounds."""
_AutoCodes.DISABLE_COLORS = False
_AutoCodes.LIGHT_BACKGROUND = True
def set_dark_background():
"""Choose dark colors for all 'auto'-prefixed codes for readability on light backgrounds."""
_AutoCodes.DISABLE_COLORS = False
_AutoCodes.LIGHT_BACKGROUND = False
def list_tags():
"""List the available tags.
Returns:
Tuple of tuples. Child tuples are four items: ('opening tag', 'closing tag', main ansi value, closing ansi value).
"""
codes = _AutoCodes()
grouped = set([(k, '/{0}'.format(k), codes[k], codes['/{0}'.format(k)]) for k in codes if not k.startswith('/')])
# Add half-tags like /all.
found = [c for r in grouped for c in r[:2]]
missing = set([('', r[0], None, r[1]) if r[0].startswith('/') else (r[0], '', r[1], None)
for r in _AutoCodes().items() if r[0] not in found])
grouped |= missing
# Sort.
payload = sorted([i for i in grouped if i[2] is None], key=lambda x: x[3]) # /all /fg /bg
grouped -= set(payload)
payload.extend(sorted([i for i in grouped if i[2] < 10], key=lambda x: x[2])) # b i u flash
grouped -= set(payload)
payload.extend(sorted([i for i in grouped if i[0].startswith('auto')], key=lambda x: x[2])) # auto colors
grouped -= set(payload)
payload.extend(sorted([i for i in grouped if not i[0].startswith('hi')], key=lambda x: x[2])) # dark colors
grouped -= set(payload)
payload.extend(sorted(grouped, key=lambda x: x[2])) # light colors
return tuple(payload)
class ColorBytes(bytes):
"""Str (bytes in Python3) subclass, .decode() overridden to return Color() instance."""
def decode(*args, **kwargs):
"""Similar to str() method of the same name, returns Color() instance."""
return Color(super(ColorBytes, args[0]).decode(*args[1:], **kwargs))
class Color(PARENT_CLASS):
"""Unicode (str in Python3) subclass with ANSI terminal text color support.
Example syntax: Color('{red}Sample Text{/red}')
For a list of codes, call: colorclass.list_tags()
"""
@classmethod
def red(cls, s, auto=False):
"""Color-code entire string."""
return cls.colorize('red', s, auto=auto)
@classmethod
def bgred(cls, s, auto=False):
"""Color-code entire string."""
return cls.colorize('bgred', s, auto=auto)
@classmethod
def green(cls, s, auto=False):
"""Color-code entire string."""
return cls.colorize('green', s, auto=auto)
@classmethod
def bggreen(cls, s, auto=False):
"""Color-code entire string."""
return cls.colorize('bggreen', s, auto=auto)
@classmethod
def blue(cls, s, auto=False):
"""Color-code entire string."""
return cls.colorize('blue', s, auto=auto)
@classmethod
def bgblue(cls, s, auto=False):
"""Color-code entire string."""
return cls.colorize('bgblue', s, auto=auto)
@classmethod
def yellow(cls, s, auto=False):
"""Color-code entire string."""
return cls.colorize('yellow', s, auto=auto)
@classmethod
def bgyellow(cls, s, auto=False):
"""Color-code entire string."""
return cls.colorize('bgyellow', s, auto=auto)
@classmethod
def cyan(cls, s, auto=False):
"""Color-code entire string."""
return cls.colorize('cyan', s, auto=auto)
@classmethod
def bgcyan(cls, s, auto=False):
"""Color-code entire string."""
return cls.colorize('bgcyan', s, auto=auto)
@classmethod
def magenta(cls, s, auto=False):
"""Color-code entire string."""
return cls.colorize('magenta', s, auto=auto)
@classmethod
def bgmagenta(cls, s, auto=False):
"""Color-code entire string."""
return cls.colorize('bgmagenta', s, auto=auto)
@classmethod
def colorize(cls, color, s, auto=False):
"""Color-code entire string using specified color."""
tag = '{0}{1}'.format('auto' if auto else '', color)
return cls('{%s}%s{/%s}' % (tag, s, tag))
def __new__(cls, *args, **kwargs):
"""Constructor."""
parent_class = cls.__bases__[0]
value_markup = args[0] if args else parent_class()
value_colors, value_no_colors = _parse_input(value_markup)
if args:
args = [value_colors] + list(args[1:])
obj = parent_class.__new__(cls, *args, **kwargs)
obj.value_colors, obj.value_no_colors = value_colors, value_no_colors
obj.has_colors = bool(_RE_NUMBER_SEARCH.match(value_colors))
return obj
def __len__(self):
"""Return length of string without color codes (what users expect)."""
return self.value_no_colors.__len__()
def capitalize(self):
"""Similar to str() method of the same name, returns Color() instance."""
split = _RE_SPLIT.split(self.value_colors)
for i in range(len(split)):
if _RE_SPLIT.match(split[i]):
continue
split[i] = PARENT_CLASS(split[i]).capitalize()
return Color().join(split)
def center(self, width, fillchar=None):
"""Similar to str() method of the same name, returns Color() instance."""
if fillchar is not None:
result = PARENT_CLASS(self.value_no_colors).center(width, fillchar)
else:
result = PARENT_CLASS(self.value_no_colors).center(width)
return result.replace(self.value_no_colors, self.value_colors)
def count(self, *args, **kwargs):
"""Similar to str() method of the same name."""
return PARENT_CLASS(self.value_no_colors).count(*args, **kwargs)
def endswith(self, *args, **kwargs):
"""Similar to str() method of the same name."""
return PARENT_CLASS(self.value_no_colors).endswith(*args, **kwargs)
def encode(*args, **kwargs):
"""Similar to str() method of the same name, returns ColorBytes() instance."""
return ColorBytes(super(Color, args[0]).encode(*args[1:], **kwargs))
def decode(*args, **kwargs):
"""Similar to str() method of the same name, returns Color() instance."""
return Color(super(Color, args[0]).decode(*args[1:], **kwargs))
def find(self, *args, **kwargs):
"""Similar to str() method of the same name."""
return PARENT_CLASS(self.value_no_colors).find(*args, **kwargs)
def format(*args, **kwargs):
"""Similar to str() method of the same name, returns Color() instance."""
return Color(super(Color, args[0]).format(*args[1:], **kwargs))
def index(self, *args, **kwargs):
"""Similar to str() method of the same name."""
return PARENT_CLASS(self.value_no_colors).index(*args, **kwargs)
def isalnum(self):
"""Similar to str() method of the same name."""
return PARENT_CLASS(self.value_no_colors).isalnum()
def isalpha(self):
"""Similar to str() method of the same name."""
return PARENT_CLASS(self.value_no_colors).isalpha()
def isdecimal(self):
"""Similar to str() method of the same name."""
return PARENT_CLASS(self.value_no_colors).isdecimal()
def isdigit(self):
"""Similar to str() method of the same name."""
return PARENT_CLASS(self.value_no_colors).isdigit()
def isnumeric(self):
"""Similar to str() method of the same name."""
return PARENT_CLASS(self.value_no_colors).isnumeric()
def isspace(self):
"""Similar to str() method of the same name."""
return PARENT_CLASS(self.value_no_colors).isspace()
def istitle(self):
"""Similar to str() method of the same name."""
return PARENT_CLASS(self.value_no_colors).istitle()
def isupper(self):
"""Similar to str() method of the same name."""
return PARENT_CLASS(self.value_no_colors).isupper()
def ljust(self, width, fillchar=None):
"""Similar to str() method of the same name, returns Color() instance."""
if fillchar is not None:
result = PARENT_CLASS(self.value_no_colors).ljust(width, fillchar)
else:
result = PARENT_CLASS(self.value_no_colors).ljust(width)
return result.replace(self.value_no_colors, self.value_colors)
def rfind(self, *args, **kwargs):
"""Similar to str() method of the same name."""
return PARENT_CLASS(self.value_no_colors).rfind(*args, **kwargs)
def rindex(self, *args, **kwargs):
"""Similar to str() method of the same name."""
return PARENT_CLASS(self.value_no_colors).rindex(*args, **kwargs)
def rjust(self, width, fillchar=None):
"""Similar to str() method of the same name, returns Color() instance."""
if fillchar is not None:
result = PARENT_CLASS(self.value_no_colors).rjust(width, fillchar)
else:
result = PARENT_CLASS(self.value_no_colors).rjust(width)
return result.replace(self.value_no_colors, self.value_colors)
def splitlines(self):
"""Similar to str() method of the same name, returns Color() instances in a list."""
return [Color(l) for l in PARENT_CLASS(self.value_colors).splitlines()]
def startswith(self, *args, **kwargs):
"""Similar to str() method of the same name."""
return PARENT_CLASS(self.value_no_colors).startswith(*args, **kwargs)
def swapcase(self):
"""Similar to str() method of the same name, returns Color() instance."""
split = _RE_SPLIT.split(self.value_colors)
for i in range(len(split)):
if _RE_SPLIT.match(split[i]):
continue
split[i] = PARENT_CLASS(split[i]).swapcase()
return Color().join(split)
def title(self):
"""Similar to str() method of the same name, returns Color() instance."""
split = _RE_SPLIT.split(self.value_colors)
for i in range(len(split)):
if _RE_SPLIT.match(split[i]):
continue
split[i] = PARENT_CLASS(split[i]).title()
return Color().join(split)
def translate(self, table):
"""Similar to str() method of the same name, returns Color() instance."""
split = _RE_SPLIT.split(self.value_colors)
for i in range(len(split)):
if _RE_SPLIT.match(split[i]):
continue
split[i] = PARENT_CLASS(split[i]).translate(table)
return Color().join(split)
def upper(self):
"""Similar to str() method of the same name, returns Color() instance."""
split = _RE_SPLIT.split(self.value_colors)
for i in range(len(split)):
if _RE_SPLIT.match(split[i]):
continue
split[i] = PARENT_CLASS(split[i]).upper()
return Color().join(split)
def zfill(self, width):
"""Similar to str() method of the same name, returns Color() instance."""
if not self.value_no_colors:
return PARENT_CLASS().zfill(width)
split = _RE_SPLIT.split(self.value_colors)
filled = PARENT_CLASS(self.value_no_colors).zfill(width)
if len(split) == 1:
return filled
padding = filled.replace(self.value_no_colors, '')
if not split[0]:
split[2] = padding + split[2]
else:
split[0] = padding + split[0]
return Color().join(split)
class Windows(object):
"""Enable and disable Windows support for ANSI color character codes.
Call static method Windows.enable() to enable color support for the remainder of the process' lifetime.
This class is also a context manager. You can do this:
with Windows():
print(Color('{autored}Test{/autored}'))
Or this:
with Windows(auto_colors=True):
print(Color('{autored}Test{/autored}'))
"""
@staticmethod
def disable():
"""Restore sys.stderr and sys.stdout to their original objects. Resets colors to their original values."""
if os.name != 'nt' or not Windows.is_enabled():
return False
getattr(sys.stderr, '_reset_colors', lambda: False)()
getattr(sys.stdout, '_reset_colors', lambda: False)()
if hasattr(sys.stderr, 'ORIGINAL_STREAM'):
sys.stderr = getattr(sys.stderr, 'ORIGINAL_STREAM')
if hasattr(sys.stdout, 'ORIGINAL_STREAM'):
sys.stdout = getattr(sys.stdout, 'ORIGINAL_STREAM')
return True
@staticmethod
def is_enabled():
"""Return True if either stderr or stdout has colors enabled."""
return hasattr(sys.stderr, 'ORIGINAL_STREAM') or hasattr(sys.stdout, 'ORIGINAL_STREAM')
@staticmethod
def enable(auto_colors=False, reset_atexit=False):
"""Enable color text with print() or sys.stdout.write() (stderr too).
Keyword arguments:
auto_colors -- automatically selects dark or light colors based on current terminal's background color. Only
works with {autored} and related tags.
reset_atexit -- resets original colors upon Python exit (in case you forget to reset it yourself with a closing
tag).
"""
if os.name != 'nt':
return False
# Overwrite stream references.
if not hasattr(sys.stderr, 'ORIGINAL_STREAM'):
sys.stderr.flush()
sys.stderr = _WindowsStreamStdErr()
if not hasattr(sys.stdout, 'ORIGINAL_STREAM'):
sys.stdout.flush()
sys.stdout = _WindowsStreamStdOut()
if not hasattr(sys.stderr, 'ORIGINAL_STREAM') and not hasattr(sys.stdout, 'ORIGINAL_STREAM'):
return False
# Automatically select which colors to display.
bg_color = getattr(sys.stdout, 'default_bg', getattr(sys.stderr, 'default_bg', None))
if auto_colors and bg_color is not None:
set_light_background() if bg_color in (112, 96, 240, 176, 224, 208, 160) else set_dark_background()
# Reset on exit if requested.
if reset_atexit:
atexit.register(lambda: Windows.disable())
return True
def __init__(self, auto_colors=False):
"""Constructor."""
self.auto_colors = auto_colors
def __enter__(self):
"""Context manager, enables colors on Windows."""
Windows.enable(auto_colors=self.auto_colors)
def __exit__(self, *_):
"""Context manager, disabled colors on Windows."""
Windows.disable()
class _WindowsCSBI(object):
"""Interface with Windows CONSOLE_SCREEN_BUFFER_INFO API/DLL calls. Gets info for stderr and stdout.
References:
http://msdn.microsoft.com/en-us/library/windows/desktop/ms683231
https://code.google.com/p/colorama/issues/detail?id=47.
pytest's py project: py/_io/terminalwriter.py.
Class variables:
CSBI -- ConsoleScreenBufferInfo class/struct (not instance, the class definition itself) defined in _define_csbi().
HANDLE_STDERR -- GetStdHandle() return integer for stderr.
HANDLE_STDOUT -- GetStdHandle() return integer for stdout.
WINDLL -- my own loaded instance of ctypes.WinDLL.
"""
CSBI = None
HANDLE_STDERR = None
HANDLE_STDOUT = None
WINDLL = ctypes.LibraryLoader(getattr(ctypes, 'WinDLL', None))
@staticmethod
def _define_csbi():
"""Define structs and populates _WindowsCSBI.CSBI."""
if _WindowsCSBI.CSBI is not None:
return
class COORD(ctypes.Structure):
"""Windows COORD structure. http://msdn.microsoft.com/en-us/library/windows/desktop/ms682119."""
_fields_ = [('X', ctypes.c_short), ('Y', ctypes.c_short)]
class SmallRECT(ctypes.Structure):
"""Windows SMALL_RECT structure. http://msdn.microsoft.com/en-us/library/windows/desktop/ms686311."""
_fields_ = [('Left', ctypes.c_short), ('Top', ctypes.c_short), ('Right', ctypes.c_short),
('Bottom', ctypes.c_short)]
class ConsoleScreenBufferInfo(ctypes.Structure):
"""Windows CONSOLE_SCREEN_BUFFER_INFO structure.
http://msdn.microsoft.com/en-us/library/windows/desktop/ms682093
"""
_fields_ = [
('dwSize', COORD),
('dwCursorPosition', COORD),
('wAttributes', ctypes.wintypes.WORD),
('srWindow', SmallRECT),
('dwMaximumWindowSize', COORD)
]
_WindowsCSBI.CSBI = ConsoleScreenBufferInfo
@staticmethod
def initialize():
"""Initialize the WINDLL resource and populated the CSBI class variable."""
_WindowsCSBI._define_csbi()
_WindowsCSBI.HANDLE_STDERR = _WindowsCSBI.HANDLE_STDERR or _WindowsCSBI.WINDLL.kernel32.GetStdHandle(-12)
_WindowsCSBI.HANDLE_STDOUT = _WindowsCSBI.HANDLE_STDOUT or _WindowsCSBI.WINDLL.kernel32.GetStdHandle(-11)
if _WindowsCSBI.WINDLL.kernel32.GetConsoleScreenBufferInfo.argtypes:
return
_WindowsCSBI.WINDLL.kernel32.GetStdHandle.argtypes = [ctypes.wintypes.DWORD]
_WindowsCSBI.WINDLL.kernel32.GetStdHandle.restype = ctypes.wintypes.HANDLE
_WindowsCSBI.WINDLL.kernel32.GetConsoleScreenBufferInfo.restype = ctypes.wintypes.BOOL
_WindowsCSBI.WINDLL.kernel32.GetConsoleScreenBufferInfo.argtypes = [
ctypes.wintypes.HANDLE, ctypes.POINTER(_WindowsCSBI.CSBI)
]
@staticmethod
def get_info(handle):
"""Get information about this current console window (for Microsoft Windows only).
Raises IOError if attempt to get information fails (if there is no console window).
Don't forget to call _WindowsCSBI.initialize() once in your application before calling this method.
Positional arguments:
handle -- either _WindowsCSBI.HANDLE_STDERR or _WindowsCSBI.HANDLE_STDOUT.
Returns:
Dictionary with different integer values. Keys are:
buffer_width -- width of the buffer (Screen Buffer Size in cmd.exe layout tab).
buffer_height -- height of the buffer (Screen Buffer Size in cmd.exe layout tab).
terminal_width -- width of the terminal window.
terminal_height -- height of the terminal window.
bg_color -- current background color (http://msdn.microsoft.com/en-us/library/windows/desktop/ms682088).
fg_color -- current text color code.
"""
# Query Win32 API.
csbi = _WindowsCSBI.CSBI()
try:
if not _WindowsCSBI.WINDLL.kernel32.GetConsoleScreenBufferInfo(handle, ctypes.byref(csbi)):
raise IOError('Unable to get console screen buffer info from win32 API.')
except ctypes.ArgumentError:
raise IOError('Unable to get console screen buffer info from win32 API.')
# Parse data.
result = dict(
buffer_width=int(csbi.dwSize.X - 1),
buffer_height=int(csbi.dwSize.Y),
terminal_width=int(csbi.srWindow.Right - csbi.srWindow.Left),
terminal_height=int(csbi.srWindow.Bottom - csbi.srWindow.Top),
bg_color=int(csbi.wAttributes & 240),
fg_color=int(csbi.wAttributes % 16),
)
return result
class _WindowsStreamStdOut(object):
"""Replacement stream which overrides sys.stdout. When writing or printing, ANSI codes are converted.
ANSI (Linux/Unix) color codes are converted into win32 system calls, changing the next character's color before
printing it. Resources referenced:
https://github.com/tartley/colorama
http://www.cplusplus.com/articles/2ywTURfi/
http://thomasfischer.biz/python-and-windows-terminal-colors/
http://stackoverflow.com/questions/17125440/c-win32-console-color
http://www.tysos.org/svn/trunk/mono/corlib/System/WindowsConsoleDriver.cs
http://stackoverflow.com/questions/287871/print-in-terminal-with-colors-using-python
http://msdn.microsoft.com/en-us/library/windows/desktop/ms682088#_win32_character_attributes
Class variables:
ALL_BG_CODES -- list of background Windows codes. Used to determine if requested color is foreground or background.
COMPILED_CODES -- 'translation' dictionary. Keys are ANSI codes (values of _BASE_CODES), values are Windows codes.
ORIGINAL_STREAM -- the original stream to write non-code text to.
WIN32_STREAM_HANDLE -- handle to the Windows stdout device. Used by other Windows functions.
Instance variables:
default_fg -- the foreground Windows color code at the time of instantiation.
default_bg -- the background Windows color code at the time of instantiation.
"""
ALL_BG_CODES = [v for k, v in _WINDOWS_CODES.items() if k.startswith('bg') or k.startswith('hibg')]
COMPILED_CODES = dict((v, _WINDOWS_CODES[k]) for k, v in _BASE_CODES.items() if k in _WINDOWS_CODES)
ORIGINAL_STREAM = sys.stdout
WIN32_STREAM_HANDLE = _WindowsCSBI.HANDLE_STDOUT
def __init__(self):
_WindowsCSBI.initialize()
self.default_fg, self.default_bg = self._get_colors()
for attr in dir(self.ORIGINAL_STREAM):
if hasattr(self, attr):
continue
setattr(self, attr, getattr(self.ORIGINAL_STREAM, attr))
def __getattr__(self, item):
"""If an attribute/function/etc is not defined in this function, retrieve the one from the original stream.
Fixes ipython arrow key presses.
"""
return getattr(self.ORIGINAL_STREAM, item)
def _get_colors(self):
"""Return a tuple of two integers representing current colors: (foreground, background)."""
try:
csbi = _WindowsCSBI.get_info(self.WIN32_STREAM_HANDLE)
return csbi['fg_color'], csbi['bg_color']
except IOError:
return 7, 0
def _reset_colors(self):
"""Set the foreground and background colors to their original values (when class was instantiated)."""
self._set_color(-33)
def _set_color(self, color_code):
"""Change the foreground and background colors for subsequently printed characters.
Since setting a color requires including both foreground and background codes (merged), setting just the
foreground color resets the background color to black, and vice versa.
This function first gets the current background and foreground colors, merges in the requested color code, and
sets the result.
However if we need to remove just the foreground color but leave the background color the same (or vice versa)
such as when {/red} is used, we must merge the default foreground color with the current background color. This
is the reason for those negative values.
Positional arguments:
color_code -- integer color code from _WINDOWS_CODES.
"""
# Get current color code.
current_fg, current_bg = self._get_colors()
# Handle special negative codes. Also determine the final color code.
if color_code == -39:
final_color_code = self.default_fg | current_bg # Reset the foreground only.
elif color_code == -49:
final_color_code = current_fg | self.default_bg # Reset the background only.
elif color_code == -33:
final_color_code = self.default_fg | self.default_bg # Reset both.
elif color_code == -8:
final_color_code = current_fg # Black background.
else:
new_is_bg = color_code in self.ALL_BG_CODES
final_color_code = color_code | (current_fg if new_is_bg else current_bg)
# Set new code.
_WindowsCSBI.WINDLL.kernel32.SetConsoleTextAttribute(self.WIN32_STREAM_HANDLE, final_color_code)
def write(self, p_str):
for segment in _RE_SPLIT.split(p_str):
if not segment:
# Empty string. p_str probably starts with colors so the first item is always ''.
continue
if not _RE_SPLIT.match(segment):
# No color codes, print regular text.
self.ORIGINAL_STREAM.write(segment)
self.ORIGINAL_STREAM.flush()
continue
for color_code in (int(c) for c in _RE_NUMBER_SEARCH.findall(segment)[0].split(';')):
if color_code in self.COMPILED_CODES:
self._set_color(self.COMPILED_CODES[color_code])
class _WindowsStreamStdErr(_WindowsStreamStdOut):
"""Replacement stream which overrides sys.stderr. Subclasses _WindowsStreamStdOut."""
ORIGINAL_STREAM = sys.stderr
WIN32_STREAM_HANDLE = _WindowsCSBI.HANDLE_STDERR