Movatterモバイル変換


[0]ホーム

URL:


Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

gh-132267: fix desynchronized cursor position and buffer mismatch after resize#132360

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to ourterms of service andprivacy statement. We’ll occasionally send you account related emails.

Already on GitHub?Sign in to your account

Open
ImFeH2 wants to merge6 commits intopython:main
base:main
Choose a base branch
Loading
fromImFeH2:gh-132267
Open
Show file tree
Hide file tree
Changes fromall commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 5 additions & 1 deletionLib/_pyrepl/reader.py
View file
Open in desktop
Original file line numberDiff line numberDiff line change
Expand Up@@ -344,7 +344,9 @@ def calc_screen(self) -> list[str]:
pos -= line_len + 1
prompt, prompt_len = self.process_prompt(prompt)
chars, char_widths = disp_str(line, colors, offset)
wrapcount = (sum(char_widths) + prompt_len) // self.console.width
wrapcount = (sum(char_widths) + prompt_len) // (self.console.width - 1) # 1 for line continuations
if (sum(char_widths) + prompt_len) % (self.console.width - 1) == 0:
wrapcount -= 1
if wrapcount == 0 or not char_widths:
offset += line_len + 1 # Takes all of the line plus the newline
last_refresh_line_end_offsets.append(offset)
Expand DownExpand Up@@ -714,6 +716,8 @@ def handle1(self, block: bool = True) -> bool:
elif event.evt == "scroll":
self.refresh()
elif event.evt == "resize":
self.console.height, self.console.width = self.console.getheightwidth()
self.console.clear()
self.refresh()
else:
translate = False
Expand Down
11 changes: 8 additions & 3 deletionsLib/_pyrepl/unix_console.py
View file
Open in desktop
Original file line numberDiff line numberDiff line change
Expand Up@@ -271,8 +271,9 @@ def refresh(self, screen, c_xy):
self.posxy = 0, old_offset
for i in range(old_offset - offset):
self.__write_code(self._ri)
oldscr.pop(-1)
oldscr.insert(0, "")
if len(oldscr) > height:
oldscr.pop(-1)
elif old_offset < offset and self._ind:
self.__hide_cursor()
self.__write_code(self._cup, self.height - 1, 0)
Expand DownExpand Up@@ -683,13 +684,18 @@ def __write_changed_line(self, y, oldline, newline, px_coord):
self.__write(newline[x_pos])
self.posxy = character_width + 1, y

if newline[-1] != oldline[-1]:
self.__move(self.width, y)
self.__write(newline[-1])
self.posxy = self.width - 1, y

else:
self.__hide_cursor()
self.__move(x_coord, y)
if wlen(oldline) > wlen(newline):
self.__write_code(self._el)
self.__write(newline[x_pos:])
self.posxy = wlen(newline), y
self.posxy =min(wlen(newline), self.width - 1), y

if "\x1b" in newline:
# ANSI escape characters are present, so we can't assume
Expand DownExpand Up@@ -752,7 +758,6 @@ def __move_tall(self, x, y):
self.__write_code(self._cup, y - self.__offset, x)

def __sigwinch(self, signum, frame):
self.height, self.width = self.getheightwidth()
self.event_queue.insert(Event("resize", None))

def __hide_cursor(self):
Expand Down
5 changes: 4 additions & 1 deletionLib/_pyrepl/windows_console.py
View file
Open in desktop
Original file line numberDiff line numberDiff line change
Expand Up@@ -216,6 +216,9 @@ def refresh(self, screen: list[str], c_xy: tuple[int, int]) -> None:
oldscr = self.screen[old_offset : old_offset + height]
newscr = screen[offset : offset + height]

if len(oldscr) < height:
oldscr += [""] * (height - len(oldscr))

self.__offset = offset

