Skip to main content

ANSI formatted terminal output toolset

Project description


Code style: black
PyPI Downloads Coverage Status

[Documentation] | [Changelog]

(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

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

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

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

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

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


Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

pytermor-2.106.6.dev0.tar.gz (199.9 kB view hashes)

Uploaded Source

Built Distribution

pytermor-2.106.6.dev0-py3-none-any.whl (159.4 kB view hashes)

Uploaded Python 3

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