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.1k stars 717 forks source link

How to make prompt-toolkit work on dumb terminals #1032

Open halloleo opened 4 years ago

halloleo commented 4 years ago

On dumb terminals (TERM=dumb or TERM=unkown) prompt-toolkit behaves strangely: Some message are not displayed, it expects arrow key support, etc.

I was wondering whether it is possible to swap the Vt100Input class for a class which offers genuinely less capabilities (i.e. no cursor movement, no arrow keys, etc).

jonathanslenders commented 4 years ago

What is the reason you are using a dumb terminal?

I believe the input should work fine, but the rendering output will not, because prompt_toolkit's rendering engine is made for a complex layout that can't be converted to what a dumb terminal expects.

What about solving it in your application the way you want to handle it?

if os.environ['TERM'] in ['dumb', 'unknown']:
    text = input(...)
else:
    text = prompt_toolkit.prompt(...)
halloleo commented 4 years ago

What is the reason you are using a dumb terminal?

I have my shell in an Emacs shell-buffer which is very handy for all interaction beween shell and files/directories. However it provides a rather minimal termianl interface accepting the output as stream of simple lines and providing the input as line string.

I believe the input should work fine, but the rendering output will not, because prompt_toolkit's rendering engine is made for a complex layout that can't be converted to what a dumb terminal expects.

Yep, I guess the output is the problem.

What about solving it in your application the way you want to handle it?

Lots of libraries and application work on top of prompt_toolkit! If prompt-toolkit had an option to degrade gracefully on dumb terminals all these applications would automatically work! :-)

jonathanslenders commented 4 years ago

Hi @halloleo,

I think we can do it. Can you have a look at this PR and see whether that works? https://github.com/prompt-toolkit/python-prompt-toolkit/pull/1035 (I didn't test it yet.)

halloleo commented 4 years ago

Wow, you are pretty quick with the fix!

I have tried it and it somewhat works. Examples like get-input.py or regular-language.py work fine and auto-suggestion.py, autocorrection.py and clock-input.py degrade reasonably.

However I noticed two things:

  1. When you run confirmation-prompt.py the print result is different: under vt100 terminals it is True/False,, under dumb terminals it is y/n.
  2. For apps using responds_to_cpr in vt100.py you still need to set the environment var PROMPT_TOOLKIT_NO_CPR for better degradation. Is there a chance to check for PROMPT_TOOLKIT_NO_CPR and for TERM=dumb?

Thanks heaps for looking into this!

jonathanslenders commented 4 years ago

Thanks for the feedback! I still have to look into fixing this.

halloleo commented 4 years ago

No worries. Fully understand that there is more to it.

salotz commented 4 years ago

+1 for this! My main use case is Emacs as well.

genovese commented 10 months ago

+1 for the same case. Thanks!

stdedos commented 3 months ago

My case involves debugging via PyCharm