Tôi đang sử dụng Python 2's cmd module để tạo dòng lệnh cho chương trình. Mọi thứ hoạt động độc đáo miễn là tôi không thêm màu vào lời nhắc của tôi.Một số ký tự dính vào lời nhắc được tô màu của tôi bằng Python cmd
đang làm việc:
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()
Khi tôi thay đổi mã của tôi như dưới đây, nếu tôi nhập một lệnh dài hoặc cố gắng tìm kiếm trong lịch sử lệnh, một số nhân vật dính vào cửa sổ của tôi.
đang Vấn đề:
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()
Bạn có thể thấy vấn đề này trong asciicasts. Vấn đề cũng tồn tại với cmd2 module.
@cxw Có, Đó là Python2 và 'cmd' nhưng vấn đề này tồn tại trong' cmd2' quá! – RaminNietzsche