Ich graste den Code Django Quelle und ich sah diese Funktion:ANSI Grafikcodes und Python
def colorize(text='', opts=(), **kwargs):
"""
Returns your text, enclosed in ANSI graphics codes.
Depends on the keyword arguments 'fg' and 'bg', and the contents of
the opts tuple/list.
Returns the RESET code if no parameters are given.
Valid colors:
'black', 'red', 'green', 'yellow', 'blue', 'magenta', 'cyan', 'white'
Valid options:
'bold'
'underscore'
'blink'
'reverse'
'conceal'
'noreset' - string will not be auto-terminated with the RESET code
Examples:
colorize('hello', fg='red', bg='blue', opts=('blink',))
colorize()
colorize('goodbye', opts=('underscore',))
print colorize('first line', fg='red', opts=('noreset',))
print 'this should be red too'
print colorize('and so should this')
print 'this should not be red'
"""
code_list = []
if text == '' and len(opts) == 1 and opts[0] == 'reset':
return '\x1b[%sm' % RESET
for k, v in kwargs.iteritems():
if k == 'fg':
code_list.append(foreground[v])
elif k == 'bg':
code_list.append(background[v])
for o in opts:
if o in opt_dict:
code_list.append(opt_dict[o])
if 'noreset' not in opts:
text = text + '\x1b[%sm' % RESET
return ('\x1b[%sm' % ';'.join(code_list)) + text
ich es aus dem Kontext herausgenommen und in einer anderen Datei platziert einfach, es zu versuchen, die Sache ist, dass es scheint den Text nicht zu färben, den ich übergebe. Es kann sein, dass ich es nicht richtig verstehe, aber es soll nicht nur den Text umgeben mit ANSI-Grafikcodes zurückgeben, die das Terminal in tatsächliche Farben umwandeln wird.
Ich habe versucht, alle angegebenen Beispiele es zu nennen, aber es sind gerade das Argument ich als Text angegeben.
Ich benutze Ubuntu, also denke ich, dass das Terminal Farben unterstützen sollte.
Haben Sie daran gedacht "Vordergrund", "Hintergrund" und "opt_dict" kopieren? Auch "Flüche". –
Ja ich tat, ich werde in Flüche schauen, danke :) – gmunk