Skip to content

pretty-print a line at a time in a table? #965

Answered by willmcgugan
emeryberger asked this question in Q&A
Discussion options

You must be logged in to vote

Hi Emery,

I think you were one of the earliest adopters of Rich.

I didn't consider this use-case so this may not be pretty. But the 'trick' is to render a single Syntax object in to lines. Rendering generates a list of Segments which can then be used to create a new renderable.

The following seems to work:

from rich import print
from rich.console import Console
from rich.syntax import Syntax
from rich.table import Table

code = '''
class SyntaxTheme(ABC):
    """Base class for a syntax theme."""

    @abstractmethod
    def get_style_for_token(self, token_type: TokenType) -> Style:
        """Get a style for a given Pygments token."""
        raise NotImplementedError  # pragma: no cover

Replies: 1 comment 1 reply

Comment options

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

Answer selected by emeryberger
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