我在浏览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
我把它从上下文中删除,放在另一个文件中只是为了尝试一下,问题是它似乎没有给我传递的文本着色。可能是我没能正确理解它,但它不应该只是返回用ANSI图形代码包围的文本,而这些代码将被终端转换成实际的颜色。在
我尝试了调用它的所有给定示例,但它只是返回我指定为文本的参数。在
我用的是Ubuntu,所以我觉得终端应该支持颜色。在
因为它依赖于函数外定义的几个变量,所以有很多术语未定义。在
相反,只是
或者只复制django/utils的前几行/术语颜色.py具体来说:
^{pr2}$另请注意:
相关问题 更多 >
编程相关推荐