Skip to content

Commit 69ca980

Browse files
randy3kjonathanslenders
authored andcommitted
bugfix in flush key
1. only invalidate ui when some keys are processed 2. do not start_timeout if the last key is _Flush, otherwise it causes a dead loop 3. only send flush key when the queue is not empty
1 parent b4498f6 commit 69ca980

File tree

1 file changed

+17
-6
lines changed

1 file changed

+17
-6
lines changed

prompt_toolkit/key_binding/key_processor.py

Lines changed: 17 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -283,11 +283,19 @@ def get_next():
283283
else:
284284
return self.input_queue.popleft()
285285

286+
keys_processed = False
287+
is_flush = False
288+
286289
while not_empty():
290+
keys_processed = True
291+
287292
# Process next key.
288293
key_press = get_next()
289294

290-
if key_press.key != Keys.CPRResponse:
295+
is_flush = key_press is _Flush
296+
is_cpr = key_press.key == Keys.CPRResponse
297+
298+
if not is_flush and not is_cpr:
291299
self.before_key_press.fire()
292300

293301
try:
@@ -300,13 +308,16 @@ def get_next():
300308
app.invalidate()
301309
raise
302310

303-
if key_press.key != Keys.CPRResponse:
311+
if not is_flush and not is_cpr:
304312
self.after_key_press.fire()
305313

306-
# Invalidate user interface.
307-
app.invalidate()
314+
if keys_processed:
315+
# Invalidate user interface.
316+
app.invalidate()
308317

309-
self._start_timeout()
318+
# skip timeout if the last key was flush
319+
if not is_flush:
320+
self._start_timeout()
310321

311322
def empty_queue(self):
312323
"""
@@ -387,7 +398,7 @@ def wait():
387398
" Wait for timeout. "
388399
time.sleep(self.timeout)
389400

390-
if counter == self._keys_pressed:
401+
if len(self.input_queue) > 0 and counter == self._keys_pressed:
391402
# (No keys pressed in the meantime.)
392403
call_from_executor(flush_keys)
393404

0 commit comments

Comments
 (0)