Skip to content

Commit

Permalink
Fix handling of a blank prompt
Browse files Browse the repository at this point in the history
Make sure prompt is correctly setup even if the prompt is literally a blank string, as could be set using input()
  • Loading branch information
TheMatt2 committed Jun 9, 2024
1 parent be39446 commit 50bb0fd
Show file tree
Hide file tree
Showing 2 changed files with 22 additions and 1 deletion.
14 changes: 13 additions & 1 deletion qtconsole/console_widget.py
Original file line number Diff line number Diff line change
Expand Up @@ -2519,6 +2519,9 @@ def _show_prompt(self, prompt=None, html=False, newline=True,

cursor = self._get_end_cursor()

# Memorize end to check if we actually add any prompt characters
prior_end_pos = cursor.position()

# Save the current position to support _append*(before_prompt=True).
# We can't leave the cursor at the end of the document though, because
# that would cause any further additions to move the cursor. Therefore,
Expand Down Expand Up @@ -2557,7 +2560,16 @@ def _show_prompt(self, prompt=None, html=False, newline=True,
self._prompt_html = None

self._flush_pending_stream()
self._prompt_cursor.setPosition(self._get_end_pos() - 1)

current_end_pos = self._get_end_pos()
if prior_end_pos != current_end_pos:
# Set the prompt cursor to end minus 1, so long as
# the prompt was not blank
self._prompt_cursor.setPosition(current_end_pos - 1)
else:
# The prompt didn't move end, i.e. the prompt inserted exactly 0 characters
# Move cursor to end
self._prompt_cursor.setPosition(current_end_pos)

if move_forward:
self._append_before_prompt_cursor.setPosition(
Expand Down
9 changes: 9 additions & 0 deletions qtconsole/tests/test_00_console_widget.py
Original file line number Diff line number Diff line change
Expand Up @@ -491,6 +491,15 @@ def test_prompt_cursors(self):
self.assertEqual(w._append_before_prompt_pos,
w._prompt_pos - len(w._prompt))

# Test a blank prompt. Such as from input()
w._append_plain_text('\n')
w._show_prompt(prompt = '', separator = False)

w._append_plain_text('plain text\n')

self.assertEqual(w._prompt_pos, w._get_end_pos())
self.assertEqual(w._append_before_prompt_pos, w._prompt_pos)

def test_select_all(self):
w = ConsoleWidget()
w._append_plain_text('Header\n')
Expand Down

0 comments on commit 50bb0fd

Please sign in to comment.