Skip to main content

A simple, zero-dependency package for adding ANSI color and style to terminal text.

Project description

PyAnsiWrap 🎨

A simple, zero-dependency Python class for adding color and style to your terminal text.

Ansiwrap provides a clean, fluent API to make your command-line applications more readable and visually appealing. It works on any terminal that supports ANSI escape codes (like most Linux, macOS, and modern Windows terminals).

Key Features

  1. Zero Dependencies: Pure Python, no external packages needed.
  2. Simple API: An intuitive, easy-to-learn set of static methods.
  3. Flexible Styling: Supports foreground/background colors, bold, underline, reverse, and more.
  4. Case-Insensitive: Color names can be red, RED, or Red.
  5. Global Control: Easily enable or disable all styling with a single command.
  6. Pipe-Aware: Automatically disables color when output is redirected to a file.

Installation

Install Ansiwrap directly from pip:

pip install pyansiwrap

Quick Start

Ansiwrap is designed to be straightforward. You can style a specific piece of text or apply a style to a whole block.

Styling a single string

This is the most common use case. The style is automatically applied and reset.

from pyansiwrap import Ansiwrap

# --- Foreground Colors ---
print("Status:", Ansiwrap.fg("green", "SUCCESS", bold=True))
print("Error:", Ansiwrap.fg("red", "File not found."))

# --- Background Colors ---
print(Ansiwrap.bg("yellow", Ansiwrap.fg("black", " WARNING ")))

# --- Other Styles ---
print("Task:", Ansiwrap.strikethrough("Review documents"), "(Complete)")
print("Selection:", Ansiwrap.reverse(" Menu Item 1 "))

Styling a block of text

Use the start_* methods to begin a style and reset() to end it.

from pyansiwrap import Ansiwrap

# Combine start codes by adding them together
print(Ansiwrap.start_fg("cyan") + Ansiwrap.start_underline(), end="")

print("System Log")
print("----------")
print("All systems are operating normally.")

# Return to the default terminal style
print(Ansiwrap.reset(), "Log finished.")

API Reference

All methods are static and can be called directly on the Ansiwrap class.

Wrapper methods

These methods style a specific text string and automatically append a reset code.

  • Ansiwrap.fg(color_name, text, bold=False): Styles foreground color.
  • Ansiwrap.bg(color_name, text): Styles background color.
  • Ansiwrap.underline(text)
  • Ansiwrap.reverse(text)
  • Ansiwrap.conceal(text)
  • Ansiwrap.strikethrough(text)

Starter methods

These methods return only the opening ANSI code to start a persistent style.

  • Ansiwrap.start_fg(color_name, bold=False)
  • Ansiwrap.start_bg(color_name)
  • Ansiwrap.start_underline()
  • Ansiwrap.start_reverse()
  • Ansiwrap.start_conceal()
  • Ansiwrap.start_strikethrough()

Global controls

  • Ansiwrap.enable(): Globally enables all styling.
  • Ansiwrap.disable(): Globally disables all styling.
  • Ansiwrap.reset(): Returns the code to reset all active styles.

Cross platform support

Ansiwrap works out-of-the-box on modern terminals. For compatibility with the legacy Windows cmd.exe, you can use it alongside the colorama library.

Initialize colorama at the start of your script to get the best of both worlds: colorama's robust Windows support and Ansiwrap's clean API.

import colorama
from pyansiwrap import Ansiwrap

# Let colorama handle Windows compatibility
colorama.init()

# Now use Ansiwrap's API as usual
print(Ansiwrap.fg("magenta", "This will work everywhere!"))

Project details


Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

pyansiwrap-1.0.0.tar.gz (3.9 kB view details)

Uploaded Source

Built Distribution

If you're not sure about the file name format, learn more about wheel file names.

pyansiwrap-1.0.0-py3-none-any.whl (4.4 kB view details)

Uploaded Python 3

File details

Details for the file pyansiwrap-1.0.0.tar.gz.

File metadata

  • Download URL: pyansiwrap-1.0.0.tar.gz
  • Upload date:
  • Size: 3.9 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/6.2.0 CPython/3.12.9

File hashes

Hashes for pyansiwrap-1.0.0.tar.gz
Algorithm Hash digest
SHA256 bff833cab43dacd0cf0ca488a93e5583759b721370ef3f9de9963c0faad52c1b
MD5 83d6ea97d942a93954b1c1bfdd6c3229
BLAKE2b-256 bb75958defdcc2210753db9b1b9c04203af0b514235c158ef36868d53bc9359a

See more details on using hashes here.

File details

Details for the file pyansiwrap-1.0.0-py3-none-any.whl.

File metadata

  • Download URL: pyansiwrap-1.0.0-py3-none-any.whl
  • Upload date:
  • Size: 4.4 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/6.2.0 CPython/3.12.9

File hashes

Hashes for pyansiwrap-1.0.0-py3-none-any.whl
Algorithm Hash digest
SHA256 a7121a6f6aa8e8b8e4372dca0a3c5036d065532c668ffd8c109f092648a4982f
MD5 36c2924ca4b02e9f15dea55671e201b6
BLAKE2b-256 174957bf5fdb1b3ff435f940d2a6c0b76ba09a31c7482948297641f2b061e147

See more details on using hashes here.

Supported by

AWS Cloud computing and Security Sponsor Datadog Monitoring Depot Continuous Integration Fastly CDN Google Download Analytics Pingdom Monitoring Sentry Error logging StatusPage Status page