Skip to main content

Terminal font style and base 16 colors formatters

Project description

Test Package Black badge

Terminal Formatters

Simple Python package for producing colored and formated terminal text.

colored-text-python

Terminals traditionally take an input of bytes and display them as white text on a black background. But if the input contains specific CSI (Control Sequence Introducer) sequences then the terminal may alter certain display properties of the text, such as the style or color. Using styles and colors in console applications helps us analyze information faster and focus on important parts of the displayed information.

The package creates ANSI escape character sequences for changing the color and style of the font, based on human-readable parameters like black, green, italic etc.

ANSI Escape Codes for Terminal Graphics

The ANSI escape code standard, formally adopted as ISO/IEC 6429, defines a series of control sequences. Each control sequence begins with a Control Sequence Introducer (CSI), defined as an escape character followed immediately by a bracket: ESC[. The ANSI ASCII standard represents the escape ESC character by the decimal number 27 (33 in octal, 1B in hexadecimal). The ESC[ is followed by any number (including none) of “parameter bytes” in the range 0x30–0x3F (ASCII 0–9:;<=>?), then by any number of “intermediate bytes” in the range 0x20–0x2F (ASCII space and !"#$%&'()*+,-./), then finally by a single “final byte” in the range 0x40–0x7E (ASCII @A–Z[\]^_a–z{|}~`).

All common sequences just use the parameters as a series of semicolon-separated numbers. Missing numbers are treated as 0 and no parameters at all in ESC[m acts like a 0 reset code.

Select Graphic Rendition (SGR) parameters

The control sequence CSI n m, named Select Graphic Rendition (SGR), sets display attributes. Several attributes can be set in the same sequence, separated by semicolons. Each display attribute remains in effect until a following occurrence of SGR resets it. If no codes are given, CSI m is treated as CSI 0 m (reset / normal).

The package uses only basic parameters to control the font style and its color. The most commonly used SGR parameters to control the font style in the range (0-8) and the 8 actual colors within the ranges (30-37, 40-47, 90-97, 100-107) are defined by the ANSI standard. The package put these parameters together to create a full SGR command.

The original specification only had 8 colors, and just gave them names. The SGR parameters 30–37 selected the foreground color, while 40–47 selected the background. Quite a few terminals implemented “bold” (SGR code 1) as a brighter color rather than a different font, thus providing 8 additional foreground colors. Usually you could not get these as background colors, though sometimes inverse video (SGR code 7) would allow that. Examples: to get black letters on white background use ESC[30;47m, to get red use ESC[31m, to get bright red use ESC[1;31m. To reset colors to their defaults, use ESC[39;49m (not supported on some terminals), or reset all attributes with ESC[0m. Later terminals added the ability to directly specify the “bright” colors with 90–97 and 100–107.

The following diagram shows the complete text style and color rendering scheme.

how-to-print-colored-text-in-python

The package supports the parameters shown in the tables below:

Font Styles

Parameter

Name

Note

0

Reset or normal

All attributes off

1

Bold or increased intensity

As with faint, the color change is a PC (SCO/CGA) invention

2

Faint, decreased intensity, or dim

May be implemented as a light font weight like bold

3

Italic

Not widely supported. Sometimes treated as inverse or blink

4

Underline

Style extensions exist for Kitty, VTE, mintty and iTerm2

5

Blink or Slow blink

Sets blinking to less than 150 times per minute

7

Negative, Reverse or invert

Swap foreground and background colors; inconsistent emulation

8

Conceal or hide

Not widely supported

Foreground/Background Colors

Foreground

Background

Name

30

40

Black

31

41

Red

32

42

Green

33

43

Yellow

34

44

Blue

35

45

Magenta

36

46

Cyan

37

47

White

90

100

Black Bright or Grey

91

101

Red Bright

92

102

Green Bright

93

103

Yellow Bright

94

104

Blue Bright

95

105

Magenta Bright

96

106

Cyan Bright

97

107

Bright

Test scripts in the source code repository print formatted/colored tables using supported ANSI sequences. The following styles and colors works with most terminal applications.

colored-style--python

colored-text--python

The package has no requirements other than the standard library.

Usage

How to use the module in your own python code:

from termformatters import StyleFormatters
from termformatters import ForegroundFormatters
from termformatters import BackgroundFormatters

"""Creating Formatters Instances"""
S = StyleFormatters()
FG = ForegroundFormatters()
BG = BackgroundFormatters()

print(FG.green("Printing `Green` colored text"))
print(FG.cyan("Printing `Cyan` colored text"))
print(
    S.bold(
        FG.white_bright(
            "Printing `Bold` and `White Bright` text"
        )
    )
)
print(
    FG.yellow(
        BG.blue_bright(
            "Printing `Yellow` text on `Blue` background"
        )
    )
)

References

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

termformatters-0.2.1.tar.gz (14.3 kB view details)

Uploaded Source

Built Distribution

If you're not sure about the file name format, learn more about wheel file names.

termformatters-0.2.1-py3-none-any.whl (13.5 kB view details)

Uploaded Python 3

File details

Details for the file termformatters-0.2.1.tar.gz.

File metadata

  • Download URL: termformatters-0.2.1.tar.gz
  • Upload date:
  • Size: 14.3 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/4.0.1 CPython/3.9.15

File hashes

Hashes for termformatters-0.2.1.tar.gz
Algorithm Hash digest
SHA256 01a1f2128b823c701bda9539b6177dbb1f47f370c897e710a9e61eb40f8d329b
MD5 b6a9d8908d853b42468b85530eb9800a
BLAKE2b-256 82da949165b54fa2f8b041e152d7a770fe08e1ce2eb03e4f2f75528a9ac6827c

See more details on using hashes here.

File details

Details for the file termformatters-0.2.1-py3-none-any.whl.

File metadata

  • Download URL: termformatters-0.2.1-py3-none-any.whl
  • Upload date:
  • Size: 13.5 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/4.0.1 CPython/3.9.15

File hashes

Hashes for termformatters-0.2.1-py3-none-any.whl
Algorithm Hash digest
SHA256 f4d5e45e9ee98cf3625bb0f755644cc86afb54ce10edb67da60197b4a44cea1e
MD5 97709d40da219c241a7f57114de9fc2e
BLAKE2b-256 76fdffa84bfe37d7da5b35c756dededae9ac38a27b72d3c715591b8d31e54711

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