Yet another ANSI escape sequence library for Python - now modernised!
Project description
escapyde
Yet another ANSI escape sequence library for Python - now modernised!
Installation
The package is readily available on PyPI. There are no dependencies, but Python 3.9 or newer is required.
On Windows:
py -m pip install escapyde
On other platforms:
pip3 install escapyde
Changelog
You can find the project changelog here.
Usage
For basic usage, pre-made colours are available for your convenience. Use them with the pipe operator on strings you want to colour. They end the colour changes automatically, too!
import escapyde
from escapyde.examples.text import SKULL
some_text = "Hello, world!"
print(f"I want to print this red: {escapyde.FRED | some_text}, and this yellow: {escapyde.FYELLOW | 'Hi!'}.")
print(f"Here's a cyan skull:\n{escapyde.FCYAN | SKULL}")
You can also colour backgrounds,
import escapyde
some_more_text = "This should have a blue background."
print(f"{escapyde.BBLUE | some_more_text}")
combine formatting options,
import escapyde
even_more_text = "Lorem Ipsum dolor sit amet."
print(f"{escapyde.FRED | escapyde.BWHITE | even_more_text}")
and even mix formatting options on the fly (although at present you need to separate strings with parentheses):
import escapyde
print(f"{(escapyde.FRED | 'Hello') | (escapyde.FBLUE | 'World')}")
It is possible to use custom foreground and background colours via the
escapyde.AnsiEscape
class; these can be either valid ANSI literals or
RGB values:
from escapyde.ansi import AnsiEscape
from escapyde.colours import sequence_table
fg_white = sequence_table['fg_white'] # You can alternatively use raw integers, eg. 37 for white
gold = AnsiEscape(foreground_colour=(0xDB, 0xAC, 0x34))
white_gold = AnsiEscape(foreground_colour=fg_white, background_colour=(219, 172, 34))
print(gold | "This is gold!")
print(white_gold | "This is white text on gold background!")
The class defaults to the default colours of the terminal, so you don't need to set both values.
Finally, if you have a string with multiple substrings you wish to recolour, there's a function for that:
from escapyde.ansi import AnsiEscape, escape_format
mapping = {
'match': AnsiEscape(foreground_colour=(255, 0, 0)),
'case': AnsiEscape(foreground_colour=(255, 255, 0)),
'print': AnsiEscape(foreground_colour=(0, 255, 0)),
}
text = """
stuff = [3, 1, 4]
match stuff:
case [3, *rest]:
print("It's pi-like")
case _:
print("Not like pi")
"""
print(escape_format(string=text, escape_map=mapping, case_sensitive=True))
Screenshots
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 Distribution
File details
Details for the file escapyde-1.0.1.tar.gz
.
File metadata
- Download URL: escapyde-1.0.1.tar.gz
- Upload date:
- Size: 10.6 kB
- Tags: Source
- Uploaded using Trusted Publishing? No
- Uploaded via: poetry/1.5.1 CPython/3.11.0 Linux/5.15.0-1038-azure
File hashes
Algorithm | Hash digest | |
---|---|---|
SHA256 | 9f8fd786412d4fed89d56e5ded210c458cd5ed83720996f6ac91eba7c0f32723 |
|
MD5 | da32821ab7438a429db4d21452b948d8 |
|
BLAKE2b-256 | 1f7fdff5120369936bed1315ec448d7c733beb19838c19a6f5326a1a74bbc9bf |
File details
Details for the file escapyde-1.0.1-py3-none-any.whl
.
File metadata
- Download URL: escapyde-1.0.1-py3-none-any.whl
- Upload date:
- Size: 11.1 kB
- Tags: Python 3
- Uploaded using Trusted Publishing? No
- Uploaded via: poetry/1.5.1 CPython/3.11.0 Linux/5.15.0-1038-azure
File hashes
Algorithm | Hash digest | |
---|---|---|
SHA256 | bb12c4f48f88b74376bcd5523f4a91890fac4a0988f38b5dcb00cfba467edeaa |
|
MD5 | 8facd40f0b5b189a17f3eed986732f1d |
|
BLAKE2b-256 | 8e1642a75cc2823a230cb1ea1b6b03011d1cec4764f6db5c376f4ab8bc6807ac |