A lightweight parser for ANSI escape sequences
Project description
stransi 🖍️
I see a
\033[1;31m
red\033[;39m
door, and I want it painted\033[1;30m
black\033[;39m
.
stransi is a lightweight parser for ANSI escape code sequences. It implements a string-like type that is aware of its own ANSI escape sequences, and can be used to parse most of the common escape sequences used in terminal output manipulation.
Features
- ✨ Good support of ANSI escape sequences
- 🎨 Focus on coloring and styling
- 🛡️ Unsupported
CSI
escape sequences are emitted as tokens - 🏜️ Only one dependency: ochre
- 🐍 Python 3.8+
Installation
$ pip install stransi
Usage
In [1]: from stransi import Ansi
In [2]: text = Ansi(
...: "I see a \033[1;31mred\033[;39m door, "
...: "and I want it painted \033[1;30mblack\033[;39m"
...: )
In [3]: list(text.escapes())
Out[3]:
['I see a ',
Escape('\x1b[1;31m'),
'red',
Escape('\x1b[;39m'),
' door, and I want it painted ',
Escape('\x1b[1;30m'),
'black',
Escape('\x1b[;39m')]
In [4]: list(text.instructions())
Out[4]:
['I see a ',
SetAttribute(attribute=<Attribute.BOLD: 1>),
SetColor(role=<ColorRole.FOREGROUND: 30>, color=Ansi256(1)),
'red',
SetAttribute(attribute=<Attribute.NORMAL: 0>),
SetColor(role=<ColorRole.FOREGROUND: 30>, color=None),
' door, and I want it painted ',
SetAttribute(attribute=<Attribute.BOLD: 1>),
SetColor(role=<ColorRole.FOREGROUND: 30>, color=Ansi256(0)),
'black',
SetAttribute(attribute=<Attribute.NORMAL: 0>),
SetColor(role=<ColorRole.FOREGROUND: 30>, color=None)]
Credits
Photo by Tien Vu Ngoc on Unsplash.
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
stransi-0.3.0.tar.gz
(8.4 kB
view details)
Built Distribution
File details
Details for the file stransi-0.3.0.tar.gz
.
File metadata
- Download URL: stransi-0.3.0.tar.gz
- Upload date:
- Size: 8.4 kB
- Tags: Source
- Uploaded using Trusted Publishing? No
- Uploaded via: poetry/1.1.11 CPython/3.9.7 Linux/5.13.0-22-generic
File hashes
Algorithm | Hash digest | |
---|---|---|
SHA256 | 2ba0195f935a8a152ad1921b25e14e7e78c549948d2ef0f63a6a5d9ab4625830 |
|
MD5 | 9dc6084ec56ca7741473c7b809e31129 |
|
BLAKE2b-256 | fe96d72e9fd64f11502e37ec397a912bba4a81cbe195a07e0cd4d5b5a6268de7 |
File details
Details for the file stransi-0.3.0-py3-none-any.whl
.
File metadata
- Download URL: stransi-0.3.0-py3-none-any.whl
- Upload date:
- Size: 9.6 kB
- Tags: Python 3
- Uploaded using Trusted Publishing? No
- Uploaded via: poetry/1.1.11 CPython/3.9.7 Linux/5.13.0-22-generic
File hashes
Algorithm | Hash digest | |
---|---|---|
SHA256 | 7082fe969abca1059437a45b81c3b05a2eb1362503487bd35bd1d8919af3f0a4 |
|
MD5 | f0a9165de031db0a871157588d96fc26 |
|
BLAKE2b-256 | fa1071eff1dc5f6e97b1d245c9efb96cc7e581340fe6218bb4744e0e2f130859 |