【发布时间】:2017-09-04 08:56:24
【问题描述】:
我正在使用Python 2's cmd module 为程序创建命令行。只要我不为提示添加颜色,一切都会很好地工作。
工作代码:
from cmd import Cmd
class App(Cmd):
def __init__(self):
Cmd.__init__(self)
self.prompt = "PG ["+ (str('username'), 'green') +"@"+ str('hostname') +"]: "
def do_exit(self, line):
'''
'''
return True
App().cmdloop()
当我如下更改我的代码时,如果我输入一个长命令或尝试在命令历史记录中搜索,一些字符会粘在我的提示符上。
问题代码:
from cmd import Cmd
class App(Cmd):
def __init__(self):
Cmd.__init__(self)
self.prompt = "PG ["+ self.colorize(str('username'), 'green') +"@"+ str('hostname') +"]: "
colorcodes = {'green':{True:'\x1b[32m',False:'\x1b[39m'}}
def colorize(self, val, color):
return self.colorcodes[color][True] + val + self.colorcodes[color][False]
def do_exit(self, line):
'''
'''
return True
App().cmdloop()
你可以在asciicasts 看到这个问题。 cmd2 module也存在这个问题。
【问题讨论】:
-
@cxw 是的,它是 Python2 和
cmd但这个问题也存在于cmd2中!
标签: python colors command-line-interface python-2.x readline