self._hide_cursor()
Expand DownExpand Up@@ -287,7 +290,7 @@ def __write_changed_line(
self._move_relative(0, y + 1)
self.posxy = 0, y + 1
else:
self.posxy = wlen(newline), y
self.posxy =min(wlen(newline), self.width - 1), y

if "\x1b" in newline or y != self.posxy[1] or '\x1a' in newline:
# ANSI escape characters are present, so we can't assume
Expand Down
84 changes: 82 additions & 2 deletionsLib/test/test_pyrepl/test_unix_console.py
View file
Open in desktop
Original file line numberDiff line numberDiff line change
Expand Up@@ -258,7 +258,7 @@ def test_resize_bigger_on_multiline_function(self, _os_write):
reader, console = handle_events_short_unix_console(events)

console.height = 2
console.getheightwidth = MagicMock(lambda _:(2, 80))
console.getheightwidth = MagicMock(return_value=(2, 80))

def same_reader(_):
return reader
Expand DownExpand Up@@ -294,7 +294,7 @@ def test_resize_smaller_on_multiline_function(self, _os_write):
reader, console = handle_events_unix_console_height_3(events)

console.height = 1
console.getheightwidth = MagicMock(lambda _:(1, 80))
console.getheightwidth = MagicMock(return_value=(1, 80))

def same_reader(_):
return reader
Expand DownExpand Up@@ -328,3 +328,83 @@ def test_getheightwidth_with_invalid_environ(self, _os_write):
self.assertIsInstance(console.getheightwidth(), tuple)
os.environ = []
self.assertIsInstance(console.getheightwidth(), tuple)

def test_resize_screen_larger(self, _os_write):
# fmt: off
code = (
"def f():\n"
" foo = 123456789\n"
" bar = 'baz'"
)
# fmt: on

events = itertools.chain(code_to_events(code))
reader, console = handle_events_narrow_unix_console(events)

console.width = 80
console.getheightwidth = MagicMock(return_value=(100, 80))

def same_reader(_):
return reader

def same_console(events):
console.get_event = MagicMock(side_effect=events)
return console

_, con = handle_all_events(
[Event(evt="resize", data=None)],
prepare_reader=same_reader,
prepare_console=same_console,
)
self.assertListEqual(
con.screen,
[
"def f():",
" foo = 123456789",
" bar = 'baz'"
]
)

def test_resize_screen_smaller(self, _os_write):
# fmt: off
code = (
"def f():\n"
" foo = 123456789\n"
" bar = 'baz'"
)
# fmt: on

events = itertools.chain(code_to_events(code))
reader, console = handle_events_unix_console(events)

console.width = 5
console.getheightwidth = MagicMock(return_value=(100, 5))

def same_reader(_):
return reader

def same_console(events):
console.get_event = MagicMock(side_effect=events)
return console

_, con = handle_all_events(
[Event(evt="resize", data=None)],
prepare_reader=same_reader,
prepare_console=same_console,
)
self.assertListEqual(
con.screen,
[
"def \\",
"f():",
" fo\\",
"o = \\",
"1234\\",
"5678\\",
"9",
" ba\\",
"r = \\",
"'baz\\",
"'"
]
)
107 changes: 95 additions & 12 deletionsLib/test/test_pyrepl/test_windows_console.py
View file
Open in desktop
Original file line numberDiff line numberDiff line change
Expand Up@@ -24,6 +24,7 @@
MOVE_UP,
MOVE_DOWN,
ERASE_IN_LINE,
CLEAR,
)
import _pyrepl.windows_console as wc
except ImportError:
Expand DownExpand Up@@ -102,7 +103,7 @@ def test_resize_wider(self):

console.height = 20
console.width = 80
console.getheightwidth = MagicMock(lambda _:(20, 80))
console.getheightwidth = MagicMock(return_value=(20, 80))

def same_reader(_):
return reader
Expand All@@ -117,9 +118,8 @@ def same_console(events):
prepare_console=same_console,
)

con.out.write.assert_any_call(self.move_right(2))
con.out.write.assert_any_call(self.move_up(2))
con.out.write.assert_any_call(b"567890")
con.out.write.assert_any_call(self.clear())
con.out.write.assert_any_call(b"1234567890")

con.restore()

