Skip to content

Commit

Permalink
Removed key_repeat_queue, only use the event_queue
Browse files Browse the repository at this point in the history
And replaced deque.insert(0, o) with deque.appendleft(o) for O(1) insertion
  • Loading branch information
SimonvBez committed Jan 13, 2025
1 parent ebe00f0 commit 1f7bf1f
Showing 1 changed file with 5 additions and 9 deletions.
14 changes: 5 additions & 9 deletions Lib/_pyrepl/windows_console.py
Original file line number Diff line number Diff line change
Expand Up @@ -132,7 +132,6 @@ def __init__(
self.height = 25
self.__offset = 0
self.event_queue: deque[Event] = deque()
self.key_repeat_queue: deque[Event] = deque()
try:
self.out = io._WindowsConsoleIO(self.output_fd, "w") # type: ignore[attr-defined]
except ValueError:
Expand Down Expand Up @@ -347,7 +346,7 @@ def move_cursor(self, x: int, y: int) -> None:
raise ValueError(f"Bad cursor position {x}, {y}")

if y < self.__offset or y >= self.__offset + self.height:
self.event_queue.insert(0, Event("scroll", ""))
self.event_queue.appendleft(Event("scroll", ""))
else:
self._move_relative(x, y)
self.__posxy = x, y
Expand Down Expand Up @@ -395,9 +394,6 @@ def get_event(self, block: bool = True) -> Event | None:
"""Return an Event instance. Returns None if |block| is false
and there is no event pending, otherwise waits for the
completion of an event."""
if self.key_repeat_queue:
return self.key_repeat_queue.pop()

if self.event_queue:
return self.event_queue.pop()

Expand All @@ -420,7 +416,7 @@ def get_event(self, block: bool = True) -> Event | None:
if event is not None:
# Queue this key event to be repeated if wRepeatCount > 1, such as when a 'dead key' is pressed twice
for _ in range(rec.Event.KeyEvent.wRepeatCount - 1):
self.key_repeat_queue.appendleft(event)
self.event_queue.appendleft(event)
elif block:
# The key event didn't ectually type a character, block until next event
continue
Expand All @@ -444,15 +440,15 @@ def _event_from_keyevent(self, key_event: KeyEvent) -> Event | None:
key = f"ctrl {key}"
elif key_event.dwControlKeyState & ALT_ACTIVE:
# queue the key, return the meta command
self.event_queue.insert(0, Event(evt="key", data=key, raw=key))
self.event_queue.appendleft(Event(evt="key", data=key, raw=key))
return Event(evt="key", data="\033") # keymap.py uses this for meta
return Event(evt="key", data=key, raw=key)

return None

if key_event.dwControlKeyState & ALT_ACTIVE:
# queue the key, return the meta command
self.event_queue.insert(0, Event(evt="key", data=key, raw=raw_key))
self.event_queue.appendleft(Event(evt="key", data=key, raw=raw_key))
return Event(evt="key", data="\033") # keymap.py uses this for meta

return Event(evt="key", data=key, raw=raw_key)
Expand Down Expand Up @@ -503,7 +499,7 @@ def wait(self, timeout: float | None) -> bool:
# Poor man's Windows select loop
start_time = time.time()
while True:
if msvcrt.kbhit() or self.key_repeat_queue: # type: ignore[attr-defined]
if msvcrt.kbhit() or self.event_queue: # type: ignore[attr-defined]
return True
if timeout and time.time() - start_time > timeout / 1000:
return False
Expand Down

0 comments on commit 1f7bf1f

Please sign in to comment.