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