*Note this is my first pep may have made some mistakes
PEP: NNNN
Title: The textfmt module
: A Standardized Approach to Terminal Text Styling
Author: Rihaan Meher
Discussions-To: Pending
Status: Draft
Type: Standards Track
Created: 25-May-2025
Python-Version: 3.15
Abstract
The textfmt
module provides a standardized approach to text formatting in Python console applications. It simplifies ANSI escape code usage by offering named attributes and intuitive functions for styling text. By introducing a built-in formatting utility, Python enhances its capabilities for CLI-based applications without requiring third-party dependencies.
Motivation
Currently, developers rely on manual ANSI escape sequences or external libraries to format terminal output. This leads to inconsistent implementations and redundancy across projects. A standardized module would unify text styling conventions, improving usability in debugging, logging, and command-line applications.
Rationale
A built-in solution for text styling in Python aligns with its philosophy of readability and simplicity. While third-party libraries exist, a standard module ensures uniformity and avoids compatibility issues. Given Pythonâs widespread use in scripting and CLI development, textfmt
would streamline terminal output formatting, making structured text display more accessible.
Additionally, textfmt
would enhance readability and code maintainability by replacing raw escape sequences with meaningful attributes like textfmt.bold
, textfmt.red
, and textfmt.underline
.
1. ANSI Alias Class
A class containing named attributes for common ANSI escape sequences:
python
import textfmt
print(textfmt.bold + "Important!" + textfmt.reset)
print(textfmt.red + "Error detected." + textfmt.reset)
2. Formatting Functions
Helper functions for applying styles cleanly:
print(textfmt.style("Warning!", bold=True, fg="yellow"))
print(textfmt.style("Success!", fg="green"))
3. Terminal Compatibility Handling
Detects terminal support and falls back gracefully when ANSI codes are unsupported.
Examples
- Logging Enhancement:
python
logger.info(textfmt.green + "Operation succeeded" + textfmt.reset)
logger.error(textfmt.red + "Fatal error!" + textfmt.reset)
- Styled User Prompts:
python
user_input = input(textfmt.blue + "Enter your choice: " + textfmt.reset)
Security Implications
None
Backwards Compatibility
No compatibility issues for relevant systems. Can be easily backported if needed
How to Teach This
The textfmt
module will be part of Pythonâs standard library, ensuring consistent text formatting across applications. Code editors should update syntax highlighting to recognize textfmt
attributes, improving readability. IDEs can integrate format-aware previews to help developers visualize styled output in real time.