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.21k stars 715 forks source link

prompt doesn't show in docker image (2.0 branch) #625

Open randy3k opened 6 years ago

randy3k commented 6 years ago

I was trying to run my application on a python docker container but the prompt is not shown. Here is a simple way to reproduce it.

docker pull python
docker run --name python -dit python
docker exec -it python /bin/bash

then in the container, install prompt_toolkit

pip install git+https://github.com/jonathanslenders/python-prompt-toolkit

a simple script test.py

from prompt_toolkit import prompt

print("enter something")
text = prompt("> ")
print("you entered %s." % text)

run the script and enter "foo"

root@2084abd7fdb1:/# python test.py
enter something
you entered foo.

Note that the prompt was not shown in above. Interestingly, it works again if I reset the terminal

root@2084abd7fdb1:/# reset
root@2084abd7fdb1:/# python test.py
enter something
> bar
you entered bar.

BTW, I am using macOS iTerm if it matters.

jonathanslenders commented 6 years ago

This looks like a bug. Thanks for reporting! I can't reproduce on my Linux system, but will try later again on OS X.

randy3k commented 6 years ago

I can reproduce the same bug on Terminal.app and my another mac. I just notice that it does not happen every time I run python test.py. The prompt is shown occasionally and it isn't shown some other times.

randy3k commented 6 years ago

It may be related to this

root@1d92bea7b3b7:/# stty size
0 0
root@1d92bea7b3b7:/# reset -w
root@1d92bea7b3b7:/# stty size
24 80

Before reset, the stty size was not determined correctly. Also, the prompt in the above example would appear if I resize the terminal window.

randy3k commented 6 years ago

I found a hacky solution from stackoverflow

docker exec -it python bash -c "stty cols $COLUMNS rows $LINES && bash"
decentral1se commented 6 years ago

Should this be documented as a bash oddity? Looks like it to me ...

randy3k commented 6 years ago

I think prompt toolkit should nevertheless be able to handle the case of tty not getting the terminal size correctly.