-
Notifications
You must be signed in to change notification settings - Fork 3
/
color.py
71 lines (58 loc) · 1.43 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
# -*- coding: utf-8 -*-
__author__='Dennis Hafemann, https://github.com/dennishafemann/python-TerminalColors'
# Severals
ESCAPE_SEQUENCE="\033[%sm"
# Styles
RESET = 0
BOLD = 1
UNDERLINE = 4
BLINK = 5
REVERSE_VIDEO = 7
# Colors
BLACK = 30
RED = 31
GREEN = 32
YELLOW = 33
BLUE = 34
MAGENTA = 35
CYAN = 36
WHITE = 37
def _createColoredString(*args):
"""
Internal method, which generates, returns a color-string.
"""
value=""
# Walk through parameters/arguments
for arg in args[0]:
if type(arg)==int:
value+=ESCAPE_SEQUENCE % str(arg)
else:
value+=arg
# If args available, append reset
if value:
value+=ESCAPE_SEQUENCE % str(RESET)
return value
def cprint(*args):
"""
Directly prints out the generated color-string.
"""
print(_createColoredString(args))
def rcprint(*args):
"""
Return a generated color-string.
"""
return _createColoredString(args)
def error(message):
"""
For printing/outputting simple, small error messages.
"""
cprint(RED, message)
if __name__=='__main__':
# Example
# Prints directly
cprint('This text is', RED, ' red', RESET, ' and ', RED, BOLD, 'bold', RESET, '.')
# Prints via a variable
colorString=rcprint('This text is', RED, ' red', RESET, ' and ', RED, BOLD, 'bold', RESET, ', returned.')
print(colorString)
# Error
error('This is a simple, small error message.')