prompt-toolkit / python-prompt-toolkit

Library for building powerful interactive command line applications in Python
https://python-prompt-toolkit.readthedocs.io/
BSD 3-Clause "New" or "Revised" License
9.28k stars 715 forks source link

Is there a way to disable input on a PromptSession? #1622

Open Henddher opened 2 years ago

Henddher commented 2 years ago

Hi!

Maybe this is a contrived use case:

I create an asyncio task that constantly reads user input using a PromptSession. Meanwhile, another asyncio task renders output using print_formatted_text.

promptsession = PromptSession(...)
async def user_input():
  while True:
    with patch_stdout():
      user_input = await promptsession.prompt_async(...)
      # ... use user_input, output will be produced later and enqueued

async def output_proc(queue):
  while True:
    out = await queue.get()
    if out.disable_user_input:
        # Disable user input! (but how?)
        # promptsession.disable()
        ...
    print_formatted_text(as_html(out))

It would be nice if one could disable/enable the input even though promptsession.prompt_async() is already awaiting.

Although it's perhaps possible to exit() (I'm not sure) and recreate a promptsession, I'd like to preserve the in-memory history (without having to commit it to filesystem). Orchestrating "manually" is an option I would pursue if no "out-of-box" option exists within prompt_toolkit.

Side note, I found that is possible to hide the cursor, as mentioned in this bug.

harrandt commented 1 year ago

Did you find a solution?

Henddher commented 1 year ago

Nope :frowning_face: