ASCII Colors
A Python library for rich terminal output with advanced logging features. It provides functionalities for styling text, creating progress bars, and handling advanced terminal interactions. The current version is 0.11.21, with active development featuring regular updates.
Common errors
-
^[[31mThis is red text.^[[0m (or similar literal ANSI escape sequences)
cause The output is being redirected to a file, piped to another command, or viewed in an IDE terminal that does not interpret ANSI escape codes, leading to raw codes being printed.fixEnsure the output is directed to a compatible terminal emulator (e.g., Windows Terminal, iTerm2). When logging to files, use `ascii_colors.set_log_file()` to automatically strip codes or ensure your log formatters handle it. -
AttributeError: type object 'ASCIIColors' has no attribute 'print_ascii_art'
cause The `print_ascii_art` method was moved from being a static method of the `ASCIIColors` class to a standalone function in the `ascii_colors` module in versions 0.11.16 and later.fixUpdate your code to call the function directly from the module: `ascii_colors.print_ascii_art(...)` (or `ac.print_ascii_art(...)` if using an alias). -
Colors are not showing up in my terminal, only plain text.
cause Your terminal emulator does not support ANSI escape codes, or support is disabled. This is particularly common in older versions of Windows Command Prompt without additional tools or modern terminal applications.fixUse a modern terminal emulator (e.g., Windows Terminal, Alacritty, iTerm2, Kitty, GNOME Terminal). Verify your terminal's settings to ensure ANSI color code interpretation is enabled.
Warnings
- breaking The `print_ascii_art` method was moved from being a method of the `ASCIIColors` class to a top-level function directly in the `ascii_colors` module.
- breaking Several logging configuration methods, such as `set_logging_level` and `set_level_colors`, were renamed or removed during the 0.11.x development cycle.
- gotcha Color codes and ASCII art may display incorrectly or fill output with raw escape sequences when redirected to a file or piped to a non-terminal application, as `ascii-colors` assumes a TTY by default.
Install
-
pip install ascii-colors
Imports
- ascii_colors
import ascii_colors as ac
- ASCIIColors
from ascii_colors import ASCIIColors
Quickstart
import ascii_colors as ac
# Basic text coloring
print(ac.red("This is red text."))
print(ac.green("This is green text."))
# Chaining styles (e.g., bold and blue)
print(ac.bold + ac.blue("Bold blue text.") + ac.reset)
# Background and foreground colors
print(ac.bg_yellow + ac.black("Black text on a yellow background.") + ac.reset)
# Using the advanced logging features
ac.log_info("This is an informational message.")
ac.log_warning("This is a warning message.")
ac.log_error("This is an error message.")
# Example of ASCII art (requires Pygments for complex fonts)
# For simple fonts, it often works without complex dependencies.
ac.print_ascii_art("Hello Registry!", font="slant")