如何在rich.prompt.Prompt中突出显示

问题描述

我有这个荧光笔

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)

如果我执行类似 console.print() 的操作,它会提供自定义突出显示,但不会为 rich.prompt.Prompt.ask() 提供自定义突出显示

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

enter image description here

如何获得 rich.prompt.Prompt.ask() 的荧光笔

解决方法

您是否希望在键入时突出显示您输入的文本?

Rich 没有这种能力。您可能需要查看 prompt_toolkit。