terminalwriter.py


Overview

The [terminalwriter.py](/projects/286/67488) module provides utility functions and the `TerminalWriter` class to facilitate writing colored and formatted text output to terminals and files. It supports terminal width detection, markup (color and style) application, source code syntax highlighting, and line width accounting, making terminal output more readable and visually structured.

This module is particularly useful for command-line applications or test runners (such as pytest) that want to enhance output with colors and formatted separators, while gracefully handling different terminal capabilities and environments (including Windows).


Detailed Description of Components

Functions

get_terminal_width() -> int

should_do_markup(file: TextIO) -> bool


Class: TerminalWriter

A final class providing an interface to write colored and formatted output to terminals or files.

Class Attributes

Initialization

TerminalWriter(file: TextIO | None = None) -> None

Properties

Methods

markup(text: str, **markup: bool) -> str
sep(sepchar: str, title: str | None = None, fullwidth: int | None = None, **markup: bool) -> None
write(msg: str, *, flush: bool = False, **markup: bool) -> None
line(s: str = "", **markup: bool) -> None
flush() -> None

Private/Internal Methods

These methods are intended for internal use and provide advanced functionality, especially for syntax highlighting source code.

_write_source(lines: Sequence[str], indents: Sequence[str] = ()) -> None

_get_pygments_lexer(lexer: Literal["python", "diff"]) -> Lexer

_get_pygments_formatter() -> TerminalFormatter

_highlight(source: str, lexer: Literal["diff", "python"] = "python") -> str


Implementation Details & Algorithms


Interaction with Other Parts of the Application

This module is designed to be used within pytest or similar CLI tools that want enhanced terminal output, especially for displaying source code, diffs, and separators with colors and styles.


Usage Example

from terminalwriter import TerminalWriter

writer = TerminalWriter()
writer.line("Starting test suite", bold=True, green=True)
writer.sep("=", "Test Results", blue=True)
writer.write("Test 1 ... ", flush=True)
writer.line("PASSED", green=True)
writer.line("Test 2 ... ", flush=True)
writer.line("FAILED", red=True, bold=True)

Mermaid Diagram: Class Diagram for TerminalWriter

classDiagram
    class TerminalWriter {
        -_esctable: dict
        -_file: TextIO
        -hasmarkup: bool
        -_current_line: str
        -_terminal_width: int | None
        -code_highlight: bool
        +__init__(file: TextIO | None)
        +fullwidth: int
        +fullwidth(value: int)
        +width_of_current_line: int
        +markup(text: str, **markup: bool) str
        +sep(sepchar: str, title: str | None, fullwidth: int | None, **markup: bool) None
        +write(msg: str, flush: bool = False, **markup: bool) None
        +line(s: str = "", **markup: bool) None
        +flush() None
        -_write_source(lines: Sequence[str], indents: Sequence[str]) None
        -_get_pygments_lexer(lexer: Literal["python", "diff"]) Lexer
        -_get_pygments_formatter() TerminalFormatter
        -_highlight(source: str, lexer: Literal["diff", "python"]) str
    }

Summary

The [terminalwriter.py](/projects/286/67488) module is a robust helper for terminal output formatting, supporting color markup, syntax highlighting, and terminal-aware formatting. It is well suited for test runners and CLI tools needing enhanced visual output. Its design carefully addresses cross-platform issues and integrates with environment-driven configuration for flexible theming.


End of Documentation for terminalwriter.py