---
title: "ansiwrap | pypx"
description: "textwrap, but savvy to ANSI colors and styles"
canonical_url: "https://pypx.app/packages/ansiwrap"
meta:
  "og:description": "textwrap, but savvy to ANSI colors and styles"
  "og:image": "https://pypx.app/_og/d/h_630%2Cc_PackageCard%2Cname_ansiwrap%2Cversion_0.8.4%2Csummary_~dGV4dHdyYXAsIGJ1dCBzYXZ2eSB0byBBTlNJIGNvbG9ycyBhbmQgc3R5bGVz%2Clicense_Apache%2BSoftware%2BLicense%2Cp_Ii9wYWNrYWdlcy9hbnNpd3JhcCI%2Cs_TO43sLrK6a2OGHK2.png"
  "og:image:height": "630"
  "og:image:type": image/png
  "og:image:width": "1200"
  "og:title": "ansiwrap 0.8.4"
  "twitter:image": "https://pypx.app/_og/d/h_630%2Cc_PackageCard%2Cname_ansiwrap%2Cversion_0.8.4%2Csummary_~dGV4dHdyYXAsIGJ1dCBzYXZ2eSB0byBBTlNJIGNvbG9ycyBhbmQgc3R5bGVz%2Clicense_Apache%2BSoftware%2BLicense%2Cp_Ii9wYWNrYWdlcy9hbnNpd3JhcCI%2Cs_TO43sLrK6a2OGHK2.png"
---

> Index of every page on this site: [llms.txt](https://pypx.app/llms.txt)

# **ansiwrap**

v0.8.4

textwrap, but savvy to ANSI colors and styles

8 KBpy2.py3-none-anyApache Software License Likely Unmaintained

[** Diff **](https://pypx.app/packages/ansiwrap/diff)

`$ uv add ansiwrap`

| [![PyPI Package latest release](http://img.shields.io/pypi/v/ansiwrap.svg?style=flat)](https://pypi.python.org/pypi/ansiwrap) [![Supported versions](https://img.shields.io/pypi/pyversions/ansiwrap.svg)](https://pypi.python.org/pypi/ansiwrap) [![Supported implementations](https://img.shields.io/pypi/implementation/ansiwrap.svg)](https://pypi.python.org/pypi/ansiwrap) [![Wheel packaging support](https://img.shields.io/pypi/wheel/ansiwrap.svg)](https://pypi.python.org/pypi/ansiwrap) [![Test line coverage](https://img.shields.io/badge/test_coverage-99%25-0000FF.svg)](https://pypi.python.org/pypi/ansiwrap)

ansiwrap wraps text, like the standard textwrap module. But it also correctly wraps text that contains ANSI control sequences that colorize or style text.

Where textwrap is fooled by the raw string length of those control codes, ansiwrap is not; it understands that however much those codes affect color and display style, they have no logical length.

The API mirrors the wrap, fill, and shorten functions of textwrap. For example::

from \_\_future\_\_ import print\_function from colors import *# ansicolors on PyPI from ansiwrap import*

s = ' '.join(\[red('this string'), blue('is going on a bit long'), green('and may need to be'), color('shortened a bit', fg='purple')])

print('-- original string --') print(s) print('-- now filled --') print(fill(s, 20)) print('-- now shortened / truncated --') print(shorten(s, 20, placeholder='...'))

It also exports several other functions:

- ansilen (giving the effective length of a string, ignoring ANSI control codes)
- ansi\_terminate\_lines (propagates control codes though a list of strings/lines

and terminates each line.)

- strip\_color (removes ANSI control codes from a string)

See also the enclosed demo.py.

## **Details **

<dl>

<dt>Version</dt>
<dd>0.8.4</dd>

<dt>License</dt>
<dd>Apache Software License</dd>

<dt>Maintainer</dt>
<dd>Jonathan Eunice</dd></dl>

## **Links**

- [Homepage](https://github.com/jonathaneunice/ansiwrap)
- [View on PyPI](https://pypi.org/project/ansiwrap/)

## **Maintainers **

- Jonathan Eunice · [jonathan.eunice@gmail.com](mailto:jonathan.eunice@gmail.com)

```json
{"@context":"https://schema.org","@graph":[{"@id":"https://pypx.app/#website","@type":"WebSite","description":"The Python Package Index, reimagined. Fast search, dependency insights, download trends, and changelogs — all in one place.","name":"pypx","url":"https://pypx.app/"},{"@id":"https://pypx.app/packages/ansiwrap#webpage","@type":"WebPage","description":"textwrap, but savvy to ANSI colors and styles","name":"ansiwrap","url":"https://pypx.app/packages/ansiwrap","isPartOf":{"@id":"https://pypx.app/#website"},"potentialAction":[{"@type":"ReadAction","target":["https://pypx.app/packages/ansiwrap"]}]},{"@id":"https://pypx.app/packages/ansiwrap#/schema/software-application/1","@type":"SoftwareApplication","applicationCategory":"DeveloperApplication","description":"textwrap, but savvy to ANSI colors and styles","license":"Apache Software License","name":"ansiwrap","softwareVersion":"0.8.4"}]}
```

## Sitemap

See the full [sitemap](https://pypx.app/sitemap.md) for all pages.
