Skip to content

Commit c614840

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 c614840

File tree

1 file changed

+8
-8
lines changed

1 file changed

+8
-8
lines changed

qtconsole/console_widget.py

+8-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))
@@ -2086,6 +2082,9 @@ def _insert_plain_text(self, cursor, text, flush=False):
20862082
""" Inserts plain text using the specified cursor, processing ANSI codes
20872083
if enabled.
20882084
"""
2085+
if not text and not flush:
2086+
return
2087+
20892088
should_autoscroll = self._viewport_at_end()
20902089
# maximumBlockCount() can be different from self.buffer_size in
20912090
# case input prompt is active.
@@ -2123,7 +2122,7 @@ def _insert_plain_text(self, cursor, text, flush=False):
21232122
cursor.select(QtGui.QTextCursor.Document)
21242123
remove = True
21252124
if act.area == 'line':
2126-
if act.erase_to == 'all':
2125+
if act.erase_to == 'all':
21272126
cursor.select(QtGui.QTextCursor.LineUnderCursor)
21282127
remove = True
21292128
elif act.erase_to == 'start':
@@ -2137,7 +2136,7 @@ def _insert_plain_text(self, cursor, text, flush=False):
21372136
QtGui.QTextCursor.EndOfLine,
21382137
QtGui.QTextCursor.KeepAnchor)
21392138
remove = True
2140-
if remove:
2139+
if remove:
21412140
nspace=cursor.selectionEnd()-cursor.selectionStart() if fill else 0
21422141
cursor.removeSelectedText()
21432142
if nspace>0: cursor.insertText(' '*nspace) # replace text by space, to keep cursor position as specified
@@ -2181,11 +2180,12 @@ def _insert_plain_text(self, cursor, text, flush=False):
21812180
remain = cursor2.position() - pos # number of characters until end of line
21822181
n=len(substring)
21832182
swallow = min(n, remain) # number of character to swallow
2184-
cursor.setPosition(pos+swallow,QtGui.QTextCursor.KeepAnchor)
2183+
cursor.setPosition(pos + swallow, QtGui.QTextCursor.KeepAnchor)
21852184
cursor.insertText(substring,format)
21862185
else:
21872186
cursor.insertText(text)
21882187
cursor.endEditBlock()
2188+
self._control.setTextCursor(cursor)
21892189

21902190
if should_autoscroll:
21912191
self._scroll_to_end()

0 commit comments

Comments
 (0)