Skip to content

Commit

Permalink
Add fix for transition from insert before prompt to insert after prompt.
Browse files Browse the repository at this point in the history
And added test case.
  • Loading branch information
TheMatt2 committed Jun 9, 2024
1 parent ad8b77c commit be39446
Show file tree
Hide file tree
Showing 2 changed files with 27 additions and 29 deletions.
5 changes: 4 additions & 1 deletion qtconsole/console_widget.py
Original file line number Diff line number Diff line change
Expand Up @@ -1028,7 +1028,7 @@ def _append_custom(self, insert, input, before_prompt=False, *args, **kwargs):
# Insert at current printing point
# If cursor is before prompt jump to end, but only if there
# is a prompt (before_prompt_pos != end)
if cursor.position() < self._append_before_prompt_pos \
if cursor.position() <= self._append_before_prompt_pos \
and self._append_before_prompt_pos != self._get_end_pos():
cursor.movePosition(QtGui.QTextCursor.End)

Expand Down Expand Up @@ -2562,6 +2562,9 @@ def _show_prompt(self, prompt=None, html=False, newline=True,
if move_forward:
self._append_before_prompt_cursor.setPosition(
self._append_before_prompt_cursor.position() + 1)
else:
# cursor position was 0, set before prompt cursor
self._append_before_prompt_cursor.setPosition(0)
self._prompt_started()

#------ Signal handlers ----------------------------------------------------
Expand Down
51 changes: 23 additions & 28 deletions qtconsole/tests/test_00_console_widget.py
Original file line number Diff line number Diff line change
Expand Up @@ -371,48 +371,43 @@ def test_erase_in_line(self):
# clear all the text
cursor.insertText('')

def test_print_while_executing(self):
""" Does overwriting the currentt line with carriage return work?
def test_print_carriage_return(self):
""" Test that overwriting the current line works as intended,
before and after the cursor prompt.
"""
w = ConsoleWidget()
test_inputs = ['Hello\n',
'World\r',
'*' * 10,
'\r',

# Show a prompt
w._prompt = "prompt>"
w._prompt_sep = "\n"

w._show_prompt()
self.assert_text_equal(w._get_cursor(), '\u2029prompt>')

test_inputs = ['Hello\n', 'World\r',
'*' * 10, '\r',
'0', '1', '2', '3', '4',
'5', '6', '7', '8', '9',
'\r\n']

expected_output = "Hello\u20290123456789\u2029"

w._executing = True
for text in test_inputs:
w._append_plain_text(text, before_prompt = True)
w._flush_pending_stream() # emulate text being flushed

cursor = w._get_cursor()
self.assert_text_equal(cursor, expected_output)

def test_print_while_reading(self):
""" Does overwriting the currentt line with carriage return work?
"""
w = ConsoleWidget()
test_inputs = ['Hello\n',
'World\r',
'*' * 10,
'\r',
'0', '1', '2', '3', '4',
'5', '6', '7', '8', '9',
'\r\n']
self.assert_text_equal(w._get_cursor(),
"Hello\u20290123456789\u2029\u2029prompt>")

expected_output = "Hello\u20290123456789\u2029"
# Print after prompt
w._executing = True
test_inputs = ['\nF', 'o', 'o',
'\r', 'Bar', '\n']

for text in test_inputs:
w._append_plain_text(text, before_prompt = True)
w._append_plain_text(text, before_prompt = False)
w._flush_pending_stream() # emulate text being flushed

cursor = w._get_cursor()
self.assert_text_equal(cursor, expected_output)
self.assert_text_equal(w._get_cursor(),
"Hello\u20290123456789\u2029\u2029prompt>\u2029Bar\u2029")

def test_link_handling(self):
noButton = QtCore.Qt.NoButton
Expand Down Expand Up @@ -491,7 +486,7 @@ def test_prompt_cursors(self):
w._prompt_pos - len(w._prompt))

# insert some text before the prompt
w._append_plain_text('line', before_prompt=True)
w._append_plain_text('line', before_prompt = True)
self.assertEqual(w._prompt_pos, w._get_end_pos())
self.assertEqual(w._append_before_prompt_pos,
w._prompt_pos - len(w._prompt))
Expand Down

0 comments on commit be39446

Please sign in to comment.