简体   繁体   English

ANSI图形代码和Python

[英]ANSI graphic codes and Python

I was browsing the Django source code and I saw this function: 我正在浏览Django源代码,并且看到了以下功能:

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

I removed it out of the context and placed in another file just to try it, the thing is that it doesn't seem to colour the text I pass it. 我将其从上下文中删除,并放置在另一个文件中只是为了尝试它,但事实是它似乎并没有为我传递的文本着色。 It might be that I don't understand it correctly but isn't it supposed to just return the text surrounded with ANSI graphics codes which than the terminal will convert to actual colours. 可能是我不正确理解它,但难道它不应该只是返回被ANSI图形代码包围的文本,然后终端将其转换为实际颜色。

I tried all the given examples of calling it, but it just returned the argument I specified as a text. 我尝试了所有给定的调用它的示例,但是它只是返回了我指定为文本的参数。

I'm using Ubuntu so I think the terminal should support colours. 我正在使用Ubuntu,因此我认为终端应支持颜色。

It's that you have many terms undefined, because it relies on several variables defined outside of the function. 因为您有许多未定义的术语,因为它依赖于函数外部定义的多个变量。

Instead just 而是

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

Or just also copy the first few lines of django/utils/termcolors.py specifically: 或者只是也专门复制django / utils / termcolors.py的前几行:

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'

Also note: 另请注意:

>>> 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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM