Skip to content

Printer API

The core printing functionality of PyRePrint.

reprint

The main function for enhanced printing.

pyreprint.core.printer.reprint(*args, sep=' ', end='\n', file=None, flush=False, before=None, after=None, surround=None, style=None, width=None, rich=None, markup=True, **kwargs)

Enhanced print function with decorator and style support.

This is the main function of pyreprint, providing a drop-in replacement for the built-in print() with additional features.

Parameters:

Name Type Description Default
*args Any

Values to print (same as built-in print).

()
sep str

Separator between values.

' '
end str

String to append at end.

'\n'
file Optional[TextIO]

Output file (default: sys.stdout).

None
flush bool

Whether to flush output.

False
before DecoratorType

Decorator to print before the content.

None
after DecoratorType

Decorator to print after the content.

None
surround DecoratorType

Decorator for both before and after.

None
style Optional[str]

Named style to apply.

None
width Optional[int]

Width for decorators (0 = auto).

None
rich Optional[bool]

Whether to use rich for this print.

None
markup bool

Whether to parse rich markup.

True
**kwargs Any

Additional arguments for style functions.

{}

Examples:

Basic usage:

>>> reprint("Hello, World!")
Hello, World!

With decorators:

>>> reprint("Section", before="=", after="=", width=40)
========================================
Section
========================================

With style:

>>> reprint("Important!", style="banner")
****************************************
*             Important!               *
****************************************
Source code in pyreprint/core/printer.py
def reprint(
    *args: Any,
    sep: str = " ",
    end: str = "\n",
    file: Optional[TextIO] = None,
    flush: bool = False,
    before: DecoratorType = None,
    after: DecoratorType = None,
    surround: DecoratorType = None,
    style: Optional[str] = None,
    width: Optional[int] = None,
    rich: Optional[bool] = None,
    markup: bool = True,
    **kwargs: Any,
) -> None:
    """Enhanced print function with decorator and style support.

    This is the main function of pyreprint, providing a drop-in
    replacement for the built-in print() with additional features.

    Args:
        *args: Values to print (same as built-in print).
        sep: Separator between values.
        end: String to append at end.
        file: Output file (default: sys.stdout).
        flush: Whether to flush output.
        before: Decorator to print before the content.
        after: Decorator to print after the content.
        surround: Decorator for both before and after.
        style: Named style to apply.
        width: Width for decorators (0 = auto).
        rich: Whether to use rich for this print.
        markup: Whether to parse rich markup.
        **kwargs: Additional arguments for style functions.

    Examples:
        Basic usage:
        >>> reprint("Hello, World!")
        Hello, World!

        With decorators:
        >>> reprint("Section", before="=", after="=", width=40)
        ========================================
        Section
        ========================================

        With style:
        >>> reprint("Important!", style="banner")
        ****************************************
        *             Important!               *
        ****************************************
    """
    _default_printer(
        *args,
        sep=sep,
        end=end,
        file=file,
        flush=flush,
        before=before,
        after=after,
        surround=surround,
        style=style,
        width=width,
        rich=rich,
        markup=markup,
        **kwargs,
    )

Printer Class

A configurable printer instance.

pyreprint.core.printer.Printer(width=0, use_rich=False, default_style=None)

Enhanced printer with decorator and style support.

The Printer class provides a configurable print function with support for decorators (lines before/after), styles, and rich integration.

Parameters:

Name Type Description Default
width int

Default width for decorators (0 = auto-detect).

0
use_rich bool

Whether to use rich for output.

False
default_style Optional[str]

Default style to apply to all prints.

None
Example

printer = Printer(width=60) printer("Hello", before="=", after="=") ============================================================ Hello ============================================================

Source code in pyreprint/core/printer.py
def __init__(
    self,
    width: int = 0,
    use_rich: bool = False,
    default_style: Optional[str] = None,
) -> None:
    self.width = width
    self.use_rich = use_rich
    self.default_style = default_style

Functions

__call__(*args, sep=' ', end='\n', file=None, flush=False, before=None, after=None, surround=None, style=None, width=None, rich=None, markup=True, **kwargs)

Print with optional decorators and styling.

Parameters:

Name Type Description Default
*args Any

Values to print (same as built-in print).

()
sep str

Separator between values.

' '
end str

String to append at end.

'\n'
file Optional[TextIO]

Output file (default: sys.stdout).

None
flush bool

Whether to flush output.

False
before DecoratorType

Decorator to print before the content.

None
after DecoratorType

Decorator to print after the content.

None
surround DecoratorType

Decorator for both before and after.

None
style Optional[str]

Named style to apply.

None
width Optional[int]

Width for decorators.

None
rich Optional[bool]

Whether to use rich for this print.

None
markup bool

Whether to parse rich markup.

True
**kwargs Any

Additional arguments for style functions.

