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'))
*
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...')
*
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)
*
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)
*
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))
*
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.117.0.dev0-py3-none-any.whl
Algorithm | Hash digest | |
---|---|---|
SHA256 | 42dfcde711366984a3a0a685fd287d612d5f3b581775a4ed59722ab998a314fd |
|
MD5 | 4591fd3c1cfc273fd02a1e6aaa60870e |
|
BLAKE2b-256 | 508f84b100b7c090fbf7e097c17440512f316a244b70fe3862430e698d250356 |