title: Some characters stick to my colorized prompt in Python cmd
body: <p>I am using <a href="https://docs.python.org/2/library/cmd.html" rel="noreferrer">Python 2's cmd module</a> to make a command line for a program.  Everything works nicely as long as I don't add color to my prompt.</p>
  
  <p>Working code:</p>
  
  <pre><code>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()
  </code></pre>
  
  <p>When I change my code as below, if I enter a long command or try to search in the command history, some characters stick to my prompt.</p>
  
  <p>Problem code:</p>
  
  <pre><code>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()
  </code></pre>
  
  <p>You can see this problem in <a href="https://asciinema.org/a/3phlotyufl6bl8hink8upxo7b" rel="noreferrer">asciicasts</a>.  The problem also exists with the <a href="https://pypi.python.org/pypi/cmd2/0.7.0" rel="noreferrer">cmd2 module</a>.</p>