Search code examples
pythonhighlightrich

How to highlight in rich.prompt.Prompt


I have this highlighter

from rich.console import Console
import rich.prompt
from rich.highlighter import RegexHighlighter
from rich.theme import Theme


class Highlighter(RegexHighlighter):
    base_style = "help."
    highlights = [r"(?P<cmd>!help\b)", r"(?P<cmd2>\'|\"[\w]+\"|\')"]

theme = Theme({"help.cmd": "bold magenta", "help.cmd2": "bold green"})
console = Console(highlighter=Highlighter(), theme=theme)

If I do something like console.print() it gives the custom highlighting but it dosen't give custom highlighting for rich.prompt.Prompt.ask()

prompt = rich.prompt.Prompt(console=console)
text = prompt.ask("\'Enter text\'\n")
console.print(text)

enter image description here

How can I get the highlighter for rich.prompt.Prompt.ask()


Solution

  • Are you expecting the text you enter to be highlighted as you type?

    Rich doesn't have that capability. You might want to look in to prompt_toolkit for that.