Output a table with ANSI terminal font colors and styles ¶
#!/usr/bin/env python# -*- coding: utf-8 -*-"""
Color Table
~~~~~~~~~~~
Display a table of available console text formatting options (colors etc.)
using the escape character and `ANSI escape codes`_.
.. _ANSI escape codes: http://en.wikipedia.org/wiki/ANSI_escape_code
:Copyright: 2002-2008 Jochen Kupperschmidt
:Date: 18-Jun-2008
:License: MIT
"""COLORS=('black','red','green','yellow','blue','magenta','cyan','white')STYLES=(('foregr.',30),('backgr.',40),('bold',1),('faint',2),('italic',3),('underl.',4),('blink',5),('reverse',7))COLUMN_SPACE=' | 'ESC='\033[%sm'CELL_FORMAT=ESC+'%-7s'+(ESC%0)# 0 resetsdefgen_column(value):"""Generate a column with all color names, but the same style."""forindex,colorinenumerate(COLORS):ifvalue>=30:style='%d'%(index+value)else:style='%d;%d'%(30+index,value)yieldCELL_FORMAT%(style,color)defbuild_table():"""Generate table head and body rows."""# titlesyield(('%-7s'%style[0])forstyleinSTYLES)# separatorsyield['-'*7]*len(STYLES)# rowsforrowinzip(*(gen_column(style[1])forstyleinSTYLES)):yieldrowif__name__=='__main__':# print tableprintprint'\n'.join(map(COLUMN_SPACE.join,build_table()))print