{}
Source code in pyreprint/core/printer.py
def __call__(
    self,
    *args: Any,
    sep: str = " ",
    end: str = "\n",
    file: Optional[TextIO] = None,
    flush: bool = False,
    before: DecoratorType = None,
    after: DecoratorType = None,
    surround: DecoratorType = None,
    style: Optional[str] = None,
    width: Optional[int] = None,
    rich: Optional[bool] = None,
    markup: bool = True,
    **kwargs: Any,
) -> None:
    """Print with optional decorators and styling.

    Args:
        *args: Values to print (same as built-in print).
        sep: Separator between values.
        end: String to append at end.
        file: Output file (default: sys.stdout).
        flush: Whether to flush output.
        before: Decorator to print before the content.
        after: Decorator to print after the content.
        surround: Decorator for both before and after.
        style: Named style to apply.
        width: Width for decorators.
        rich: Whether to use rich for this print.
        markup: Whether to parse rich markup.
        **kwargs: Additional arguments for style functions.
    """
    output_file = file or sys.stdout
    effective_width = width or self.width

    # Handle surround shortcut
    if surround is not None:
        before = before or surround
        after = after or surround

    # Apply style if specified
    effective_style = style or self.default_style
    if effective_style:
        self._apply_style(
            *args,
            sep=sep,
            end=end,
            file=output_file,
            flush=flush,
            style=effective_style,
            width=effective_width,
            **kwargs,
        )
        return

    # Determine if we should use rich
    use_rich_output = rich if rich is not None else self.use_rich

    # Print with decorators
    if use_rich_output:
        self._rich_print(
            *args,
            sep=sep,
            end=end,
            file=output_file,
            flush=flush,
            before=before,
            after=after,
            width=effective_width,
            markup=markup,
        )
    else:
        self._standard_print(
            *args,
            sep=sep,
            end=end,
            file=output_file,
            flush=flush,
            before=before,
            after=after,
            width=effective_width,
        )

banner(text, char='*', width=None, file=None)

Print a full-width banner.

Parameters:

Name Type Description Default
text str

Banner text.

required
char str

Border character.

'*'
width Optional[int]

Width of banner (0 = auto).

None
file Optional[TextIO]

Output file.

None
Source code in pyreprint/core/printer.py
def banner(
    self,
    text: str,
    char: str = "*",
    width: Optional[int] = None,
    file: Optional[TextIO] = None,
) -> None:
    """Print a full-width banner.

    Args:
        text: Banner text.
        char: Border character.
        width: Width of banner (0 = auto).
        file: Output file.
    """
    from pyreprint.core.decorators import banner

    effective_width = width or self.width
    print(banner(text, char, effective_width), file=file or sys.stdout)

box(text, char='|', corner='+', file=None)

Print text in a box.

Parameters:

Name Type Description Default
text str

Text to box.

required
char str

Vertical border character.

'|'
corner str

Corner character.

'+'
file Optional[TextIO]

Output file.

None
Source code in pyreprint/core/printer.py
def box(
    self,
    text: str,
    char: str = "|",
    corner: str = "+",
    file: Optional[TextIO] = None,
) -> None:
    """Print text in a box.

    Args:
        text: Text to box.
        char: Vertical border character.
        corner: Corner character.
        file: Output file.
    """
    from pyreprint.core.decorators import box

    print(box(text, char, corner), file=file or sys.stdout)

header(text, char='=', width=None, above=False, file=None)

Print a header with underline.

Parameters:

Name Type Description Default
text str

Header text.

required
char str

Underline character.

'='
width Optional[int]

Width of underline (0 = match text).

None
above bool

Whether to include line above.

False
file Optional[TextIO]

Output file.

None
Source code in pyreprint/core/printer.py
def header(
    self,
    text: str,
    char: str = "=",
    width: Optional[int] = None,
    above: bool = False,
    file: Optional[TextIO] = None,
) -> None:
    """Print a header with underline.

    Args:
        text: Header text.
        char: Underline character.
        width: Width of underline (0 = match text).
        above: Whether to include line above.
        file: Output file.
    """
    from pyreprint.core.decorators import header

    effective_width = width or self.width
    print(header(text, char, effective_width, above), file=file or sys.stdout)

line(char='=', width=None, file=None)

Print a horizontal line.

Parameters:

Name Type Description Default
char str

Character for the line.

'='
width Optional[int]

Width of the line (0 = auto).

None
file Optional[TextIO]

Output file.

None
Source code in pyreprint/core/printer.py
def line(
    self,
    char: str = "=",
    width: Optional[int] = None,
    file: Optional[TextIO] = None,
) -> None:
    """Print a horizontal line.

    Args:
        char: Character for the line.
        width: Width of the line (0 = auto).
        file: Output file.
    """
    from pyreprint.core.decorators import line

    effective_width = width or self.width
    print(line(char, effective_width), file=file or sys.stdout)

Utility Functions

install_print

pyreprint.core.printer.install_print()

Replace the built-in print with reprint.

Returns:

Type Description
Callable[..., None]

The original print function (for restoration if needed).

Example

original_print = install_print() print("Now uses reprint!") # Uses reprint import builtins builtins.print = original_print # Restore

Source code in pyreprint/core/printer.py
def install_print() -> Callable[..., None]:
    """Replace the built-in print with reprint.

    Returns:
        The original print function (for restoration if needed).

    Example:
        >>> original_print = install_print()
        >>> print("Now uses reprint!")  # Uses reprint
        >>> import builtins
        >>> builtins.print = original_print  # Restore
    """
    import builtins

    original = builtins.print
    builtins.print = reprint  # type: ignore[assignment]
    return original

uninstall_print

pyreprint.core.printer.uninstall_print(original=None)

Restore the built-in print function.

Parameters:

Name Type Description Default
original Optional[Callable[..., None]]

The original print function to restore. If None, restores the standard print.

None
Source code in pyreprint/core/printer.py
def uninstall_print(original: Optional[Callable[..., None]] = None) -> None:
    """Restore the built-in print function.

    Args:
        original: The original print function to restore.
                  If None, restores the standard print.
    """
    import builtins

    if original is not None:
        builtins.print = original
    else:
        # Get the original from the builtins module itself
        import _io

        builtins.print = _io.TextIOWrapper.write  # type: ignore[assignment]