[Tutor] command history in a console

Hans Dushanthakumar hansd at g2microsystems.com
Thu Sep 18 04:14:27 CEST 2008


G'day everyone.

I'm experimenting with a custom console application, and trying to add 
command history functionality to it. It seems to basically work ok 
except for the fact that when I press the Up arrow key to run previous 
commands, the right commands are not displayed. It displays a wierd 
"^[[A" for each Up arrow key that I press. Inspite of this, when I hit 
enter, it DOES seem to run the right command.

Heres a bit of code (stripped down to the bare essentials to demonstrate 
just this problem) that shows this behaviour:

------X Code starts here

import readline
import select
import sys
import os

historyPath = ".pyhistory.test"

if os.path.exists(historyPath):
    readline.read_history_file(historyPath)

inputs = [sys.stdin]   #More on this later.

while 1:
    in_ready, out_ready, ex_ready = select.select(inputs, [], [], 10)   
#More on this later.
    x= raw_input()
    print "You entered: ", x

----X End of code

Heres what an example run of the program looks like, with my comments:

hansd at zilla:~/tools$ python test_history1.py
ls <--------------------------------------Me: I entered "ls" here. So, 
this is the 1st command.
ls
You entered:  ls
1 <---------------------------------------Me: 2nd command "1"
1
You entered:  1
2 <---------------------------------------Me: 3rd command "2"
2
You entered:  2
^[[A^[[A <--------------------------Me: This is the strange characters. 
I pressed the Up key twice here.
1
You entered:  1 <----------------Me: So it looks like it received the 
right command from history :"1".


So, my question is, why does it not display the right command from 
history instead of those "^[[A" characters?

It works fine if I comment out the line "in_ready, out_ready, ex_ready = 
select.select(inputs, [], [], 10)". But there is a reason why I need 
this to be there..I'm expecting input from more sources than just the 
keyboard.

Thanks,
Hans


More information about the Tutor mailing list