Left and right arrow keys work

This commit is contained in:
Kovid Goyal 2010-09-20 14:24:42 -06:00
parent 111c73ab80
commit f770aa43bb

View File

@ -45,18 +45,30 @@ class Console(QTextEdit):
@property @property
def cursor_pos(self): def cursor_pos(self):
pass '''
#pos = self.cursor.position() - self.prompt_frame.firstPosition() Return cursor position in prompt frame as (row, col).
#i = 0 row starts at 0 for the first line
#for line in self.current_prompt: col is 0 if the cursor is at the start of the line, 1 if it is after
# i += self.prompt_len the first character, n if it is after the nth char.
'''
if self.prompt_frame is not None:
pos = self.cursor.position()
it = self.prompt_frame.begin()
lineno = 0
while not it.atEnd():
bl = it.currentBlock()
if bl.contains(pos):
return (lineno, pos - bl.position())
it += 1
lineno += 1
return (-1, -1)
def __init__(self, def __init__(self,
prompt='>>> ', prompt='>>> ',
continuation='... ', continuation='... ',
parent=None): parent=None):
QTextEdit.__init__(self, parent) QTextEdit.__init__(self, parent)
self.buf = '' self.buf = []
self.prompt_frame = None self.prompt_frame = None
self.allow_output = False self.allow_output = False
self.prompt_frame_format = QTextFrameFormat() self.prompt_frame_format = QTextFrameFormat()
@ -130,7 +142,6 @@ class Console(QTextEdit):
# }}} # }}}
# Non-prompt Rendering {{{ # Non-prompt Rendering {{{
def render_block(self, text, restore_prompt=True): def render_block(self, text, restore_prompt=True):
@ -143,26 +154,25 @@ class Console(QTextEdit):
def show_error(self, is_syntax_err, tb): def show_error(self, is_syntax_err, tb):
if self.prompt_frame is not None: if self.prompt_frame is not None:
# At a prompt, so redirect output # At a prompt, so redirect output
return prints(tb) return prints(tb, end='')
try: try:
self.buf += tb self.buf.append(tb)
if is_syntax_err: if is_syntax_err:
self.formatter.render_syntax_error(tb, self.cursor) self.formatter.render_syntax_error(tb, self.cursor)
else: else:
self.formatter.render(self.tb_lexer.get_tokens(tb), self.cursor) self.formatter.render(self.tb_lexer.get_tokens(tb), self.cursor)
except: except:
prints(tb) prints(tb, end='')
def show_output(self, raw): def show_output(self, raw):
if self.prompt_frame is not None: if self.prompt_frame is not None:
# At a prompt, so redirect output # At a prompt, so redirect output
return prints(raw) return prints(raw, end='')
try: try:
self.current_prompt_range = None self.buf.append(raw)
self.buf += raw
self.formatter.render_raw(raw, self.cursor) self.formatter.render_raw(raw, self.cursor)
except: except:
prints(raw) prints(raw, end='')
# }}} # }}}
@ -187,13 +197,29 @@ class Console(QTextEdit):
QTextEdit.keyPressEvent(self, ev) QTextEdit.keyPressEvent(self, ev)
def left_pressed(self): def left_pressed(self):
pass lineno, pos = self.cursor_pos
if lineno < 0: return
if pos > self.prompt_len:
c = self.cursor
c.movePosition(c.PreviousCharacter)
self.setTextCursor(c)
elif lineno > 0:
c = self.cursor
c.movePosition(c.Up)
c.movePosition(c.EndOfLine)
self.setTextCursor(c)
def right_pressed(self): def right_pressed(self):
if self.prompt_frame is not None: lineno, pos = self.cursor_pos
c = self.cursor if lineno < 0: return
c = self.cursor
lineno, pos = self.cursor_pos
cp = list(self.prompt(False))
if pos < len(cp[lineno]):
c.movePosition(c.NextCharacter) c.movePosition(c.NextCharacter)
self.setTextCursor(c) elif lineno < len(cp)-1:
c.movePosition(c.NextCharacter, n=1+self.prompt_len)
self.setTextCursor(c)
def home_pressed(self): def home_pressed(self):
if self.prompt_frame is not None: if self.prompt_frame is not None:
@ -218,7 +244,7 @@ class Console(QTextEdit):
old_pf = self.prompt_frame old_pf = self.prompt_frame
self.prompt_frame = None self.prompt_frame = None
oldbuf = self.buf oldbuf = self.buf
self.buf = '' self.buf = []
ret = self.interpreter.runsource('\n'.join(cp)) ret = self.interpreter.runsource('\n'.join(cp))
if ret: # Incomplete command if ret: # Incomplete command
self.buf = oldbuf self.buf = oldbuf