Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
sqlmapproject
GitHub Repository: sqlmapproject/sqlmap
Path: blob/master/thirdparty/termcolor/termcolor.py
2992 views
1
# coding: utf-8
2
# Copyright (c) 2008-2011 Volvox Development Team
3
#
4
# Permission is hereby granted, free of charge, to any person obtaining a copy
5
# of this software and associated documentation files (the "Software"), to deal
6
# in the Software without restriction, including without limitation the rights
7
# to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
8
# copies of the Software, and to permit persons to whom the Software is
9
# furnished to do so, subject to the following conditions:
10
#
11
# The above copyright notice and this permission notice shall be included in
12
# all copies or substantial portions of the Software.
13
#
14
# THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
15
# IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
16
# FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
17
# AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
18
# LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
19
# OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
20
# THE SOFTWARE.
21
#
22
# Author: Konstantin Lepa <[email protected]>
23
24
"""ANSII Color formatting for output in terminal."""
25
26
from __future__ import print_function
27
import os
28
29
30
__ALL__ = [ 'colored', 'cprint' ]
31
32
VERSION = (1, 1, 0)
33
34
ATTRIBUTES = dict(
35
list(zip([
36
'bold',
37
'dark',
38
'',
39
'underline',
40
'blink',
41
'',
42
'reverse',
43
'concealed'
44
],
45
list(range(1, 9))
46
))
47
)
48
del ATTRIBUTES['']
49
50
51
HIGHLIGHTS = dict(
52
list(zip([
53
'on_grey',
54
'on_red',
55
'on_green',
56
'on_yellow',
57
'on_blue',
58
'on_magenta',
59
'on_cyan',
60
'on_white'
61
],
62
list(range(40, 48))
63
))
64
)
65
66
67
COLORS = dict(
68
list(zip([
69
'grey',
70
'red',
71
'green',
72
'yellow',
73
'blue',
74
'magenta',
75
'cyan',
76
'white',
77
],
78
list(range(30, 38))
79
))
80
)
81
82
COLORS.update(dict(("light%s" % color, COLORS[color] + 60) for color in COLORS))
83
84
# Reference: https://misc.flogisoft.com/bash/tip_colors_and_formatting
85
COLORS["lightgrey"] = 37
86
COLORS["darkgrey"] = 90
87
88
RESET = '\033[0m'
89
90
91
def colored(text, color=None, on_color=None, attrs=None):
92
"""Colorize text.
93
94
Available text colors:
95
red, green, yellow, blue, magenta, cyan, white.
96
97
Available text highlights:
98
on_red, on_green, on_yellow, on_blue, on_magenta, on_cyan, on_white.
99
100
Available attributes:
101
bold, dark, underline, blink, reverse, concealed.
102
103
Example:
104
colored('Hello, World!', 'red', 'on_grey', ['blue', 'blink'])
105
colored('Hello, World!', 'green')
106
"""
107
if os.getenv('ANSI_COLORS_DISABLED') is None:
108
fmt_str = '\033[%dm%s'
109
if color is not None:
110
text = fmt_str % (COLORS[color], text)
111
112
if on_color is not None:
113
text = fmt_str % (HIGHLIGHTS[on_color], text)
114
115
if attrs is not None:
116
for attr in attrs:
117
text = fmt_str % (ATTRIBUTES[attr], text)
118
119
text += RESET
120
return text
121
122
123
def cprint(text, color=None, on_color=None, attrs=None, **kwargs):
124
"""Print colorize text.
125
126
It accepts arguments of print function.
127
"""
128
129
print((colored(text, color, on_color, attrs)), **kwargs)
130
131
132
if __name__ == '__main__':
133
print('Current terminal type: %s' % os.getenv('TERM'))
134
print('Test basic colors:')
135
cprint('Grey color', 'grey')
136
cprint('Red color', 'red')
137
cprint('Green color', 'green')
138
cprint('Yellow color', 'yellow')
139
cprint('Blue color', 'blue')
140
cprint('Magenta color', 'magenta')
141
cprint('Cyan color', 'cyan')
142
cprint('White color', 'white')
143
print(('-' * 78))
144
145
print('Test highlights:')
146
cprint('On grey color', on_color='on_grey')
147
cprint('On red color', on_color='on_red')
148
cprint('On green color', on_color='on_green')
149
cprint('On yellow color', on_color='on_yellow')
150
cprint('On blue color', on_color='on_blue')
151
cprint('On magenta color', on_color='on_magenta')
152
cprint('On cyan color', on_color='on_cyan')
153
cprint('On white color', color='grey', on_color='on_white')
154
print('-' * 78)
155
156
print('Test attributes:')
157
cprint('Bold grey color', 'grey', attrs=['bold'])
158
cprint('Dark red color', 'red', attrs=['dark'])
159
cprint('Underline green color', 'green', attrs=['underline'])
160
cprint('Blink yellow color', 'yellow', attrs=['blink'])
161
cprint('Reversed blue color', 'blue', attrs=['reverse'])
162
cprint('Concealed Magenta color', 'magenta', attrs=['concealed'])
163
cprint('Bold underline reverse cyan color', 'cyan',
164
attrs=['bold', 'underline', 'reverse'])
165
cprint('Dark blink concealed white color', 'white',
166
attrs=['dark', 'blink', 'concealed'])
167
print(('-' * 78))
168
169
print('Test mixing:')
170
cprint('Underline red on grey color', 'red', 'on_grey',
171
['underline'])
172
cprint('Reversed green on red color', 'green', 'on_red', ['reverse'])
173
174
175