Skip to content

Rich Integration API

Integration with the Rich library for colorful output.

RichPrinter

pyreprint.integrations.rich.RichPrinter(console=None, force_terminal=False, width=None)

Rich-enabled printer with enhanced formatting.

This class provides a printer that leverages Rich for colorful, formatted console output.

Parameters:

Name Type Description Default
console Optional[Console]

Rich Console instance to use.

None
force_terminal bool

Force terminal output even if not a TTY.

False
width Optional[int]

Console width (None for auto-detect).

None
Example

printer = RichPrinter() printer.print("Hello, [bold]World[/bold]!") printer.rule("Section") printer.panel("Important message", title="Notice")

Source code in pyreprint/integrations/rich.py
def __init__(
    self,
    console: Optional[Console] = None,
    force_terminal: bool = False,
    width: Optional[int] = None,
) -> None:
    self.console = console or Console(
        force_terminal=force_terminal,
        width=width,
    )

Functions

header(text, style='bold', rule_style='rule.line')

Print a header with underline.

Parameters:

Name Type Description Default
text str

Header text.

required
style str

Style for the text.

'bold'
rule_style str

Style for the underline.

'rule.line'
Source code in pyreprint/integrations/rich.py
def header(
    self,
    text: str,
    style: str = "bold",
    rule_style: str = "rule.line",
) -> None:
    """Print a header with underline.

    Args:
        text: Header text.
        style: Style for the text.
        rule_style: Style for the underline.
    """
    self.console.print(Text(text, style=style))
    self.console.print(Rule(style=rule_style))

panel(content, title=None, subtitle=None, border_style='blue', expand=True)

Print content in a panel.

Parameters:

Name Type Description Default
content str

The panel content.

required
title Optional[str]

Optional panel title.

None
subtitle Optional[str]

Optional panel subtitle.

None
border_style str

Style for the border.

'blue'
expand bool

Whether to expand to full width.

True
Source code in pyreprint/integrations/rich.py
def panel(
    self,
    content: str,
    title: Optional[str] = None,
    subtitle: Optional[str] = None,
    border_style: str = "blue",
    expand: bool = True,
) -> None:
    """Print content in a panel.

    Args:
        content: The panel content.
        title: Optional panel title.
        subtitle: Optional panel subtitle.
        border_style: Style for the border.
        expand: Whether to expand to full width.
    """
    panel = Panel(
        content,
        title=title,
        subtitle=subtitle,
        border_style=border_style,
        expand=expand,
    )
    self.console.print(panel)

print(*args, sep=' ', end='\n', markup=True, highlight=True, **kwargs)

Print with Rich formatting.

Parameters:

Name Type Description Default
*args Any

Values to print.

()
sep str

Separator between values.

' '
end str

String to append at end.

'\n'
markup bool

Whether to parse Rich markup.

True
highlight bool

Whether to highlight syntax.

True
**kwargs Any

Additional arguments for Console.print.

{}
Source code in pyreprint/integrations/rich.py
def print(
    self,
    *args: Any,
    sep: str = " ",
    end: str = "\n",
    markup: bool = True,
    highlight: bool = True,
    **kwargs: Any,
) -> None:
    """Print with Rich formatting.

    Args:
        *args: Values to print.
        sep: Separator between values.
        end: String to append at end.
        markup: Whether to parse Rich markup.
        highlight: Whether to highlight syntax.
        **kwargs: Additional arguments for Console.print.
    """
    message = sep.join(str(arg) for arg in args)
    self.console.print(message, end=end, markup=markup, highlight=highlight, **kwargs)

rule(title='', char='-', style='rule.line', align='center')

Print a horizontal rule.

Parameters:

Name Type Description Default
title str

Optional title in the rule.

''
char str

Character for the line (Rich may override).

'-'
style str

Rich style for the rule.

'rule.line'
align str

Title alignment ('left', 'center', 'right').

