Python Command Line Interface Tools for colored output, progress bars, text formatting, and argument handling
—
Quality
Pending
Does it follow best practices?
Impact
Pending
No eval scenarios have been run
Rich colored text output with automatic TTY detection and cross-platform terminal color support. Provides color functions and ColoredString class that maintains color information while supporting standard string operations and intelligent color output based on terminal capabilities.
Create colored text with support for different colors, bold styling, and forced color output.
def black(string, always=False, bold=False):
"""
Returns black ColoredString.
Parameters:
- string: str, text to color
- always: bool, force color even when not TTY (default: False)
- bold: bool, make text bold (default: False)
Returns:
ColoredString: colored string object
"""
def red(string, always=False, bold=False):
"""
Returns red ColoredString.
Parameters:
- string: str, text to color
- always: bool, force color even when not TTY (default: False)
- bold: bool, make text bold (default: False)
Returns:
ColoredString: colored string object
"""
def green(string, always=False, bold=False):
"""
Returns green ColoredString.
Parameters:
- string: str, text to color
- always: bool, force color even when not TTY (default: False)
- bold: bool, make text bold (default: False)
Returns:
ColoredString: colored string object
"""
def yellow(string, always=False, bold=False):
"""
Returns yellow ColoredString.
Parameters:
- string: str, text to color
- always: bool, force color even when not TTY (default: False)
- bold: bool, make text bold (default: False)
Returns:
ColoredString: colored string object
"""
def blue(string, always=False, bold=False):
"""
Returns blue ColoredString.
Parameters:
- string: str, text to color
- always: bool, force color even when not TTY (default: False)
- bold: bool, make text bold (default: False)
Returns:
ColoredString: colored string object
"""
def magenta(string, always=False, bold=False):
"""
Returns magenta ColoredString.
Parameters:
- string: str, text to color
- always: bool, force color even when not TTY (default: False)
- bold: bool, make text bold (default: False)
Returns:
ColoredString: colored string object
"""
def cyan(string, always=False, bold=False):
"""
Returns cyan ColoredString.
Parameters:
- string: str, text to color
- always: bool, force color even when not TTY (default: False)
- bold: bool, make text bold (default: False)
Returns:
ColoredString: colored string object
"""
def white(string, always=False, bold=False):
"""
Returns white ColoredString.
Parameters:
- string: str, text to color
- always: bool, force color even when not TTY (default: False)
- bold: bool, make text bold (default: False)
Returns:
ColoredString: colored string object
"""Enhanced string class that maintains color information while supporting all standard string operations.
class ColoredString:
def __init__(self, color, s, always_color=False, bold=False):
"""
Enhanced string for colored output with string operation compatibility.
Parameters:
- color: str, color name (e.g., 'RED', 'GREEN', 'BLUE')
- s: str, the actual string content
- always_color: bool, force color output regardless of TTY (default: False)
- bold: bool, make text bold (default: False)
"""
def __len__(self):
"""Return length of the underlying string (ignoring color codes)."""
def __str__(self):
"""Return colored string or plain string based on terminal capabilities."""
def __add__(self, other):
"""Concatenate with another string, preserving color."""
def __mul__(self, other):
"""Multiply string content, preserving color."""
def __repr__(self):
"""Return string representation for debugging."""
def __unicode__(self):
"""Return unicode string representation (Python 2/3 compatibility)."""
def __iter__(self):
"""Enable iteration over the underlying string."""
def __radd__(self, other):
"""Support right-hand addition with other strings."""
def __getattr__(self, att):
"""
Proxy all string methods to the underlying string while preserving color.
This enables ColoredString to support all string methods like .upper(),
.lower(), .split(), etc. while maintaining color information.
Parameters:
- att: str, attribute/method name to proxy
Returns:
ColoredString or result of string method
"""
@property
def color_str(self):
"""
Returns the actual colored string with ANSI escape codes.
Returns:
str: string with ANSI color codes applied
"""Functions for color management and string cleaning.
def clean(s):
"""
Strips ANSI color codes and special characters from string.
Parameters:
- s: str or ColoredString, string to clean
Returns:
str: cleaned string without color codes
"""
def disable():
"""
Disables colors globally.
Sets global DISABLE_COLOR flag to prevent color output.
"""CLINT_FORCE_COLOR=1 to force colors even in non-TTY environmentsColoredString objects support all standard string methods through the __getattr__ method:
colored_text = red("Hello World")
print(colored_text.upper()) # Returns RED "HELLO WORLD"
print(colored_text.split()) # Returns list with RED "Hello" and RED "World"
print(len(colored_text)) # Returns 11 (ignoring ANSI codes)Note: ColoredString is not included in the module's __all__ declaration but is used throughout the codebase and should be considered part of the public API for type annotations and advanced usage.
from clint.textui import puts
from clint.textui.colored import red, green, blue, yellow, clean
# Basic colored output
puts(red('This is red text'))
puts(green('This is green text'))
puts(blue('This is blue text', bold=True))
# Force color output (useful for logging/piping)
puts(red('Always red', always=True))
# String operations work with colored strings
colored_text = red('Hello')
result = colored_text + ' ' + blue('World')
puts(result) # "Hello World" with colors preserved
# Multiplication
puts(red('-') * 20) # Red dashes
# Length operations (ignores color codes)
text = red('Hello')
print(len(text)) # 5, not including ANSI codes
# Clean color codes from strings
dirty_text = red('Colored text')
clean_text = clean(dirty_text)
print(clean_text) # "Colored text" without color codes
# Mix with indentation and other formatting
from clint.textui import indent
puts("Status messages:")
with indent(2):
puts(green('✓ Success: Operation completed'))
puts(yellow('⚠ Warning: Minor issue detected'))
puts(red('✗ Error: Operation failed'))from clint.textui.colored import ColoredString, disable
import os
# Direct ColoredString usage
custom_colored = ColoredString('CYAN', 'Custom colored text', bold=True)
puts(custom_colored)
# Environment variable control
# Set CLINT_FORCE_COLOR=1 to force colors in non-TTY environments
os.environ['CLINT_FORCE_COLOR'] = '1'
puts(red('This will be colored even when piped'))
# Disable colors globally
disable()
puts(red('This will not be colored'))
# Complex formatting with colors
def status_report(items):
puts(blue('Status Report', bold=True))
puts(blue('=' * 20))
for item, status in items:
if status == 'success':
puts(f" {green('✓')} {item}")
elif status == 'warning':
puts(f" {yellow('⚠')} {item}")
else:
puts(f" {red('✗')} {item}")
# Usage
items = [
('Database connection', 'success'),
('Cache system', 'warning'),
('External API', 'error')
]
status_report(items)Clint automatically detects terminal capabilities:
CLINT_FORCE_COLOR=1 to force colors in non-TTY environmentsdisable() function to turn off colors entirelyimport sys
from clint.textui.colored import red
# This will be colored in terminal, plain when piped
puts(red('Status message'))
# This will always be colored
puts(red('Status message', always=True))
# Check if output is going to terminal
if sys.stdout.isatty():
puts("Output is going to terminal")
else:
puts("Output is being piped or redirected")Install with Tessl CLI
npx tessl i tessl/pypi-clint