Skip to content

Instantly share code, notes, and snippets.

@Ivlyth
Created May 14, 2015 03:33
Show Gist options
  • Save Ivlyth/39922dff3e14b7db9524 to your computer and use it in GitHub Desktop.
Save Ivlyth/39922dff3e14b7db9524 to your computer and use it in GitHub Desktop.
Print in terminal with colors using Python
#!/usr/bin/env python
# -*- coding:utf-8 -*-
'''
Author : myth
Date : 15-5-14
Email : belongmyth at 163.com
'''
Styles = dict(
list(zip(list(range(1, 9)),[
'bold',
'dark',
'',
'underline',
'blink',
'',
'reverse',
'concealed'
]
))
)
for k,v in Styles.items():
if v == '':
del Styles[k]
ForegroundColors = dict(
list(zip(list(range(30, 38)),[
'grey',
'red',
'green',
'yellow',
'blue',
'magenta',
'cyan',
'white',
]
))
)
BackgroundColors = dict(
list(zip(list(range(40, 48)),[
'on_grey',
'on_red',
'on_green',
'on_yellow',
'on_blue',
'on_magenta',
'on_cyan',
'on_white'
]
))
)
max_length = max([len(v) for v in Styles.values()]) + max([len(v) for v in ForegroundColors.values()]) + max([len(v) for v in BackgroundColors.values()])
def print_format_table():
"""
prints table of formatted text format options
"""
for style, styleMsg in Styles.items():
for fg, fgMsg in ForegroundColors.items():
s1 = ''
for bg, bgMsg in BackgroundColors.items():
format = ';'.join([str(style), str(fg), str(bg)])
formatMsg = '-'.join([styleMsg, fgMsg, bgMsg]).center(max_length+3)
s1 += '\x1b[%sm %s \x1b[0m' % (format, formatMsg)
print s1
print ''
print_format_table()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment