Tools for easily reading and manipulating text with formatting provided by ANSI escape sequences
Project description
ANSI Text
This repo contains code for reading and manipulating text that has had formatting applied using ANSI escape sequences (colored foreground, colored background, bold, underline, etc.).
This isn't really all that useful for composing ANSI formatted text, it's more of a utility for easily editing text that already has had ANSI formatting applied to it. Once text has been read into an AnsiText object, the text can then be accessed and manipulated in a number of ways.
Introduction
ANSI escape sequences are special sequences of characters that tell a terminal, terminal-like interpreter, Jupyter Notebook, etc. to do something. They can do a lot of stuff, but for the purposes of this README we'll just say "ANSI escape sequences are magical strings of characters that can change the color of text or apply formatting (like making the text bold or underlined)." If you'd like to learn more, this Wikipedia article goes more in depth.
Here is an example of some ANSI formatted text: There's a chunk at the beginning that tells the terminal to to set the text color to blue:
\x1b[38;5;12m
Then there's the text:
ANSI formatted text
And finally there's a sequence that tells the terminal to reset all attributes to their default values:
\x1b[0m
Reading text into AnsiText object:
If you have a string of text that already has ANSI formatting applied to it, you can read it into an AnsiText object when the AnsiText object is created:
>>> text = '\x1b[38;5;12mANSI formatted text\x1b[0m'
>>> atext = AnsiText(text)
or by using the 'read' method:
>>> text = '\x1b[38;5;12mANSI formatted text\x1b[0m'
>>> atext.read(text)
Reading from AnsiText object:
Operations that use the str() method, such as print() or str(), will receive the formatted text. If you use str(), you'll be able to see the escape sequences used to format the text. If you use print(), the colored string will be visible in your terminal:
The unformatted text can be accessed using the 'text' attribute:
>>> text = '\x1b[38;5;12mANSI formatted text\x1b[0m'
>>> atext = AnsiText(text)
>>> atext.text
ANSI formatted text
Editing AnsiText object:
When the text is read into the AnsiText object it will detect if the formatting changes through the string. The plaintext and formatting information for each of these is stored in an AnsiSubString object.
These substrings can be accessed either through the groups attribute, or by indexing into the AnsiText object. This also allows for item assignment, which will replace the plaintext for a given group while retaining the ANSI formatting. This means that the size of the text can change (see below, where 'stuff' is replaced with 'Doggos!').
Alternate indexing mode
There are some use cases where the method of indexing demonstrated above wouldn't make sense. For example, if the color of the text changes with each letter, as would be the case if a gradient has been applied to it. In this situation it would be very cumbersome to edit the text, since the user would need to assign each letter to a separate group.
The alternate indexing mode addresses this problem. When this mode is active, indexing operations will refer to the unformatted text as a whole. No matter how many groups there are in, AnsiText object atext accessed as atext[n] will return the character at position 'n' in the unformatted text.
You can think of the unformatted text as a list: this allows for slicing operations, etc., but it may lead to some unexpected behavior. The text will always be truncated to the length of the original unformatted text, and if you provide a string that's larger than the span of the slice it will "run over", overwriting text past the stop value.
The user can cause the AnsiText object to use the alternate indexing mode either by setting the 'index_groups' argument to False when the object is created, or by setting the 'index_groups' attribute to False.
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 ansi_text-1.0.1.tar.gz
.
File metadata
- Download URL: ansi_text-1.0.1.tar.gz
- Upload date:
- Size: 243.1 kB
- Tags: Source
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/4.0.1 CPython/3.10.5
File hashes
Algorithm | Hash digest | |
---|---|---|
SHA256 | b999c912ff158cd3eb0d1bef990d6a0e9f5dc73cbb02a88262dfce2eed9526a4 |
|
MD5 | 6c36247e21b55bef948f00c9d1d72012 |
|
BLAKE2b-256 | 8f6f84cbed0235fac2fb4f6bf468fab9d9b2a1c984fab1faeb01fdbeecf3f07e |
File details
Details for the file ansi_text-1.0.1-py3-none-any.whl
.
File metadata
- Download URL: ansi_text-1.0.1-py3-none-any.whl
- Upload date:
- Size: 6.5 kB
- Tags: Python 3
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/4.0.1 CPython/3.10.5
File hashes
Algorithm | Hash digest | |
---|---|---|
SHA256 | 3e0bd0fc2f9843993203b3cc191c4721bc632d485c333076fb5c33693e3e25d5 |
|
MD5 | 0a1214354074bae71171c51a96f2c7e2 |
|
BLAKE2b-256 | 2d863bed6ae024318346161172d51d8adbe9ccf8846d70f2400896a5212803b1 |