'center'
Source code in pyreprint/integrations/rich.py
def rule(
    self,
    title: str = "",
    char: str = "-",
    style: str = "rule.line",
    align: str = "center",
) -> None:
    """Print a horizontal rule.

    Args:
        title: Optional title in the rule.
        char: Character for the line (Rich may override).
        style: Rich style for the rule.
        align: Title alignment ('left', 'center', 'right').
    """
    if title:
        self.console.print(Rule(title=title, style=style, align=align))  # type: ignore
    else:
        self.console.print(Rule(style=style))

section(text, style='bold blue')

Print a section with rules above and below.

Parameters:

Name Type Description Default
text str

Section text.

required
style str

Style for the text.

'bold blue'
Source code in pyreprint/integrations/rich.py
def section(
    self,
    text: str,
    style: str = "bold blue",
) -> None:
    """Print a section with rules above and below.

    Args:
        text: Section text.
        style: Style for the text.
    """
    self.console.print(Rule())
    self.console.print(Text(text, style=style), justify="center")
    self.console.print(Rule())

status(message, status_type='info')

Print a status message with appropriate styling.

Parameters:

Name Type Description Default
message str

The status message.

required
status_type str

Type of status ('info', 'success', 'warning', 'error').

'info'
Source code in pyreprint/integrations/rich.py
def status(
    self,
    message: str,
    status_type: str = "info",
) -> None:
    """Print a status message with appropriate styling.

    Args:
        message: The status message.
        status_type: Type of status ('info', 'success', 'warning', 'error').
    """
    styles = {
        "info": ("[blue][INFO][/blue]", "blue"),
        "success": ("[green][OK][/green]", "green"),
        "warning": ("[yellow][WARN][/yellow]", "yellow"),
        "error": ("[red][ERROR][/red]", "red"),
    }
    prefix, style = styles.get(status_type, styles["info"])
    self.console.print(f"{prefix} {message}")

table(data, headers=None, title=None)

Print data as a table.

Parameters:

Name Type Description Default
data list

List of rows (each row is a list of values).

required
headers Optional[list]

Optional list of column headers.

None
title Optional[str]

Optional table title.

None
Source code in pyreprint/integrations/rich.py
def table(
    self,
    data: list,
    headers: Optional[list] = None,
    title: Optional[str] = None,
) -> None:
    """Print data as a table.

    Args:
        data: List of rows (each row is a list of values).
        headers: Optional list of column headers.
        title: Optional table title.
    """
    table = Table(title=title)

    if headers:
        for header in headers:
            table.add_column(str(header))
    elif data:
        for i in range(len(data[0])):
            table.add_column(f"Column {i + 1}")

    for row in data:
        table.add_row(*[str(cell) for cell in row])

    self.console.print(table)

Functions

rprint

pyreprint.integrations.rich.rprint(*args, sep=' ', end='\n', file=None, markup=True, highlight=True, **kwargs)

Rich-enabled print function.

A drop-in replacement for print() that uses Rich for formatting.

Parameters:

Name Type Description Default
*args Any

Values to print.

()
sep str

Separator between values.

' '
end str

String to append at end.

'\n'
file Optional[TextIO]

Output file (uses Rich console if None).

None
markup bool

Whether to parse Rich markup.

True
highlight bool

Whether to highlight syntax.

True
**kwargs Any

Additional arguments for Console.print.

{}
Example

rprint("[bold red]Error:[/bold red] Something went wrong") rprint("Processing...", style="italic")

Source code in pyreprint/integrations/rich.py
def rprint(
    *args: Any,
    sep: str = " ",
    end: str = "\n",
    file: Optional[TextIO] = None,
    markup: bool = True,
    highlight: bool = True,
    **kwargs: Any,
) -> None:
    """Rich-enabled print function.

    A drop-in replacement for print() that uses Rich for formatting.

    Args:
        *args: Values to print.
        sep: Separator between values.
        end: String to append at end.
        file: Output file (uses Rich console if None).
        markup: Whether to parse Rich markup.
        highlight: Whether to highlight syntax.
        **kwargs: Additional arguments for Console.print.

    Example:
        >>> rprint("[bold red]Error:[/bold red] Something went wrong")
        >>> rprint("Processing...", style="italic")
    """
    if file is not None and file != sys.stdout:
        # Use standard print for non-stdout files
        message = sep.join(str(arg) for arg in args)
        print(message, end=end, file=file)
    else:
        message = sep.join(str(arg) for arg in args)
        console.print(message, end=end, markup=markup, highlight=highlight, **kwargs)

