@ -1,8 +1,6 @@
"""
Code to handle colored text
"""
"""
Here is a little explanation about bash color code , useful to understand
the code below . See http : / / invisible - island . net / xterm / ctlseqs / ctlseqs . html
for a complete referece .
@ -25,6 +23,7 @@ by the bright version of the font; some terminals allow the user to decide that.
display colors , with 0 < = c < 8 corresponding to the 8 above colors , and
8 < = c < 16 their bright version .
"""
from __future__ import unicode_literals
import sys
import re
@ -32,15 +31,15 @@ import os
import subprocess
COLOR_LIST = { u ' black ' : ' 0 ' , u ' red ' : ' 1 ' , u ' green ' : ' 2 ' , u ' yellow ' : ' 3 ' , u ' blue ' : ' 4 ' ,
u ' magenta ' : ' 5 ' , u ' cyan ' : ' 6 ' , u ' grey ' : ' 7 ' ,
u ' brightblack ' : ' 8 ' , u ' brightred ' : ' 9 ' , u ' brightgreen ' : ' 10 ' ,
u ' brightyellow ' : ' 11 ' , u ' brightblue ' : ' 12 ' , u ' brightmagenta ' : ' 13 ' ,
u ' brightcyan ' : ' 14 ' , u ' brightgrey ' : ' 15 ' ,
u ' darkgrey ' : ' 8 ' , # == brightblack
u ' gray ' : ' 7 ' , u ' darkgray ' : ' 8 ' , u ' brightgray ' : ' 15 ' , # gray/grey spelling
u ' purple ' : ' 5 ' , # for compatibility reasons
u ' white ' : ' 15 ' # == brightgrey
COLOR_LIST = { ' black ' : ' 0 ' , ' red ' : ' 1 ' , ' green ' : ' 2 ' , ' yellow ' : ' 3 ' , ' blue ' : ' 4 ' ,
' magenta ' : ' 5 ' , ' cyan ' : ' 6 ' , ' grey ' : ' 7 ' ,
' brightblack ' : ' 8 ' , ' brightred ' : ' 9 ' , ' brightgreen ' : ' 10 ' ,
' brightyellow ' : ' 11 ' , ' brightblue ' : ' 12 ' , ' brightmagenta ' : ' 13 ' ,
' brightcyan ' : ' 14 ' , ' brightgrey ' : ' 15 ' ,
' darkgrey ' : ' 8 ' , # == brightblack
' gray ' : ' 7 ' , ' darkgray ' : ' 8 ' , ' brightgray ' : ' 15 ' , # gray/grey spelling
' purple ' : ' 5 ' , # for compatibility reasons
' white ' : ' 15 ' # == brightgrey
}
for c in range ( 256 ) :
COLOR_LIST [ str ( c ) ] = str ( c )
@ -74,43 +73,43 @@ def generate_colors(stream, color=True, bold=True, italic=True, force_colors=Fal
normal colors .
: param italic : generate italic colors
"""
colors = { u ' bold ' : u ' ' , u ' italic ' : u ' ' , u ' end ' : u ' ' , u ' ' : u ' ' }
colors = { ' bold ' : ' ' , ' italic ' : ' ' , ' end ' : ' ' , ' ' : ' ' }
for name , code in COLOR_LIST . items ( ) :
colors [ name ] = u ' '
colors [ u ' b ' + name ] = u ' '
colors [ u ' i ' + name ] = u ' '
colors [ u ' bi ' + name ] = u ' '
colors [ name ] = ' '
colors [ ' b ' + name ] = ' '
colors [ ' i ' + name ] = ' '
colors [ ' bi ' + name ] = ' '
color_support = _color_supported ( stream , force = force_colors ) > = 8
if ( color or bold or italic ) and color_support :
bold_flag , italic_flag = ' ' , ' '
if bold :
colors [ ' bold ' ] = u ' \033 [1m '
colors [ ' bold ' ] = ' \033 [1m '
bold_flag = ' 1; '
if italic :
colors [ ' italic ' ] = u ' \033 [3m '
colors [ ' italic ' ] = ' \033 [3m '
italic_flag = ' 3; '
if bold and italic :
colors [ ' bolditalic ' ] = u ' \033 [1;3m '
colors [ ' bolditalic ' ] = ' \033 [1;3m '
for name , code in COLOR_LIST . items ( ) :
if color :
colors [ name ] = u ' \033 [38;5; {} m ' . format ( code )
colors [ u ' b ' + name ] = u ' \033 [ {} 38;5; {} m ' . format ( bold_flag , code )
colors [ u ' i ' + name ] = u ' \033 [ {} 38;5; {} m ' . format ( italic_flag , code )
colors [ u ' bi ' + name ] = u ' \033 [ {} 38;5; {} m ' . format ( bold_flag , italic_flag , code )
colors [ name ] = ' \033 [38;5; {} m ' . format ( code )
colors [ ' b ' + name ] = ' \033 [ {} 38;5; {} m ' . format ( bold_flag , code )
colors [ ' i ' + name ] = ' \033 [ {} 38;5; {} m ' . format ( italic_flag , code )
colors [ ' bi ' + name ] = ' \033 [ {} 38;5; {} m ' . format ( bold_flag , italic_flag , code )
else :
if bold :
colors . update ( { u ' b ' + name : u ' \033 [1m ' for i , name in enumerate ( COLOR_LIST ) } )
colors . update ( { ' b ' + name : ' \033 [1m ' for i , name in enumerate ( COLOR_LIST ) } )
if italic :
colors . update ( { u ' i ' + name : u ' \033 [3m ' for i , name in enumerate ( COLOR_LIST ) } )
colors . update ( { ' i ' + name : ' \033 [3m ' for i , name in enumerate ( COLOR_LIST ) } )
if bold or italic :
colors . update ( { u ' bi ' + name : u ' \033 [ {} {} m ' . format ( bold_flag , italic_flag ) for i , name in enumerate ( COLOR_LIST ) } )
colors . update ( { ' bi ' + name : ' \033 [ {} {} m ' . format ( bold_flag , italic_flag ) for i , name in enumerate ( COLOR_LIST ) } )
if color or bold or italic :
colors [ u ' end ' ] = u ' \033 [0m '
colors [ ' end ' ] = ' \033 [0m '
return colors
@ -121,11 +120,11 @@ COLORS_ERR = generate_colors(sys.stderr, color=False, bold=False, italic=False)
def dye_out ( s , color = ' end ' ) :
""" Color a string for output on stdout """
return u ' {} {} {} ' . format ( COLORS_OUT [ color ] , s , COLORS_OUT [ ' end ' ] )
return ' {} {} {} ' . format ( COLORS_OUT [ color ] , s , COLORS_OUT [ ' end ' ] )
def dye_err ( s , color = ' end ' ) :
""" Color a string for output on stderr """
return u ' {} {} {} ' . format ( COLORS_ERR [ color ] , s , COLORS_OUT [ ' end ' ] )
return ' {} {} {} ' . format ( COLORS_ERR [ color ] , s , COLORS_OUT [ ' end ' ] )
def setup ( conf , force_colors = False ) :