Skip to content

Combine Text and Syntax? #3363

Answered by TomJGooding
moyix asked this question in Q&A
May 20, 2024 · 1 comments · 1 reply
Discussion options

You must be logged in to vote

I've had a quick bash at this (pun intended). Probably not exactly what you're looking for, but might help point you in the right direction.

from rich.console import Console
from rich.syntax import Syntax
from rich.text import Text

console = Console(width=10)

prefix_text = Text("# ")
syntax = Syntax("long ${commaaaaaand} goes here", "bash", word_wrap=True)
syntax_text = syntax.highlight(
    syntax.code,  # `Syntax.highlight` returns a `Text` instance
)

combined_text = prefix_text.append_text(syntax_text)
console.print(combined_text)

Replies: 1 comment 1 reply

Comment options

You must be logged in to vote
1 reply
@moyix
Comment options

Answer selected by moyix
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Category
Q&A
Labels
None yet
2 participants