-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathcolorprint.py
110 lines (85 loc) · 3.34 KB
/
colorprint.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
# -*- encoding: utf-8 -*-
"""
Module colorprint
~~~~~~~~~~~~~~
Python module to print in color using py3k-style print function. It uses
funny hack, which allow to create print function instead standard print
routine and give it some "black" magic.
Print function is like imported for __future__, but has three additional
parameters: color (foreground of text output), background (it's background)
and format (bold, blink and so on).
You can read more at __future__.print_function documentation.
Usage example
-------------
>>> from __future__ import print_function
>>> from colorprint import *
>>> print('Hello', 'world', color='blue', end='', sep=', ')
>>> print('!', color='red', format=['bold', 'blink'])
Hello, world!
^-- blue ^-- blinking, bold and red
:copyright: 2012 Aleksey Rembish
:license: BSD
"""
from __future__ import print_function
import __builtin__
import sys
__all__ = ['print']
__author__ = 'Aleksey Rembish'
__email__ = 'alex@rembish.ru'
__description__ = 'Python module to print in color using py3k-style print function'
__url__ = 'https://github.com/don-ramon/colorprint'
__copyright__ = '(c) 2012 %s' % __author__
__license__ = 'BSD'
__version__ = '0.1'
_colors = {
'grey': 30, 'red': 31,
'green': 32, 'yellow': 33,
'blue': 34, 'magenta': 35,
'cyan': 36, 'white': 37,
}
_backgrounds = {
'grey': 40, 'red': 41,
'green': 42, 'yellow': 43,
'blue': 44, 'magenta': 45,
'cyan': 46, 'white': 47,
}
_formats = {
'bold': 1, 'dark': 2,
'underline': 4, 'blink': 5,
'reverse': 7, 'concealed': 8,
}
def print(*args, **kwargs):
'''print(value, ..., sep=' ', end='\n', file=sys.stdout, color=None, background=None, format=None)
Prints the values to a stream, or to sys.stdout by default.
Optional keyword arguments:
file: a file-like object (stream); defaults to the current sys.stdout.
sep: string inserted between values, default a space.
end: string appended after the last value, default a newline.
Additional keyword arguments:
color: prints values in specified color:
grey red green yellow blue magenta cyan white
background: prints values on specified color (same as color)
format: prints values using specifiend format(s) (can be string or list):
bold dark underline reverse concealed'''
color = kwargs.pop('color', None)
background = kwargs.pop('background', None)
formats = kwargs.pop('format', [])
if isinstance(formats, basestring):
formats = [formats]
file = kwargs.get('file', sys.stdout)
if color or background:
end = kwargs.pop('end', "\n")
kwargs['end'] = ""
if color:
__builtin__.print('\033[%dm' % _colors[color], file=file, end='')
if background:
__builtin__.print('\033[%dm' % _backgrounds[background], file=file, end='')
for format in formats:
__builtin__.print('\033[%dm' % _formats[format], file=file, end='')
__builtin__.print(*args, **kwargs)
__builtin__.print('\033[0m', file=file, end=end)
else:
__builtin__.print(*args, **kwargs)
if __name__ == '__main__':
print('Hello', 'world', color='white', background='blue', format='underline', end='', sep=', ')
print('!', color='red', format=['bold', 'blink'])