prompt-toolkit / prompt-toolkit/python-prompt-toolkit

FR: ability to pause KeyProcessor

Open
#1,910 1 comment 0 reactions 0 assignees View on GitHub

Nobody has claimed this yet.

Dominant language
Python
Stars
10.6k
Forks
815
PR merge metrics
No merged PRs in 30d

Description

I want to implement typeahead in a full screen app.

For example: if the user types a key while a list is loading, the key shouldn't be processed until the list is loaded and the item is focused.

# suppose the user types `nfoo\nbar\n` on a todo list

@kb.add("n") # create a new todo
async def _(event):
  app.key_processor.pause()

  await some_tasks()

  p = await prepare_and_focus_prompt()
  app.key_processor.resume()
  name = await p.readline(on_accept=app.key_processor.pause)
  # we have to pause key_processor immediately after `\n` so `bar\n` can be kept in `input_queue`

  await some_tasks()

  p2 = await prepare_and_focus_prompt()
  app.key_processor.resume() # now `bar` would be processed by the second prompt.
  desc = await p2.readline(on_accept=app.key_processor.pause)

  # back to the list and resume key_processor
  await create_item(name, desc)
  await load_and_focus_list()
  app.key_processor.resume() # any keys after `\n` should go to list window

Currently this is what I do:

  1. Configure asyncio to run tasks eagerly: asyncio.get_running_loop().set_task_factory(asyncio.eager_task_factory)
  2. Modify app._default_bindings to add an <any> handler, which will catch all unhandled keys.
  3. Before loading the list, focus a dummy window so key presses can be cought by (2)
  4. After the list is ready, focus the list item and send those keys to app.key_processor.feed_multiple.
  5. Call key_processor.process_keys. However, you have to call it via aynscio.get_running_loop().call_later(0.1, app.key_processor.process_keys) or the app will freeze. I have no idea why.

I also noticed that there is an input.detach function. However, it doesn't work in a key binding handler since keys typed are already sent to key_processor.input_queue.

Contributor guide

No contributing guide indexed for this repository

First steps

  1. Read the whole issue, then the project's contributing guide.
  2. Comment on the issue to say you are picking it up — it saves two people doing the same work.
  3. Fork the repository and make your change on a branch.
  4. Open a pull request that references the issue number.

Research direction

Start by tracing KeyProcessor, its input_queue, process_keys, feed_multiple, and the input.detach behavior described in the issue. Reproduce the queued-key scenario with asynchronous key bindings and inspect the default bindings and event loop interaction. Done means applications can pause processing, retain keys typed while paused, and resume them deterministically without freezing.

Written by the indexing model from the issue text.

Assessment

Tech stack
python
Domain
cli
Issue type
Feature
Difficulty
4/5
Estimated time
3-5 days
Activity status
Stale
Clarity
Mostly clear
Newbie friendliness
35/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.