ANSI formatted terminal output toolset
Project description
(yet another) Python library initially designed for formatting terminal output using ANSI escape codes.
Provides high-level methods for working with text sections, colors, formats, alignment and wrapping, as well as low-level ansi
module which allows operating with SGR sequences and also implements automatic "soft" format termination. Depending on the context and technical requirements either approach can be used. Also includes a set of additional number/string/date formatters for pretty output.
Motivation
Key feature of this library is extendability and a variety of formatters (called renderers), which determine the output syntax:
SgrRenderer
(global default)TmuxRenderer
HtmlRenderer
SgrDebugger
(mostly for development)- etc.
No dependencies required, only Python Standard Library (there are some for testing and docs building, though).
Installation
pip install pytermor
Features
Span is a combination of two control sequences; it wraps specified string with pre-defined leading and trailing SGR definitions.
from pytermor import span
print(span.red('Feat') + span.bold('ures'))
* ![image](https://pypi-camo.freetls.fastly.net/fb4f9e8fdab95e2afe603b8cee2bd561acbb0d91/68747470733a2f2f757365722d696d616765732e67697468756275736572636f6e74656e742e636f6d2f35303338313934362f3136313338373639322d34333734656463622d633166652d343338662d393666312d6461653363356164343038382e706e67)
Preset spans can safely overlap with each other (as long as they require different breaker sequences to reset).
from pytermor import span
print('... ' +
span.blue(span.underlined('nested') +
span.bold(' styles')) + ' in...')
* ![image](https://pypi-camo.freetls.fastly.net/bf70a814959681d5ea6992e22cd1358d57e81a96/68747470733a2f2f757365722d696d616765732e67697468756275736572636f6e74656e742e636f6d2f35303338313934362f3136313338373731312d32333734363532302d343139622d343931372d393430312d3235373835346666326438612e706e67)
Compose text spans with automatic content-aware format termination.
from pytermor import autocomplete
span1 = autocomplete('blue', 'bold')
span2 = autocomplete('cyan', 'inversed', 'underlined', 'italic')
msg = span1(f'Content{span2("-aware format")} nesting')
print(msg)
* ![image](https://pypi-camo.freetls.fastly.net/2325af2856d5c466b8230575f7c6a8df34defc4c/68747470733a2f2f757365722d696d616765732e67697468756275736572636f6e74656e742e636f6d2f35303338313934362f3136313338373733342d36373764356231302d313563312d343932362d393333662d6231313434623063653563622e706e67)
Create your own SGR sequences with build()
method, which accepts color/attribute keys, integer codes and even existing SGRs, in any amount and in any order. Key resolving is case-insensitive.
from pytermor import sequence, build
seq1 = build('red', 1) # keys or integer codes
seq2 = build(seq1, sequence.ITALIC) # existing SGRs
seq3 = build('underlined', 'YELLOW') # case-insensitive
msg = f'{seq1}Flexible{sequence.RESET} ' + \
f'{seq2}sequence{sequence.RESET} ' + \
str(seq3) + 'builder' + str(sequence.RESET)
print(msg)
* ![image](https://pypi-camo.freetls.fastly.net/ee4f1e6308d0c97d6157c7f54a268204968ded4e/68747470733a2f2f757365722d696d616765732e67697468756275736572636f6e74656e742e636f6d2f35303338313934362f3136313338373734362d30613934653364322d383239352d343738632d383238632d3333336539396535643530612e706e67)
Use color_indexed()
to set foreground/background color to any of ↗ xterm-256 colors.
from pytermor import color_indexed, sequence, autocomplete
txt = '256 colors support'
start_color = 41
msg = ''
for idx, c in enumerate(range(start_color, start_color+(36*6), 36)):
msg += f'{color_indexed(c)}{txt[idx*3:(idx+1)*3]}{sequence.COLOR_OFF}'
print(autocomplete(sequence.BOLD).wrap(msg))
* ![image](https://pypi-camo.freetls.fastly.net/3a7a58eaad99a2713deab2693f17d45ea58d5cac/68747470733a2f2f757365722d696d616765732e67697468756275736572636f6e74656e742e636f6d2f35303338313934362f3136313431313537372d37343362396138312d656163332d343763302d396235392d3832623238396363306634352e706e67)
It's also possible to use 16M-color mode (or True color) — with color_rgb()
wrapper method.
from pytermor import color_rgb, sequence, span
txt = 'True color support'
msg = ''
for idx, c in enumerate(range(0, 256, 256//18)):
r = max(0, 255-c)
g = max(0, min(255, 127-(c*2)))
b = c
msg += f'{color_rgb(r, g, b)}{txt[idx:(idx+1)]}{sequence.COLOR_OFF}'
print(span.bold(msg))
Proceed to documentation.
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
Hashes for pytermor-2.109.0.dev0-py3-none-any.whl
Algorithm | Hash digest | |
---|---|---|
SHA256 | ec73d216690dd2d7ce6b16b3cbfc2ea8f2b90da29b34e18344e22b6512ebc3aa |
|
MD5 | b6e4dbccde643dd19722d65898f65b71 |
|
BLAKE2b-256 | 9f23321da1301af5019624fa5f886961c5f0e8625e3cf868fc6610847ca86498 |