raw_input() support added.

There's now a fake stdin object instantiated when the curses interface loads
which hooks in to the interface and calls a get_key method on it (some
refactoring done to make this work cleanly). I've only provided a readline()
method on this object (write, writelines and read are there too but no-ops) as
I can't think of any other use for reading from stdin in the interactive
interpreter.
This commit is contained in:
Bob Farrell
2008-08-24 23:38:48 +01:00
parent 3020d488a1
commit b50a6adcbf
2 changed files with 49 additions and 4 deletions
+2
View File
@@ -10,6 +10,8 @@ Jørgen Tjernø has done lots of cool things like write a manpage and .desktop
file and improved the way tabbing works and also added home, end and del key
handling as well as C-w for deleting words - thanks a lot!
raw_input() and all its friends now work fine.
v0.6.4
======
KeyboardInterrupt handler clears the list window properly now.
+47 -4
View File
@@ -44,6 +44,7 @@ import fcntl
import string
import shlex
import pydoc
import cStringIO
# These are used for syntax hilighting.
from pygments import highlight
@@ -58,9 +59,43 @@ from pyparsing import Forward, Suppress, QuotedString, dblQuotedString, \
class Struct( object ):
pass # When we inherit, a __dict__ is added (object uses slots)
class FakeStdin(object):
"""Provide a fake stdin type for things like raw_input() etc."""
def __init__(self, interface):
"""Take the curses Repl on init and assume it provides a get_key method
which, fortunately, it does."""
self.interface = interface
def readline(self):
"""I can't think of any reason why anything other than readline would
be useful in the context of an interactive interpreter so this is the
only one I've done anything with. The others are just there in case
someone does something weird to stop it from blowing up."""
buffer = ''
while True:
key = self.interface.get_key()
sys.stdout.write(key)
# Include the \n in the buffer - raw_input() seems to deal with trailing
# linebreaks and will break if it gets an empty string.
buffer += key
if key == '\n':
break
return buffer
def read(self, x):
pass
def readlines(self, x):
pass
OPTS = Struct()
DO_RESIZE = False
# Set default values. (Overridden by loadrc())
OPTS.tab_length = 4
OPTS.auto_display_list = True
@@ -206,6 +241,7 @@ class Repl( object ):
self.s = ''
self.list_win_visible = False
self._C = {}
sys.stdin = FakeStdin(self)
if not OPTS.arg_spec:
return
@@ -777,6 +813,7 @@ class Repl( object ):
try:
inp = self.get_line()
except KeyboardInterrupt:
sys.exit() # DELETE ME !!!
self.statusbar.message('KeyboardInterrupt')
self.scr.addstr('\n')
self.scr.touchwin()
@@ -970,7 +1007,6 @@ class Repl( object ):
def p_key( self ):
"""Process a keypress"""
if self.c is None:
return ''
@@ -1198,11 +1234,18 @@ class Repl( object ):
self.c = None
self.cpos = 0
while True:
self.c = self.get_key()
if self.p_key() is None:
return self.s
def get_key(self):
while True:
if self.idle:
self.idle( self )
try:
self.c = self.scr.getkey()
key = self.scr.getkey()
except curses.error: # I'm quite annoyed with the ambiguity of
# this exception handler. I previously caught "curses.error, x" and accessed
# x.message and checked that it was "no input", which seemed a crappy way of
@@ -1210,9 +1253,9 @@ class Repl( object ):
# seems to have entirely different attributes. So let's hope getkey() doesn't
# raise any other crazy curses exceptions. :)
continue
else:
return key
if self.p_key() is None:
return self.s
class Statusbar( object ):
"""This class provides the status bar at the bottom of the screen.