Newbie: raw_input and other input methods

M.-A. Lemburg mal at lemburg.com
Fri Nov 26 18:21:41 CET 1999


Carel Fellinger wrote:
> 
> Hassan Aurag <aurag at geocities.com> wrote:
> 
> > Anyway, I used raw_input to be able to use readline and its nifty back
> > arrow, up arrow...
> 
> > However when I wanted to have a nifty prompt like the following one:
> 
> > raw_input("\033[0;34mInput: \033[0m"), I was surprised it got all
> > screwed up.
> 
> bash copes with this, so I dived into the source. And low and behold
> there it is: bash uses the readline special codes RL_PROMPT_START_IGNORE
> (='\001') and RL_PROMPT_END_IGNORE (='\002') to tell readline to 'ignore'
> the chars in between.  Using this info you can get what you want:)
> 
> e.g.
> >>>raw_input('\001\033[0:34m\002Input: \001\033[0m\002')
> 
> works like a charm, atleast here on my linux box

It even does the Right Thing for the Python prompt. Try this:

>>> sys.ps1 = '\001\033[0:34m\002>>> \001\033[0m\002'
>>> sys.ps2 = '\001\033[0:31m\002... \001\033[0m\002'
>>> if 1:
...     print 'looks cool :-)'
... 
looks cool :-)
>>>

The above adds a little color to your average Python prompt.

-- 
Marc-Andre Lemburg
______________________________________________________________________
Y2000:                                                    35 days left
Business:                                      http://www.lemburg.com/
Python Pages:                           http://www.lemburg.com/python/






More information about the Python-list mailing list