A quick and user-friendly way to style your text using ANSI codes
Project description
Antsi
A quick and user-friendly way to style your text using ANSI codes.
Built around a delightfully simple markup language, there's no messing about with codes or style resets. All the ANSI code conversion, handling of overlapping styles, and terminal support is automatically handled for you.
Usage
You can either download wheels from the Releases tab or install it from PyPI:
pip install antsi
The function you'll most likely care about is colorize
. Colorize is responsible for converting any styled markup
within the provided source to ANSI escape sequences. The styled markup syntax can be found below.
You can use the following code to test that it's working as expected.
import antsi
print(antsi.colorize("Hello [fg:green](there!)"))
print(antsi.colorize("It looks like [fg:black;bg:white;deco:bold,blink](antsi) is working!"))
If you're planning on doing any string manipulation or templating with styled markup, you'll want to escape
the
substituted text to prevent any unexpected colors or errors. The escape
function ensures that any control characters
are properly escaped.
Styled Markup
Styled markup is a decidedly simple language that is make for ease of use and readability. There's no unruly sequences of numbers and characters that you'll forget what they mean in a week, just words and a couple memorable abbreviations.
Styled markup can appear anywhere within a piece of text. It can even be nested infinitely! The only requirements are that it start with a style specifier and is followed by some content. It will always be in the form:
[ <style specifier(s)> ]( <content> )
For example, Hello [fg:green](there), user!
which will make only "there" be colored green. Everything else will be the
default text color.
Content
The content is what the style specifier applies to. It always immediately follows a style specifier and must be wrapped
in parentheses. Any whitespace within the content will be emitted as-is unless escaped using a
backslash (\
).
[!IMPORTANT]
Putting any character(s) between the style specifier and content is explicitly disallowed.
The content can contain any characters, and it can even contain other styled markup! If you want to use any square brackets or parentheses in the content, they must be escaped.
[!TIP]
When nesting styled markup, the styles of the parent markup will also be applied unless explicitly overridden. However, there is currently no way to remove text decorations from the children of nested markup.
Style Specifiers
Style specifiers are surrounded by square brackets ([]
) and contain the directives for applying style to the content
immediately following them. They are essentially a list of key-value pairs, where the key denotes the type of style.
The styles within a specifier will ignore whitespace to facilitate readability. However, whitespace cannot exist in the
middle of a key or value (i.e. bl ue
will cause an error). The benefit of this is that [ fg: red ; bg: blue ]
will be parsed the same as [fg:red;bg:blue]
, so use whatever style you prefer.
Examples:
Style Specifier | Meaning |
---|---|
[fg:red] |
Red foreground |
[bg:blue] |
Blue background |
[deco:bold] |
Bold text |
[deco:bold,blink] |
Bold and blinking |
[fg:red;bg:white] |
Red foreground and white background |
[bg:blue;fg:white;deco:bold] |
White foreground, blue background, and bold |
[deco:bold,underline;fg:cyan] |
Cyan foreground, bold, and underlined |
[fg:red;bg:white;fg:blue] |
Blue foreground and white background (last specifier wins) |
[!IMPORTANT]
If any tags are repeated in the specifier, the value of the last tag takes precedence. This means that tags which can accept multiple values (i.e.
deco
) are not merged.
As show above, there are currently three specifiers that are allowed: fg
(
foreground), bg
(background), and deco
(decoration).
Foreground color (fg
)
Format: fg:<color>
Changes the color of the text itself. Currently only the standard color pallet is implemented, providing 8 colors with a
standard and a bright variant. The bright variant can be chosen by prefixing the color with bright-
.
[!TIP]
Colors will appear differently depending on the terminal being used.
Color | Standard Code | Bright Code |
---|---|---|
Default | default |
N/A |
Black | black |
bright-black |
Red | red |
bright-red |
Green | green |
bright-green |
Yellow | yellow |
bright-yellow |
Blue | blue |
bright-blue |
Magenta | magenta |
bright-magenta |
Cyan | cyan |
bright-cyan |
White | white |
bright-white |
Background color (bg
)
Format: bg:<color>
Changes the color of the text background. It accepts the same colors as fg
.
Text decoration (deco
)
Format: deco:<decoration>,[<decoration>...]
Applies additional text decorations like bolding, dimming, blinking, etc. Unlike the foreground and background, multiple text decorations can be applied at the same time using a comma-separated list.
[!TIP]
Support for decorations may differ between terminals.
Decoration | Code(s) |
---|---|
Bold | bold |
Dim | dim , faint |
Italic | italic |
Underline | underline |
Fast Blink | fast-blink , blink-fast |
Slow Blink | slow-blink , blink-slow |
Invert | invert , reverse |
Hide | hide , conceal |
Strikethrough | strike-through , strikethrough |
Escape Sequences
There are a handful of control characters that must be escaped to include them anywhere in your text. This includes text outside styled markup.
Character | Sequence |
---|---|
\ |
\\ |
[ |
\[ |
] |
\] |
( |
\( |
) |
\) |
Beyond these characters, you can also escape any whitespace (i.e. spaces, tabs, newlines, and carriage returns) to make
writing multi-line text easier. All you need to do is prefix it with a backslash (\
).
Contributing
Any and all contributions are welcome! Some ideas if you can't think of anything:
- Improve the documentation
- Add some functionality that might be missing
- Report (or even fix) any bugs
If you don't have time to contribute yourself but still wish to support the project, sponsorship would be greatly appreciated!
License
Licensed under the MIT license (or http://opensource.org/licenses/MIT).
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
Built Distributions
File details
Details for the file antsi-0.2.1.tar.gz
.
File metadata
- Download URL: antsi-0.2.1.tar.gz
- Upload date:
- Size: 44.9 kB
- Tags: Source
- Uploaded using Trusted Publishing? Yes
- Uploaded via: maturin/1.5.1
File hashes
Algorithm | Hash digest | |
---|---|---|
SHA256 | c8ffa86ada62ed163c718d7497419c413d5270ea9ac848eab12760270a70df47 |
|
MD5 | b4c6469b13515a81f45e8f718d88b90b |
|
BLAKE2b-256 | 38e3d2438c04c9d3bedf849324b3655b3fe5e5ccbf09701c53cec32301b59d39 |
File details
Details for the file antsi-0.2.1-cp310-abi3-win_amd64.whl
.
File metadata
- Download URL: antsi-0.2.1-cp310-abi3-win_amd64.whl
- Upload date:
- Size: 195.4 kB
- Tags: CPython 3.10+, Windows x86-64
- Uploaded using Trusted Publishing? Yes
- Uploaded via: maturin/1.5.1
File hashes
Algorithm | Hash digest | |
---|---|---|
SHA256 | 7c506372762c9f4e4a191cdf77871c0b108b7bc44f3d01804da35ee5b10b6a6b |
|
MD5 | 8e2c529896a2100a677e4bba2bd355d3 |
|
BLAKE2b-256 | 01ed29221ebe82eb574b15cafb24bca6fe4a8eb68b259cd31cfeb3f940b83b58 |
File details
Details for the file antsi-0.2.1-cp310-abi3-win32.whl
.
File metadata
- Download URL: antsi-0.2.1-cp310-abi3-win32.whl
- Upload date:
- Size: 189.1 kB
- Tags: CPython 3.10+, Windows x86
- Uploaded using Trusted Publishing? Yes
- Uploaded via: maturin/1.5.1
File hashes
Algorithm | Hash digest | |
---|---|---|
SHA256 | 03bca8c0efe592c2a11919d3fb43e4890f3bf7fb058aae8fc161839e47cc1f8e |
|
MD5 | b6e9112fd6dedd84644fe560eef08a0d |
|
BLAKE2b-256 | 99215d020ec1501e76c08c486a9eb9ffe9ea79e0f92a093bc25d53d728643a72 |
File details
Details for the file antsi-0.2.1-cp310-abi3-manylinux_2_17_x86_64.manylinux2014_x86_64.whl
.
File metadata
- Download URL: antsi-0.2.1-cp310-abi3-manylinux_2_17_x86_64.manylinux2014_x86_64.whl
- Upload date:
- Size: 1.1 MB
- Tags: CPython 3.10+, manylinux: glibc 2.17+ x86-64
- Uploaded using Trusted Publishing? Yes
- Uploaded via: maturin/1.5.1
File hashes
Algorithm | Hash digest | |
---|---|---|
SHA256 | c0e8078851525773d1b37dc8b15a431b3246887ff84bfbb476697306f31b23ff |
|
MD5 | 8b3f28aa0c6da4ccb50f46931552d304 |
|
BLAKE2b-256 | 5422bb316d1f13c358fcd5aba6cfcabc745861f0634e98d60f3a0cf6f281a44c |
File details
Details for the file antsi-0.2.1-cp310-abi3-manylinux_2_17_s390x.manylinux2014_s390x.whl
.
File metadata
- Download URL: antsi-0.2.1-cp310-abi3-manylinux_2_17_s390x.manylinux2014_s390x.whl
- Upload date:
- Size: 1.2 MB
- Tags: CPython 3.10+, manylinux: glibc 2.17+ s390x
- Uploaded using Trusted Publishing? Yes
- Uploaded via: maturin/1.5.1
File hashes
Algorithm | Hash digest | |
---|---|---|
SHA256 | 74dbf45ea38e60ca64504966e33815c49e4334e85ecfce8a5e5c6331798287da |
|
MD5 | bb522c42ca5a2838450abc648cbe9f2f |
|
BLAKE2b-256 | 035a81ad575c73e926523f49e8d3de020dfd73c928d52b90249dad914fad472d |
File details
Details for the file antsi-0.2.1-cp310-abi3-manylinux_2_17_ppc64le.manylinux2014_ppc64le.whl
.
File metadata
- Download URL: antsi-0.2.1-cp310-abi3-manylinux_2_17_ppc64le.manylinux2014_ppc64le.whl
- Upload date:
- Size: 1.2 MB
- Tags: CPython 3.10+, manylinux: glibc 2.17+ ppc64le
- Uploaded using Trusted Publishing? Yes
- Uploaded via: maturin/1.5.1
File hashes
Algorithm | Hash digest | |
---|---|---|
SHA256 | 6d67943f872094688230de432ed4d0a770c8481eb9487f376a69655b0f08e2fd |
|
MD5 | 92ef639ecfcbbe01956d352345634bc1 |
|
BLAKE2b-256 | 13e27d4c3929c84aaa4f2e4dce333c6c4319dc7e727de3469fc789b7c3938d15 |
File details
Details for the file antsi-0.2.1-cp310-abi3-manylinux_2_17_armv7l.manylinux2014_armv7l.whl
.
File metadata
- Download URL: antsi-0.2.1-cp310-abi3-manylinux_2_17_armv7l.manylinux2014_armv7l.whl
- Upload date:
- Size: 1.1 MB
- Tags: CPython 3.10+, manylinux: glibc 2.17+ ARMv7l
- Uploaded using Trusted Publishing? Yes
- Uploaded via: maturin/1.5.1
File hashes
Algorithm | Hash digest | |
---|---|---|
SHA256 | dd9d6e1f872d31f0513946cbfbc872bf5c41383628f400e51a8156ab6f02ee98 |
|
MD5 | db9144feffe1913437e0ac32a8f5897c |
|
BLAKE2b-256 | 9bdcb688b1c333d69dd1dc816cad3b5e97d73acee53d239b2a6aa1b556d0077c |
File details
Details for the file antsi-0.2.1-cp310-abi3-manylinux_2_17_aarch64.manylinux2014_aarch64.whl
.
File metadata
- Download URL: antsi-0.2.1-cp310-abi3-manylinux_2_17_aarch64.manylinux2014_aarch64.whl
- Upload date:
- Size: 1.1 MB
- Tags: CPython 3.10+, manylinux: glibc 2.17+ ARM64
- Uploaded using Trusted Publishing? Yes
- Uploaded via: maturin/1.5.1
File hashes
Algorithm | Hash digest | |
---|---|---|
SHA256 | 2e70b38a4c2d93f08a3c56583e1c17845e1fafa9bfa8c95963d3d6c4a3548eaa |
|
MD5 | 3254ea490a21f44c0f4560b8c9ad974c |
|
BLAKE2b-256 | 3e9619f7755d43fbabbf1ef4e00f1787a983bc5f7c338d433d3aa071c6f57266 |
File details
Details for the file antsi-0.2.1-cp310-abi3-manylinux_2_5_i686.manylinux1_i686.whl
.
File metadata
- Download URL: antsi-0.2.1-cp310-abi3-manylinux_2_5_i686.manylinux1_i686.whl
- Upload date:
- Size: 1.1 MB
- Tags: CPython 3.10+, manylinux: glibc 2.5+ i686
- Uploaded using Trusted Publishing? Yes
- Uploaded via: maturin/1.5.1
File hashes
Algorithm | Hash digest | |
---|---|---|
SHA256 | 1a0c5a2d8e66204d19b9d491071fc87e54244dda682e72bc9ee493ac08ae1941 |
|
MD5 | cdd90c2bbd4fb151000f7bc0af338f7f |
|
BLAKE2b-256 | 34fdd119eedfb8fd057c8c2aa3d425e5bcdc05e4a7db200b13112a34840db8c2 |
File details
Details for the file antsi-0.2.1-cp310-abi3-macosx_11_0_arm64.whl
.
File metadata
- Download URL: antsi-0.2.1-cp310-abi3-macosx_11_0_arm64.whl
- Upload date:
- Size: 310.1 kB
- Tags: CPython 3.10+, macOS 11.0+ ARM64
- Uploaded using Trusted Publishing? Yes
- Uploaded via: maturin/1.5.1
File hashes
Algorithm | Hash digest | |
---|---|---|
SHA256 | e755bbf64b747a8979e9ab8c0983f02c958cb31909ee224d09122e603583a370 |
|
MD5 | 8fd6f4bf45b5f9bd5dbbcb3c72b9ac86 |
|
BLAKE2b-256 | c6a5f9dc16dfecf65dea55f28d2ed7304308cbf890278385d62b1b640733e5c1 |
File details
Details for the file antsi-0.2.1-cp310-abi3-macosx_10_12_x86_64.whl
.
File metadata
- Download URL: antsi-0.2.1-cp310-abi3-macosx_10_12_x86_64.whl
- Upload date:
- Size: 313.0 kB
- Tags: CPython 3.10+, macOS 10.12+ x86-64
- Uploaded using Trusted Publishing? Yes
- Uploaded via: maturin/1.5.1
File hashes
Algorithm | Hash digest | |
---|---|---|
SHA256 | ad56e659e39a53c09485aeba9a2f0e2b20b0f6e4013c7f120941638632e5fe0b |
|
MD5 | 586cff5d559860572b3d7f168f3be1b8 |
|
BLAKE2b-256 | f20374103d17f1a5fbab7bb1b35fce20703347dc1428cbac2efd1554fad5a8f3 |