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

input() style prompt coloring #1737

Open bonecountysheriff opened 1 year ago

bonecountysheriff commented 1 year ago

I need to temporarily use color codes like "\033[0m". But prompt() doesn't seem to allow it.

I've tried using print() to achieve this, followed by prompt(), but as soon as I hit enter after the input, the prompt disappears. Any way around this?

This is the code snippet I'm currently using.

print(f"\033[32mprompt> \033[0m", end='')
content = session.prompt(multiline=True, prompt_continuation=prompt_continuation)
ryran commented 1 year ago

If you have strings with raw ansi codes, Wrap them with prompt_tookit.ANSI(), e.g.:

from prompt_toolkit import ANSI, prompt
prompt(ANSI("\033[32mprompt>\033[0m "))

Docref: https://python-prompt-toolkit.readthedocs.io/en/master/pages/printing_text.html?highlight=ansi#formatted-text