rule

pyreprint.integrations.rich.rule(title='', style='rule.line')

Print a horizontal rule.

Parameters:

Name Type Description Default
title str

Optional title in the rule.

''
style str

Rich style for the rule.

'rule.line'
Source code in pyreprint/integrations/rich.py
def rule(title: str = "", style: str = "rule.line") -> None:
    """Print a horizontal rule.

    Args:
        title: Optional title in the rule.
        style: Rich style for the rule.
    """
    if title:
        console.print(Rule(title=title, style=style))
    else:
        console.print(Rule(style=style))

panel

pyreprint.integrations.rich.panel(content, title=None, border_style='blue')

Print content in a panel.

Parameters:

Name Type Description Default
content str

The panel content.

required
title Optional[str]

Optional panel title.

None
border_style str

Style for the border.

'blue'
Source code in pyreprint/integrations/rich.py
def panel(
    content: str,
    title: Optional[str] = None,
    border_style: str = "blue",
) -> None:
    """Print content in a panel.

    Args:
        content: The panel content.
        title: Optional panel title.
        border_style: Style for the border.
    """
    console.print(
        Panel(content, title=title, border_style=border_style)
    )

PrettyPrint

pprint

pyreprint.integrations.pprint.pprint(obj, stream=None, indent=1, width=80, depth=None, compact=False, sort_dicts=True)

Pretty print an object.

Enhanced wrapper around the standard pprint module.

Parameters:

Name Type Description Default
obj Any

Object to print.

required
stream Optional[TextIO]

Output stream (default: sys.stdout).

None
indent int

Indentation level for nested structures.

1
width int

Maximum line width.

80
depth Optional[int]

Maximum depth to print (None for unlimited).

None
compact bool

Use compact format for sequences.

False
sort_dicts bool

Sort dictionary keys.

True
Example

pprint({"a": [1, 2, 3], "b": {"nested": "value"}}) {'a': [1, 2, 3], 'b': {'nested': 'value'}}

Source code in pyreprint/integrations/pprint.py
def pprint(
    obj: Any,
    stream: Optional[TextIO] = None,
    indent: int = 1,
    width: int = 80,
    depth: Optional[int] = None,
    compact: bool = False,
    sort_dicts: bool = True,
) -> None:
    """Pretty print an object.

    Enhanced wrapper around the standard pprint module.

    Args:
        obj: Object to print.
        stream: Output stream (default: sys.stdout).
        indent: Indentation level for nested structures.
        width: Maximum line width.
        depth: Maximum depth to print (None for unlimited).
        compact: Use compact format for sequences.
        sort_dicts: Sort dictionary keys.

    Example:
        >>> pprint({"a": [1, 2, 3], "b": {"nested": "value"}})
        {'a': [1, 2, 3], 'b': {'nested': 'value'}}
    """
    _pprint.pprint(
        obj,
        stream=stream,
        indent=indent,
        width=width,
        depth=depth,
        compact=compact,
        sort_dicts=sort_dicts,
    )

pformat

pyreprint.integrations.pprint.pformat(obj, indent=1, width=80, depth=None, compact=False, sort_dicts=True)

Format an object as a pretty-printed string.

Parameters:

Name Type Description Default
obj Any

Object to format.

required
indent int

Indentation level for nested structures.

1
width int

Maximum line width.

80
depth Optional[int]

Maximum depth to print (None for unlimited).

None
compact bool

Use compact format for sequences.

False
sort_dicts bool

Sort dictionary keys.

True

Returns:

Type Description
str

Pretty-printed string representation.

Example

s = pformat({"key": "value"}) print(s)

