Skip to main content

ANSI escape code library for Python

Project description

Pansi is a clean and simple ANSI escape code library for Python.

>>> from pansi import ansi
>>> print("hello, {green}{name}{_}".format(name="world", **ansi))
hello, world

General usage

Pansi provides an object called ansi through which all escape codes can be selected. This object exposes the codes as both attributes (e.g. ansi.red) and items (e.g. ansi["red"]).

This object can therefore be used in several different ways, but the simplest is through the string format method. Here, if the object is supplied as a simple named argument, all references need to be prefixed:

>>> print("hello, {ansi.green}{name}{ansi.reset}".format(name="world", ansi=ansi))
hello, world

Alternatively, passing the object with the ** operator removes the need for prefixes and thus makes the template string shorter. However, this does introduce a greater chance of clashing with other parameters, and doesn’t signal which parameters are which.

>>> print("hello, {green}{name}{_}".format(name="world", **ansi))
hello, world

Ultimately, it is a subjective choice between these two options.

Colours

For foreground text, the standard set of colours can be selected using the lower case name for normal brightness and the upper case name for high intensity. To select as the background colour instead, simply prefix with bg., e.g. bg.blue.

~

Foreground

Background

Colour

Normal

Bright

Normal

Bright

Black

black

BLACK

bg.black

bg.BLACK

Red

red

RED

bg.red

bg.RED

Green

green

GREEN

bg.green

bg.GREEN

Yellow

yellow

YELLOW

bg.yellow

bg.YELLOW

Blue

blue

BLUE

bg.yellow

bg.YELLOW

Magenta

magenta

MAGENTA

bg.magenta

bg.MAGENTA

Cyan

cyan

CYAN

bg.cyan

bg.CYAN

White

white

WHITE

bg.white

bg.WHITE

Full 24-bit colour support is also available (on those terminals that support it) by using the rgb selector.

rgb

::
>>> print("A Clockwork {rgb[FF8000]}Orange{_}".format(**ansi))
A Clockwork Orange

Reverse video

rev _rev

Colour reset

fg.reset bg.reset

Text Weight

weight.normal weight.bold weight.light b _b

Text Style

style.normal style.italic style.fraktur i _i

Text decoration

u uu _u o _o s _s

Blinking

blink BLINK _blink

Hide & show

hide show

Font

font0

Download files

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

Source Distribution

pansi-0.0.dev0.tar.gz (8.9 kB view details)

Uploaded Source

File details

Details for the file pansi-0.0.dev0.tar.gz.

File metadata

  • Download URL: pansi-0.0.dev0.tar.gz
  • Upload date:
  • Size: 8.9 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: Python-urllib/3.8

File hashes

Hashes for pansi-0.0.dev0.tar.gz
Algorithm Hash digest
SHA256 c50a4936dc8442b2bc95f0e6eebefa12755164c2ef23d714a57121fe38b036e0
MD5 eb83896fcb1590dd22c78176a8621596
BLAKE2b-256 11e2625239187ac5a0fbcec63b3f2e08a57abcd54f273aab6e7b70e8a8737da9

See more details on using hashes here.

Supported by

AWS Cloud computing and Security Sponsor Datadog Monitoring Depot Continuous Integration Fastly CDN Google Download Analytics Pingdom Monitoring Sentry Error logging StatusPage Status page