2010-11-18 27 views
2

Django kaynak kodunu geziyordum ve bu işlevi gördü:ANSI grafik kodlar ve 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 

ben bağlam dışına çıkarıldı ve sadece denemek için başka bir dosyaya yerleştirilir şey bu kadar mı ilettiğim metni renklendirmiyor gibi görünüyor. Doğru anlamadım ama terminalin gerçek renklere dönüştüğü ANSI grafik kodları ile çevrelenmiş olan metni geri göndermemesi gerekiyordu.

Tüm çağrı örneklerini denedim, ancak yalnızca metin olarak belirttiğim argümanı döndürdüm.

Ubuntu kullanıyorum, bu yüzden terminalin renkleri desteklemesi gerektiğini düşünüyorum.

+0

Hatırlarsanız mü 'önalan', 'arka plan' ve 'opt_dict' kopyalamak için? Ayrıca, "küfürler". –

+0

Evet yaptım, küfürlere bakacağım, teşekkürler :) – gmunk

cevap

1

Bu, tanımlanmamış birçok terime sahip olduğunuzdan, işlev dışında tanımlanmış birçok değişkene dayanıyor.

yerine sadece

import django.utils.termcolors as termcolors 
red_hello = termcolors.colorize("Hello", fg='red') # '\x1b[31mHello\x1b[0m' 
print red_hello 

Ya da sadece, aynı zamanda Django/utils/termcolors.py özellikle ilk birkaç satır kopyalayın:

Ayrıca
color_names = ('black', 'red', 'green', 'yellow', 'blue', 'magenta', 'cyan', 'white') 
foreground = dict([(color_names[x], '3%s' % x) for x in range(8)]) 
background = dict([(color_names[x], '4%s' % x) for x in range(8)]) 
RESET = '0' 

def colorize(...): 
    ... 
print colorize("Hello", fg='red') # '\x1b[31mHello\x1b[0m' 

not:

>>> from django.utils.termcolors import colorize 
>>> red_hello = colorize("Hello", fg="red") 
>>> red_hello # by not printing; it will not appear red; special characters are escaped 
'\x1b[31mHello\x1b[0m' 
>>> print red_hello # by print it will appear red; special characters are not escaped 
Hello 
+0

Yorumunuz için teşekkürler, bu satırlarla işlevi kopyaladım, işe yaramıyor. Belki yanlış bir şey yapıyorum, ne olacağını görmek için küfürler de deneyeceğim. Alkışlar – gmunk

+0

@gmunk Son düzenlemeyi fark ettiniz mi? (Örn., Dizeyi hala mevcut olan dize tırnaklarıyla görüntülemenin tersine dizgeyi 'yazdırıyorsunuz'). –

+0

ANSI metin terminali kontrollerini kaçış dizileri aracılığıyla desteklemek için konsolunuz kurulu mu, yoksa kurulu mu? – martineau

İlgili konular