forked from CERTCC/tapioca
-
Notifications
You must be signed in to change notification settings - Fork 0
/
color.py
executable file
·162 lines (129 loc) · 3.88 KB
/
color.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
#!/usr/bin/env python3.8
# BEGIN LICENSE #
#
# CERT Tapioca
#
# Copyright 2018 Carnegie Mellon University. All Rights Reserved.
#
# NO WARRANTY. THIS CARNEGIE MELLON UNIVERSITY AND SOFTWARE
# ENGINEERING INSTITUTE MATERIAL IS FURNISHED ON AN "AS-IS" BASIS.
# CARNEGIE MELLON UNIVERSITY MAKES NO WARRANTIES OF ANY KIND, EITHER
# EXPRESSED OR IMPLIED, AS TO ANY MATTER INCLUDING, BUT NOT LIMITED
# TO, WARRANTY OF FITNESS FOR PURPOSE OR MERCHANTABILITY, EXCLUSIVITY,
# OR RESULTS OBTAINED FROM USE OF THE MATERIAL. CARNEGIE MELLON
# UNIVERSITY DOES NOT MAKE ANY WARRANTY OF ANY KIND WITH RESPECT TO
# FREEDOM FROM PATENT, TRADEMARK, OR COPYRIGHT INFRINGEMENT.
#
# Released under a BSD (SEI)-style license, please see license.txt or
# contact [email protected] for full terms.
#
# [DISTRIBUTION STATEMENT A] This material has been approved for
# public release and unlimited distribution. Please see Copyright
# notice for non-US Government use and distribution.
# CERT(R) is registered in the U.S. Patent and Trademark Office by
# Carnegie Mellon University.
#
# DM18-0637
#
# END LICENSE #
"""
Utility methods for writing output to a console.
"""
import re
import sys
from colorama import Fore, Style, AnsiToWin32, init as colorama_init
def color_stream():
"""
Initializes sys.stdout with `colorama's <https://github.com/tartley/colorama>`_ formatting capabilities.
:return: a colorized output stream
:rtype: colorama.ansitowin32.StreamWrapper
"""
colorama_init(wrap=False)
return AnsiToWin32(sys.stdout).stream
def colorize(color, string):
"""
Wraps `string` with the `color` ANSI color code.
:param color: color to style `string` with
:type color: colorama.ansi.Fore
:param string: string to colorize
:type string: str
:return: colorized string
:rtype: str
"""
return "{}{}{}".format(color, string, Style.RESET_ALL)
# Credit goes to Martijn Pieters (http://stackoverflow.com/users/100297/martijn-pieters)
# From post:
# http://stackoverflow.com/questions/14693701/how-can-i-remove-the-ansi-escape-sequences-from-a-string-in-python
_ansi_escape = re.compile(r'\x1b[^m]*m')
def decolorize(string):
"""
Removes any ANSI color codes from `string`.
:param string: a string with or without ANSI color codes
:type string: str
:return: the string without ANSI color codes
:rtype: str
"""
return _ansi_escape.sub('', string)
def red(string):
"""
Colors `string` red.
:param string: a string
:type string: str
:return: colorized string
:rtype: str
"""
return colorize(Fore.RED, string)
def yellow(string):
"""
Colors `string` yellow.
:param string: a string
:type string: str
:return: colorized string
:rtype: str
"""
return colorize(Fore.YELLOW, string)
def green(string):
"""
Colors `string` green.
:param string: a string
:type string: str
:return: colorized string
:rtype: str
"""
return colorize(Fore.GREEN, string)
def blue(string):
"""
Colors `string` blue.
:param string: a string
:type string: str
:return: colorized string
:rtype: str
"""
return colorize(Fore.BLUE, string)
def cyan(string):
"""
Colors `string` cyan.
:param string: a string
:type string: str
:return: colorized string
:rtype: str
"""
return colorize(Fore.CYAN, string)
def bright(string):
"""
Brightens `string`.
:param string: a string
:type string: str
:return: styled string
:rtype: str
"""
return colorize(Style.BRIGHT, string)
def dim(string):
"""
Dims `string`.
:param string: a string
:type string: str
:return: styled string
:rtype: str
"""
return colorize(Style.DIM, string)