prompt-toolkit / prompt-toolkit/python-prompt-toolkit

print_formatted_text overwrites prompt, print does not

Open
#1,453 4 comments 0 reactions 1 assignee View on GitHub

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

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

Contributor guide

No contributing guide indexed for this repository

First steps

  1. Read the whole issue, then the project's contributing guide.
  2. Comment on the issue to say you are picking it up — it saves two people doing the same work.
  3. Fork the repository and make your change on a branch.
  4. Open a pull request that references the issue number.

Assessment

This issue has not been assessed yet.

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.