-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathcolors.py
More file actions
51 lines (44 loc) · 1.05 KB
/
colors.py
File metadata and controls
51 lines (44 loc) · 1.05 KB
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
import sys
color_codes = {
'white' : '00',
'black' : '01',
'blue' : '02',
'green' : '03',
'red' : '04',
'brown' : '05',
'purple': '06',
'orange': '07',
'yellow': '08',
'lime' : '09',
'teal' : '10',
'aqua' : '11',
'royal' : '12',
'pink' : '13',
'grey' : '14',
'silver': '15',
}
ansi_colors = {
'green' : '1;32m',
'blue' : '1;34m',
'red' : '1;31m',
'brown' : '0;33m',
}
def color_modifier(color):
if color == 'reset':
return '\x0F'
fmt = ''
real_color = color
if color.startswith('bold'):
fmt += '\x02'
real_color = color[5:] # remove the "bold-" prefix
if len(real_color) > 0:
fmt += '\x03'
fmt += color_codes[real_color]
return fmt
def colorize(msg, color, target='irc'):
if target == 'irc':
return color_modifier(color) + msg + color_modifier('reset')
elif target == 'shell':
if not sys.stdout.isatty():
return line
return '\033[' + ansi_colors[color] + msg + '\033[0m'