Skip to main content

A small library for fast color styling of the string using ANSI escape sequences.

Project description

pyansistring

pyansistring Banner

pyansistring is a small library for fast color styling of the string using ANSI escape sequences. The base class inherits from Python's str. You can split, join, slice the string while preserving the styling.

Inspired by rich Python library.

Features

  • Preservation of the str methods.
  • RGB foreground and background coloring.
  • Per-word coloring.
  • Align left, right and center without the problems caused by the length of the string.

For a more comprehensive list of what's been done so far, see the TODO section.

Install

Since the library is published on PyPI, it can be installed using pip:

pip install pyansistring

Or locally, by cloning the project:

git clone https://github.com/l1asis/pyansistring
cd ./pyansistring/
pip install .

Contributing

Any ideas (vectors) for improvements, actual help with implementations, bug fixes and anything else is highly appreciated. You can also contribute by adding your own art to the arts.py file in the .\src\pyansistring directory if you like.

Usage

from pyansistring import ANSIString
from pyansistring.constants import SGR, Foreground, Background

# Does what it should: prints all text in bold, with magenta foreground and white background.
print(ANSIString("Hello, World!").fg_4b(Foreground.MAGENTA).bg_4b(Background.WHITE).fm(SGR.BOLD))

# But you can do the same on a specific slice:
print(ANSIString("Hello, World!").fg_4b(Foreground.MAGENTA, (0, 4)).bg_4b(Background.WHITE, (2, 4)).fm(SGR.BOLD, (4, 6)))

# Or if you want to apply styles to a specific word
print(ANSIString("Hello, World!").fg_4b_w(Foreground.MAGENTA, "Hello", "World").bg_4b_w(Background.WHITE, "World").fm_w(SGR.BOLD, ","))

# You may find predefined colors boring, let's do it with RGB:
print(ANSIString("Hello, World!").fg_24b(255, 0, 255).bg_24b(255, 255, 255))

# And of course you can do the same tricks with words:
print(ANSIString("Hello, World!").fg_24b_w(255, 0, 255, "Hello").bg_24b_w(255, 255, 255, "World"))

# By the way...
print(len(ANSIString("Hello, World!").fg_4b(Foreground.MAGENTA)) == len("Hello, World!"))
# -> True

# Why? Because I wanted it to behave this way. But at the same time:
print(len(ANSIString("Hello, World!").fg_4b(Foreground.MAGENTA).styled) == len("Hello, World!"))
# -> False
print(ANSIString("Hello, World!").fg_4b(Foreground.MAGENTA).actual_length == len("Hello, World!"))
# -> False

# If you need the original string:
print(ANSIString("Hello, World!").fg_4b(Foreground.MAGENTA).plain)

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

pyansistring-0.0.2.tar.gz (49.4 kB view details)

Uploaded Source

Built Distribution

pyansistring-0.0.2-py3-none-any.whl (16.2 kB view details)

Uploaded Python 3

File details

Details for the file pyansistring-0.0.2.tar.gz.

File metadata

  • Download URL: pyansistring-0.0.2.tar.gz
  • Upload date:
  • Size: 49.4 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/5.1.1 CPython/3.12.5

File hashes

Hashes for pyansistring-0.0.2.tar.gz
Algorithm Hash digest
SHA256 f31c5ce32682b4249e2de2d1df4b6e97f6ba173c355dce08d796ab1c47f524f8
MD5 586a83b0efda4646eb906bfc29a43565
BLAKE2b-256 ca08143af4d608c033474abfc76ee23faa8e4db42f62ec99f5eff5e9409ff87e

See more details on using hashes here.

File details

Details for the file pyansistring-0.0.2-py3-none-any.whl.

File metadata

  • Download URL: pyansistring-0.0.2-py3-none-any.whl
  • Upload date:
  • Size: 16.2 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/5.1.1 CPython/3.12.5

File hashes

Hashes for pyansistring-0.0.2-py3-none-any.whl
Algorithm Hash digest
SHA256 1ba41de7259bc8b9ac06081e835161d69725785ead9ea3b19b3a0eabd1090b9b
MD5 c5c596ba4c9d31b5c8f39001d3515b87
BLAKE2b-256 3cc17711593d513604ed968ca2f255fc96f314bc5a92d04e7869565e03b76987

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