skillfed

ansiwrap

textwrap, but savvy to ANSI colors and styles

ansiwrap v0.8.4 1.2M downloads/30d#4,202 on PyPI22
Permissive license Apache License 2.0 Active released

What it is and what it does

ansiwrap extends Python's standard textwrap module to handle ANSI escape sequences correctly. When you wrap or fill text containing ANSI color codes, textwrap miscalculates line length because it counts the raw bytes of invisible control codes. ansiwrap strips those codes during measurement, so wrapped lines respect actual display width, not byte length.

The package provides wrap(), fill(), and shorten() functions mirroring textwrap's API, plus utility functions like ansilen() to measure visible text length and strip_color() to remove codes entirely. It has no runtime dependencies and works across Python 2 and 3, making it straightforward to drop into terminal UI code or CLI tools that use colored output.

Use it for:

  • Format colored log output to fit terminal width without breaking lines due to invisible codes
  • Wrap help text in CLI tools that use ANSI colors for emphasis without miscounting line length
  • Truncate colored status messages to a fixed display width in terminal dashboards
  • Process multi-line colored text and reflow it to new widths while preserving styling
  • Strip ANSI codes from colored strings when you need plain text for logging or storage

Worth the install?

AI-flagged interpretation of the facts on this page — verify before relying

Wraps and formats text while correctly handling ANSI color and style codes, treating them as invisible to line length calculations the way textwrap cannot.

Yes. The package solves a real problem (textwrap + ANSI codes) with no dependencies, permissive licensing, and active maintenance. It's small, focused, and widely downloaded. Install it if you're building terminal UIs or CLI tools with colored output that need proper text wrapping.

Install

ansiwrap on PyPI

pip

pip install ansiwrap

uv

uv add ansiwrap

poetry

poetry add ansiwrap

Installing ansiwrap

Before you install

Low friction: pure Python wheel with no runtime dependencies. Maintenance is active with recent commits, though the latest release was in 2019.

License in practice

Apache License 2.0 is permissive; you can use this in commercial and proprietary projects with minimal restriction.

Quickstart

pip install ansiwrap
from ansiwrap import wrap, fill, shorten, ansilen, strip_color
lines = wrap('colored text', width=20)
filled = fill('colored text', width=20)
shortened = shorten('colored text', 20, placeholder='...')

Verify before relying

  • Whether the package handles all ANSI escape sequences or only color/style codes
  • Performance characteristics on very long strings or high-volume wrapping operations
  • Exact Python version support beyond the classifiers listed (2.6, 2.7, 3.3-3.7)

Package facts

License Apache License 2.0 (permissive)
Python support not specified
Install friction low — pure-Python wheel
Runtime dependencies none
Maintenance actively maintained — 2,760 days since the last release
Last repo commit
First released
Downloads 1,218,912/month — #4,202 on PyPI (30-day window, as of 2026-08-14)
Known vulnerabilities none known (OSV.dev, checked 2026-08-14)

Evidence: ansiwrap-0.8.4-py2.py3-none-any.whl

Keywords: text, textwrap, ANSI, colors

Development Status :: 4 - BetaEnvironment :: ConsoleIntended Audience :: DevelopersLicense :: OSI Approved :: Apache Software LicenseOperating System :: OS IndependentProgramming Language :: PythonProgramming Language :: Python :: 2Programming Language :: Python :: 2.6Programming Language :: Python :: 2.7Programming Language :: Python :: 3Programming Language :: Python :: 3.3Programming Language :: Python :: 3.4Programming Language :: Python :: 3.5Programming Language :: Python :: 3.6Programming Language :: Python :: 3.7Programming Language :: Python :: Implementation :: CPythonProgramming Language :: Python :: Implementation :: PyPyTopic :: Software Development :: Libraries :: Python ModulesTopic :: Text ProcessingTopic :: Text Processing :: Filters

Tags

ANSI color text wrappingtextwrap with color codeswrap colored terminal textANSI aware text formattingterminal text wrappingcolored text line breaking
terminal-uiansi-colors

More Python Modules packages