Pygments ANSI Color

0.3.0 · maintenance · verified Fri Apr 17

Pygments ANSI Color (version 0.3.0) provides a Pygments lexer specifically designed to highlight ANSI escape codes for colors and styles. It allows Pygments to correctly render colored terminal output, making it readable and formatted. The library has a slow release cadence, with the last update in September 2022, but remains functional for its intended purpose.

Common errors

Warnings

Install

Imports

Quickstart

This quickstart demonstrates how to use `AnsiColorLexer` with Pygments' `highlight` function and `TerminalFormatter` to render a string containing ANSI color codes directly to your terminal. Ensure your terminal supports ANSI escape sequences for colors to display correctly.

from pygments import highlight
from pygments.formatters import TerminalFormatter
from pygments_ansi_color import AnsiColorLexer

# Example string with ANSI color codes
ansi_string = "\u001b[31mError: Something went wrong!\u001b[0m \u001b[32mSuccess: Operation completed.\u001b[0m"

# Highlight the string using AnsiColorLexer and output to terminal
highlighted_output = highlight(ansi_string, AnsiColorLexer(), TerminalFormatter())

print(highlighted_output)

view raw JSON →