Skip to content

Commit

Permalink
Preserve cursor position and _insert_plain_text()
Browse files Browse the repository at this point in the history
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.
  • Loading branch information
TheMatt2 committed May 27, 2024
1 parent 56e5a5e commit c614840
Showing 1 changed file with 8 additions and 8 deletions.
16 changes: 8 additions & 8 deletions qtconsole/console_widget.py
Original file line number Diff line number Diff line change
Expand Up @@ -1006,7 +1006,6 @@ def _append_custom(self, insert, input, before_prompt=False, *args, **kwargs):
else:
if insert != self._insert_plain_text:
self._flush_pending_stream()
cursor.movePosition(QtGui.QTextCursor.End)

# Perform the insertion.
result = insert(cursor, input, *args, **kwargs)
Expand Down Expand Up @@ -1660,10 +1659,7 @@ def _on_flush_pending_stream_timer(self):
""" Flush the pending stream output and change the
prompt position appropriately.
"""
cursor = self._control.textCursor()
cursor.movePosition(QtGui.QTextCursor.End)
self._flush_pending_stream()
cursor.movePosition(QtGui.QTextCursor.End)

def _flush_pending_stream(self):
""" Flush out pending text into the widget. """
Expand All @@ -1674,7 +1670,7 @@ def _flush_pending_stream(self):
text = self._get_last_lines_from_list(text, buffer_size)
text = ''.join(text)
t = time.time()
self._insert_plain_text(self._get_end_cursor(), text, flush=True)
self._insert_plain_text(self._control.textCursor(), text, flush=True)
# Set the flush interval to equal the maximum time to update text.
self._pending_text_flush_interval.setInterval(
int(max(100, (time.time() - t) * 1000))
Expand Down Expand Up @@ -2086,6 +2082,9 @@ def _insert_plain_text(self, cursor, text, flush=False):
""" Inserts plain text using the specified cursor, processing ANSI codes
if enabled.
"""
if not text and not flush:
return

should_autoscroll = self._viewport_at_end()
# maximumBlockCount() can be different from self.buffer_size in
# case input prompt is active.
Expand Down Expand Up @@ -2123,7 +2122,7 @@ def _insert_plain_text(self, cursor, text, flush=False):
cursor.select(QtGui.QTextCursor.Document)
remove = True
if act.area == 'line':
if act.erase_to == 'all':
if act.erase_to == 'all':
cursor.select(QtGui.QTextCursor.LineUnderCursor)
remove = True
elif act.erase_to == 'start':
Expand All @@ -2137,7 +2136,7 @@ def _insert_plain_text(self, cursor, text, flush=False):
QtGui.QTextCursor.EndOfLine,
QtGui.QTextCursor.KeepAnchor)
remove = True
if remove:
if remove:
nspace=cursor.selectionEnd()-cursor.selectionStart() if fill else 0
cursor.removeSelectedText()
if nspace>0: cursor.insertText(' '*nspace) # replace text by space, to keep cursor position as specified
Expand Down Expand Up @@ -2181,11 +2180,12 @@ def _insert_plain_text(self, cursor, text, flush=False):
remain = cursor2.position() - pos # number of characters until end of line
n=len(substring)
swallow = min(n, remain) # number of character to swallow
cursor.setPosition(pos+swallow,QtGui.QTextCursor.KeepAnchor)
cursor.setPosition(pos + swallow, QtGui.QTextCursor.KeepAnchor)
cursor.insertText(substring,format)
else:
cursor.insertText(text)
cursor.endEditBlock()
self._control.setTextCursor(cursor)

if should_autoscroll:
self._scroll_to_end()
Expand Down

0 comments on commit c614840

Please sign in to comment.