Source code in pyreprint/integrations/pprint.py
def pformat(
    obj: Any,
    indent: int = 1,
    width: int = 80,
    depth: Optional[int] = None,
    compact: bool = False,
    sort_dicts: bool = True,
) -> str:
    """Format an object as a pretty-printed string.

    Args:
        obj: Object to format.
        indent: Indentation level for nested structures.
        width: Maximum line width.
        depth: Maximum depth to print (None for unlimited).
        compact: Use compact format for sequences.
        sort_dicts: Sort dictionary keys.

    Returns:
        Pretty-printed string representation.

    Example:
        >>> s = pformat({"key": "value"})
        >>> print(s)
        {'key': 'value'}
    """
    return _pprint.pformat(
        obj,
        indent=indent,
        width=width,
        depth=depth,
        compact=compact,
        sort_dicts=sort_dicts,
    )

PrettyPrinter

pyreprint.integrations.pprint.PrettyPrinter(indent=1, width=80, depth=None, compact=False, sort_dicts=True, stream=None)

Configurable pretty printer.

This class provides a reusable pretty printer with customizable settings.

Parameters:

Name Type Description Default
indent int

Indentation level for nested structures.

1
width int

Maximum line width.

80
depth Optional[int]

Maximum depth to print (None for unlimited).

None
compact bool

Use compact format for sequences.

False
sort_dicts bool

Sort dictionary keys.

True
stream Optional[TextIO]

Default output stream.

None
Example

pp = PrettyPrinter(indent=2, width=60) pp.pprint({"a": [1, 2, 3]})

Source code in pyreprint/integrations/pprint.py
def __init__(
    self,
    indent: int = 1,
    width: int = 80,
    depth: Optional[int] = None,
    compact: bool = False,
    sort_dicts: bool = True,
    stream: Optional[TextIO] = None,
) -> None:
    self.indent = indent
    self.width = width
    self.depth = depth
    self.compact = compact
    self.sort_dicts = sort_dicts
    self.stream = stream or sys.stdout
    self._pp = _pprint.PrettyPrinter(
        indent=indent,
        width=width,
        depth=depth,
        compact=compact,
        sort_dicts=sort_dicts,
        stream=self.stream,
    )

Functions

__call__(obj)

Pretty print an object (shortcut for pprint).

Parameters:

Name Type Description Default
obj Any

Object to print.

required
Source code in pyreprint/integrations/pprint.py
def __call__(self, obj: Any) -> None:
    """Pretty print an object (shortcut for pprint).

    Args:
        obj: Object to print.
    """
    self.pprint(obj)

isreadable(obj)

Check if object repr is readable.

Parameters:

Name Type Description Default
obj Any

Object to check.

required

Returns:

Type Description
bool

True if repr is readable Python code.

Source code in pyreprint/integrations/pprint.py
def isreadable(self, obj: Any) -> bool:
    """Check if object repr is readable.

    Args:
        obj: Object to check.

    Returns:
        True if repr is readable Python code.
    """
    return self._pp.isreadable(obj)

isrecursive(obj)

Check if object has recursive reference.

Parameters:

Name Type Description Default
obj Any

Object to check.

required

Returns:

Type Description
bool

True if object has recursive reference.

Source code in pyreprint/integrations/pprint.py
def isrecursive(self, obj: Any) -> bool:
    """Check if object has recursive reference.

    Args:
        obj: Object to check.

    Returns:
        True if object has recursive reference.
    """
    return self._pp.isrecursive(obj)

pformat(obj)

Format an object as a pretty-printed string.

Parameters:

Name Type Description Default
obj Any

Object to format.

required

Returns:

Type Description
str

Pretty-printed string representation.

Source code in pyreprint/integrations/pprint.py
def pformat(self, obj: Any) -> str:
    """Format an object as a pretty-printed string.

    Args:
        obj: Object to format.

    Returns:
        Pretty-printed string representation.
    """
    return self._pp.pformat(obj)

pprint(obj)

Pretty print an object.

Parameters:

Name Type Description Default
obj Any

Object to print.

required
Source code in pyreprint/integrations/pprint.py
def pprint(self, obj: Any) -> None:
    """Pretty print an object.

    Args:
        obj: Object to print.
    """
    self._pp.pprint(obj)