prompt-toolkit / prompt-toolkit/python-prompt-toolkit
print_formatted_text overwrites prompt, print does not
Open
@jonathanslenders is already working on this.
Since Jul 6, 2021.
bug
- Dominant language
- Python
- Stars
- 10.6k
- Forks
- 815
- PR merge metrics
- No merged PRs in 30d
Description
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())

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())

Contributor guide
No contributing guide indexed for this repository
First steps
- Read the whole issue, then the project's contributing guide.
- Comment on the issue to say you are picking it up — it saves two people doing the same work.
- Fork the repository and make your change on a branch.
- Open a pull request that references the issue number.
Assessment
This issue has not been assessed yet.