This issue tracker has been migrated to GitHub, and is currently read-only.
For more information, see the GitHub FAQs in the Python's Developer Guide.

Title: Color prompt + readline
Type: behavior Stage: test needed
Components: Versions: Python 2.7
Status: closed Resolution: out of date
Dependencies: Superseder:
Assigned To: Nosy List: atagar, atagar1, terry.reedy
Priority: normal Keywords:

Created on 2011-09-13 15:29 by atagar1, last changed 2022-04-11 14:57 by admin. This issue is now closed.

Messages (6)
msg143977 - (view) Author: Damian (atagar1) Date: 2011-09-13 15:29
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@fenrir:~/Desktop/arm$ python 

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
msg144150 - (view) Author: Terry J. Reedy (terry.reedy) * (Python committer) Date: 2011-09-16 19:03
From your version choice, you apparently are using 2.6, which is in security-fix only mode. Please test with the latest 2.7 and/or 3.2.
msg144227 - (view) Author: Damian (atagar) Date: 2011-09-18 04:34
Retested with Python 3.1.1 and this issue doesn't manifest. This can be resolved - sorry about the noise. :)
msg144245 - (view) Author: Terry J. Reedy (terry.reedy) * (Python committer) Date: 2011-09-18 17:10
Since 2.7 was released after 3.1, I will assumed any bugfix was applied there also until someone determines otherwise. Thanks for checking.
msg219101 - (view) Author: Damian (atagar) Date: 2014-05-25 19:34
Just a quick comment that I ran into this again, but turns out that it's not an issue with python. Rather, this is a quirk with how readline works...

Color prompts need to be wrapped by RL_PROMPT_START_IGNORE and RL_PROMPT_END_IGNORE.
msg219106 - (view) Author: Terry J. Reedy (terry.reedy) * (Python committer) Date: 2014-05-25 21:45
Thanks for the followup. This should be useful info for anyone who finds this issue.
Date User Action Args
2022-04-11 14:57:21adminsetgithub: 57181
2014-05-25 21:45:07terry.reedysetmessages: + msg219106
2014-05-25 19:34:41atagarsetmessages: + msg219101
2011-09-18 17:10:26terry.reedysetstatus: open -> closed
resolution: out of date
messages: + msg144245
2011-09-18 04:34:48atagarsetnosy: + atagar
messages: + msg144227
2011-09-16 19:03:24terry.reedysetversions: + Python 2.7, - Python 2.6
nosy: + terry.reedy

messages: + msg144150

stage: test needed
2011-09-13 15:29:32atagar1create