Skip to main content

textwrap, but savvy to ANSI colors and styles

Project description

Build Status Coverage Status Documentation Status GitHub Release Status PyPI Release Status PyPI Supported Python Versions License Status

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.

https://github.com/doblabs/ansiwrap-hotoffthehamster/blob/release/docs/assets/00000569.png

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

ansiwrap_hotoffthehamster-0.8.7.tar.gz (31.4 kB view details)

Uploaded Source

Built Distribution

File details

Details for the file ansiwrap_hotoffthehamster-0.8.7.tar.gz.

File metadata

File hashes

Hashes for ansiwrap_hotoffthehamster-0.8.7.tar.gz
Algorithm Hash digest
SHA256 caaf939682b9ba306c7a7c80dbf31ff9f2e460a0943c6899fc70660766447aa1
MD5 7a5cb9d2501a7ce825f767c19df55c7b
BLAKE2b-256 c43dcd19ac1313ccaf2cfe2fb8f9f6c09de12cc8b0d1a8a4badc4591929ad51c

See more details on using hashes here.

File details

Details for the file ansiwrap_hotoffthehamster-0.8.7-py3-none-any.whl.

File metadata

File hashes

Hashes for ansiwrap_hotoffthehamster-0.8.7-py3-none-any.whl
Algorithm Hash digest
SHA256 adcfa28d63393b2d50fb1186bfd3587cb8a6caf2a0039a115acecec71486c101
MD5 11b6c38530cf499dedf176467f2a72d7
BLAKE2b-256 0007abd70163c49fe61e1991d24cd0c8c367769a42c27a81bc5168d24b1037f0

See more details on using hashes here.

Supported by

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