我正在使用Python 2的cmd模块为程序创建命令行.只要我不在我的提示中添加颜色,一切都很好用.
工作代码:
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()
Run Code Online (Sandbox Code Playgroud)
当我更改下面的代码时,如果我输入一个长命令或尝试搜索命令历史记录,一些字符会坚持我的提示.
问题代码:
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()
Run Code Online (Sandbox Code Playgroud)
你可以在asciicasts中看到这个问题.cmd2模块也存在问题.
是否可以将“在控制台中执行选择”的快捷方式从alt+ shift+ e更改为command+ enter,类似于RStudio?