Skip to content

Commit ed4ddc8

Browse files
committed
Preserve cursor position and _insert_plain_text()
Preserve position of cursor so ANSI commands such as \r that modify position will preserve the position they specifed over multiple calls, even if these calls are not all combined into one.
1 parent 56e5a5e commit ed4ddc8

File tree

1 file changed

+5
-8
lines changed

1 file changed

+5
-8
lines changed

qtconsole/console_widget.py

+5-8
Original file line numberDiff line numberDiff line change
@@ -1006,7 +1006,6 @@ def _append_custom(self, insert, input, before_prompt=False, *args, **kwargs):
10061006
else:
10071007
if insert != self._insert_plain_text:
10081008
self._flush_pending_stream()
1009-
cursor.movePosition(QtGui.QTextCursor.End)
10101009

10111010
# Perform the insertion.
10121011
result = insert(cursor, input, *args, **kwargs)
@@ -1660,10 +1659,7 @@ def _on_flush_pending_stream_timer(self):
16601659
""" Flush the pending stream output and change the
16611660
prompt position appropriately.
16621661
"""
1663-
cursor = self._control.textCursor()
1664-
cursor.movePosition(QtGui.QTextCursor.End)
16651662
self._flush_pending_stream()
1666-
cursor.movePosition(QtGui.QTextCursor.End)
16671663

16681664
def _flush_pending_stream(self):
16691665
""" Flush out pending text into the widget. """
@@ -1674,7 +1670,7 @@ def _flush_pending_stream(self):
16741670
text = self._get_last_lines_from_list(text, buffer_size)
16751671
text = ''.join(text)
16761672
t = time.time()
1677-
self._insert_plain_text(self._get_end_cursor(), text, flush=True)
1673+
self._insert_plain_text(self._control.textCursor(), text, flush=True)
16781674
# Set the flush interval to equal the maximum time to update text.
16791675
self._pending_text_flush_interval.setInterval(
16801676
int(max(100, (time.time() - t) * 1000))
@@ -2123,7 +2119,7 @@ def _insert_plain_text(self, cursor, text, flush=False):
21232119
cursor.select(QtGui.QTextCursor.Document)
21242120
remove = True
21252121
if act.area == 'line':
2126-
if act.erase_to == 'all':
2122+
if act.erase_to == 'all':
21272123
cursor.select(QtGui.QTextCursor.LineUnderCursor)
21282124
remove = True
21292125
elif act.erase_to == 'start':
@@ -2137,7 +2133,7 @@ def _insert_plain_text(self, cursor, text, flush=False):
21372133
QtGui.QTextCursor.EndOfLine,
21382134
QtGui.QTextCursor.KeepAnchor)
21392135
remove = True
2140-
if remove:
2136+
if remove:
21412137
nspace=cursor.selectionEnd()-cursor.selectionStart() if fill else 0
21422138
cursor.removeSelectedText()
21432139
if nspace>0: cursor.insertText(' '*nspace) # replace text by space, to keep cursor position as specified
@@ -2181,11 +2177,12 @@ def _insert_plain_text(self, cursor, text, flush=False):
21812177
remain = cursor2.position() - pos # number of characters until end of line
21822178
n=len(substring)
21832179
swallow = min(n, remain) # number of character to swallow
2184-
cursor.setPosition(pos+swallow,QtGui.QTextCursor.KeepAnchor)
2180+
cursor.setPosition(pos + swallow, QtGui.QTextCursor.KeepAnchor)
21852181
cursor.insertText(substring,format)
21862182
else:
21872183
cursor.insertText(text)
21882184
cursor.endEditBlock()
2185+
self._control.setTextCursor(cursor)
21892186

21902187
if should_autoscroll:
21912188
self._scroll_to_end()

0 commit comments

Comments
 (0)