Expand All@@ -130,7 +130,7 @@ def test_resize_narrower(self):

console.height = 20
console.width = 4
console.getheightwidth = MagicMock(lambda _:(20, 4))
console.getheightwidth = MagicMock(return_value=(20, 4))

def same_reader(_):
return reader
Expand DownExpand Up@@ -264,7 +264,7 @@ def test_resize_bigger_on_multiline_function(self):
reader, console = self.handle_events_short(events)

console.height = 2
console.getheightwidth = MagicMock(lambda _:(2, 80))
console.getheightwidth = MagicMock(return_value=(2, 80))

def same_reader(_):
return reader
Expand All@@ -280,11 +280,13 @@ def same_console(events):
)
con.out.write.assert_has_calls(
[
call(self.move_left(5)),
call(self.clear()),
call(b"\n"),
call(self.move_up()),
call(b"def f():"),
call(self.move_left(3)),
call(self.move_left(8)),
call(self.move_down()),
call(b" foo")
]
)
console.restore()
Expand All@@ -302,7 +304,7 @@ def test_resize_smaller_on_multiline_function(self):
reader, console = self.handle_events_height_3(events)

console.height = 1
console.getheightwidth = MagicMock(lambda _:(1, 80))
console.getheightwidth = MagicMock(return_value=(1, 80))

def same_reader(_):
return reader
Expand All@@ -318,9 +320,7 @@ def same_console(events):
)
con.out.write.assert_has_calls(
[
call(self.move_left(5)),
call(self.move_up()),
call(self.erase_in_line()),
call(self.clear()),
call(b" foo"),
]
)
Expand All@@ -342,6 +342,9 @@ def move_right(self, cols=1):
def erase_in_line(self):
return ERASE_IN_LINE.encode("utf8")

def clear(self):
return CLEAR.encode("utf8")

def test_multiline_ctrl_z(self):
# see gh-126332
code = "abcdefghi"
Expand All@@ -357,6 +360,86 @@ def test_multiline_ctrl_z(self):
self.assertEqual(reader.cxy, (2, 3))
con.restore()

def test_resize_screen_larger(self):
# fmt: off
code = (
"def f():\n"
" foo = 123456789\n"
" bar = 'baz'"
)
# fmt: on

events = itertools.chain(code_to_events(code))
reader, console = self.handle_events_narrow(events)

console.width = 80
console.getheightwidth = MagicMock(return_value=(100, 80))

def same_reader(_):
return reader

def same_console(events):
console.get_event = MagicMock(side_effect=events)
return console

_, con = handle_all_events(
[Event(evt="resize", data=None)],
prepare_reader=same_reader,
prepare_console=same_console,
)
self.assertListEqual(
con.screen,
[
"def f():",
" foo = 123456789",
" bar = 'baz'"
]
)

def test_resize_screen_smaller(self):
# fmt: off
code = (
"def f():\n"
" foo = 123456789\n"
" bar = 'baz'"
)
# fmt: on

events = itertools.chain(code_to_events(code))
reader, console = self.handle_events(events)

console.width = 5
console.getheightwidth = MagicMock(return_value=(100, 5))

def same_reader(_):
return reader

def same_console(events):
console.get_event = MagicMock(side_effect=events)
return console

_, con = handle_all_events(
[Event(evt="resize", data=None)],
prepare_reader=same_reader,
prepare_console=same_console,
)
self.assertListEqual(
con.screen,
[
"def \\",
"f():",
" fo\\",
"o = \\",
"1234\\",
"5678\\",
"9",
" ba\\",
"r = \\",
"'baz\\",
"'"
]
)


class WindowsConsoleGetEventTests(TestCase):
# Virtual-Key Codes: https://learn.microsoft.com/en-us/windows/win32/inputdev/virtual-key-codes
Expand Down
View file
Open in desktop
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
In the New REPL, resize triggers full redraw now.
Also fixed cross-line width calculation and content rendering during scrolling.
Loading

[8]ページ先頭

©2009-2025 Movatter.jp