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

print_formatted_text overwrites prompt, print does not #1453

Open GNUGradyn opened 3 years ago

GNUGradyn commented 3 years ago

I'm trying to write a console using this library. If I just use print() to print data, everything works as expected

from prompt_toolkit import PromptSession
from prompt_toolkit.patch_stdout import patch_stdout
from prompt_toolkit import print_formatted_text
from prompt_toolkit.formatted_text import FormattedText
import asyncio

async def printer():
    while True:
        print("DATA TIME")
        await asyncio.sleep(1)

async def prompter():
    session = PromptSession()
    while True:
        with patch_stdout():
            input = await session.prompt_async('$ ')

async def main():
    printer_task = asyncio.create_task(printer())
    await prompter()
    await printer_task

asyncio.run(main())

out1

But if we do the exact same thing, literally just replacing the print() with a print_formatted_text(), everything falls apart

from prompt_toolkit import PromptSession
from prompt_toolkit.patch_stdout import patch_stdout
from prompt_toolkit import print_formatted_text
from prompt_toolkit.formatted_text import FormattedText
import asyncio

async def printer():
    while True:
        print_formatted_text(FormattedText([('#0000FF', '[I] '),('', "DATA TIME")]))
        await asyncio.sleep(1)

async def prompter():
    session = PromptSession()
    while True:
        with patch_stdout():
            input = await session.prompt_async('$ ')

async def main():
    printer_task = asyncio.create_task(printer())
    await prompter()
    await printer_task

asyncio.run(main())

out2

jonathanslenders commented 2 years ago

This is definitely a bug. This is supposed to work. I'll look into it.

jonathanslenders commented 2 years ago

https://github.com/prompt-toolkit/python-prompt-toolkit/pull/1455

GNUGradyn commented 2 years ago

Thanks for the update. However, this still appears to be an issue with the latest version on pypi

GNUGradyn commented 2 years ago

Any update on this?