[issue12972] Color prompt + readline

Damian report at bugs.python.org
Tue Sep 13 17:29:32 CEST 2011

New submission from Damian <atagar1 at gmail.com>:

Hi, when using terminal coloring codes (for instance '\x1b[32mhello world\x1b[0m' for a green 'hello world') the raw_input function and readline module behave well except under a very specific use case...


import readline # provides history via up/down

prompt = '\x1b[32m>>> \x1b[0m' # green '>>> ' prompt

while True:


This provides a green prompt and up/down cycles through prior input. This works well as long as the input is shorter than the prompt string length (in this case 13 characters). However, if the input is longer than the prompt then up/down thinks that the first thirteen rendered characters now belong to the prompt. For instance...

atagar at fenrir:~/Desktop/arm$ python tmp.py 
>>> http://docs.python.org/library/readline

Press up, then down to get back to a blank prompt. You'll have...
>>> http://do

This is probably due to a len() check on the raw_input argument...
>>> len('>>> http://do')
>>> len('\x1b[32m>>> \x1b[0m')

I'm at a bit of a loss for investigating this further - help would be appreciated! -Damian

messages: 143977
nosy: atagar1
priority: normal
severity: normal
status: open
title: Color prompt + readline
type: behavior
versions: Python 2.6

Python tracker <report at bugs.python.org>

More information about the Python-bugs-list mailing list