textwrap, but savvy to ANSI colors and styles
Project description
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.
Project details
Release history Release notifications | RSS feed
Download files
Download the file for your platform. If you're not sure which to choose, learn more about installing packages.
Source Distribution
Built Distribution
File details
Details for the file ansiwrap_hotoffthehamster-0.8.7.tar.gz
.
File metadata
- Download URL: ansiwrap_hotoffthehamster-0.8.7.tar.gz
- Upload date:
- Size: 31.4 kB
- Tags: Source
- Uploaded using Trusted Publishing? Yes
- Uploaded via: twine/4.0.2 CPython/3.11.7
File hashes
Algorithm | Hash digest | |
---|---|---|
SHA256 | caaf939682b9ba306c7a7c80dbf31ff9f2e460a0943c6899fc70660766447aa1 |
|
MD5 | 7a5cb9d2501a7ce825f767c19df55c7b |
|
BLAKE2b-256 | c43dcd19ac1313ccaf2cfe2fb8f9f6c09de12cc8b0d1a8a4badc4591929ad51c |
File details
Details for the file ansiwrap_hotoffthehamster-0.8.7-py3-none-any.whl
.
File metadata
- Download URL: ansiwrap_hotoffthehamster-0.8.7-py3-none-any.whl
- Upload date:
- Size: 8.1 kB
- Tags: Python 3
- Uploaded using Trusted Publishing? Yes
- Uploaded via: twine/4.0.2 CPython/3.11.7
File hashes
Algorithm | Hash digest | |
---|---|---|
SHA256 | adcfa28d63393b2d50fb1186bfd3587cb8a6caf2a0039a115acecec71486c101 |
|
MD5 | 11b6c38530cf499dedf176467f2a72d7 |
|
BLAKE2b-256 | 0007abd70163c49fe61e1991d24cd0c8c367769a42c27a81bc5168d24b1037f0 |