Coverage for /pythoncovmergedfiles/medio/medio/usr/local/lib/python3.8/site-packages/colorama/ansi.py: 89%
74 statements
« prev ^ index » next coverage.py v7.2.7, created at 2023-06-07 06:35 +0000
« prev ^ index » next coverage.py v7.2.7, created at 2023-06-07 06:35 +0000
1# Copyright Jonathan Hartley 2013. BSD 3-Clause license, see LICENSE file.
2'''
3This module generates ANSI character codes to printing colors to terminals.
4See: http://en.wikipedia.org/wiki/ANSI_escape_code
5'''
7CSI = '\033['
8OSC = '\033]'
9BEL = '\a'
12def code_to_chars(code):
13 return CSI + str(code) + 'm'
15def set_title(title):
16 return OSC + '2;' + title + BEL
18def clear_screen(mode=2):
19 return CSI + str(mode) + 'J'
21def clear_line(mode=2):
22 return CSI + str(mode) + 'K'
25class AnsiCodes(object):
26 def __init__(self):
27 # the subclasses declare class attributes which are numbers.
28 # Upon instantiation we define instance attributes, which are the same
29 # as the class attributes but wrapped with the ANSI escape sequence
30 for name in dir(self):
31 if not name.startswith('_'):
32 value = getattr(self, name)
33 setattr(self, name, code_to_chars(value))
36class AnsiCursor(object):
37 def UP(self, n=1):
38 return CSI + str(n) + 'A'
39 def DOWN(self, n=1):
40 return CSI + str(n) + 'B'
41 def FORWARD(self, n=1):
42 return CSI + str(n) + 'C'
43 def BACK(self, n=1):
44 return CSI + str(n) + 'D'
45 def POS(self, x=1, y=1):
46 return CSI + str(y) + ';' + str(x) + 'H'
49class AnsiFore(AnsiCodes):
50 BLACK = 30
51 RED = 31
52 GREEN = 32
53 YELLOW = 33
54 BLUE = 34
55 MAGENTA = 35
56 CYAN = 36
57 WHITE = 37
58 RESET = 39
60 # These are fairly well supported, but not part of the standard.
61 LIGHTBLACK_EX = 90
62 LIGHTRED_EX = 91
63 LIGHTGREEN_EX = 92
64 LIGHTYELLOW_EX = 93
65 LIGHTBLUE_EX = 94
66 LIGHTMAGENTA_EX = 95
67 LIGHTCYAN_EX = 96
68 LIGHTWHITE_EX = 97
71class AnsiBack(AnsiCodes):
72 BLACK = 40
73 RED = 41
74 GREEN = 42
75 YELLOW = 43
76 BLUE = 44
77 MAGENTA = 45
78 CYAN = 46
79 WHITE = 47
80 RESET = 49
82 # These are fairly well supported, but not part of the standard.
83 LIGHTBLACK_EX = 100
84 LIGHTRED_EX = 101
85 LIGHTGREEN_EX = 102
86 LIGHTYELLOW_EX = 103
87 LIGHTBLUE_EX = 104
88 LIGHTMAGENTA_EX = 105
89 LIGHTCYAN_EX = 106
90 LIGHTWHITE_EX = 107
93class AnsiStyle(AnsiCodes):
94 BRIGHT = 1
95 DIM = 2
96 NORMAL = 22
97 RESET_ALL = 0
99Fore = AnsiFore()
100Back = AnsiBack()
101Style = AnsiStyle()
102Cursor = AnsiCursor()