Package: termformatters
Terminal FormattersSimple Python package for producing colored and formated terminal text.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 likeblack,green,italicetc.ANSI Escape Codes for Terminal GraphicsTheANSI escape code
standard, formally
adopted asISO/IEC
6429,
defines a series of control sequences. Each control sequence begins with
aControl 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). TheESC[is followed by any number
(including none) of “parameter bytes” in the range0x30–0x3F(ASCII0–9:;<=>?), then by any number of “intermediate bytes” in the range0x20–0x2F(ASCIIspaceand!"#$%&'()*+,-./), then finally by
a single “final byte” in the range0x40–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 inESC[macts like a 0 reset code.Select Graphic Rendition (SGR) parametersThe control sequenceCSI n m, namedSelect 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 mis treated asCSI 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 parameters30–37selected the foreground color, while40–47selected the background. Quite a few terminals implemented
“bold” (SGR code1) 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 useESC[30;47m, to get red useESC[31m, to get bright
red useESC[1;31m. To reset colors to their defaults, useESC[39;49m(not supported on some terminals), or reset all
attributes withESC[0m. Later terminals added the ability to
directly specify the “bright” colors with90–97and100–107.The following diagram shows the complete text style and color rendering
scheme.The package supports the parameters shown in the tables below:Font StylesParameterNameNote0Reset or normalAll attributes off1Bold or increased
intensityAs with faint, the color
change is a PC (SCO/CGA)
invention2Faint, decreased
intensity, or dimMay be implemented as a
light font weight like
bold3ItalicNot widely supported.
Sometimes treated as
inverse or blink4UnderlineStyle extensions exist
for Kitty, VTE, mintty
and iTerm25Blink or Slow blinkSets blinking to less
than 150 times per minute7Negative, Reverse or
invertSwap foreground and
background colors;
inconsistent emulation8Conceal or hideNot widely supportedForeground/Background ColorsForegroundBackgroundName3040Black3141Red3242Green3343Yellow3444Blue3545Magenta3646Cyan3747White90100Black Bright or Grey91101Red Bright92102Green Bright93103Yellow Bright94104Blue Bright95105Magenta Bright96106Cyan Bright97107BrightTest scripts in the source code repository print formatted/colored
tables using supported ANSI sequences. The following styles and colors
works with most terminal applications.The package has no requirements other than the standard library.UsageHow to use the module in your own python code:fromtermformattersimportStyleFormattersfromtermformattersimportForegroundFormattersfromtermformattersimportBackgroundFormatters"""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")))ReferencesANSI escape code
standardISO/IEC
6429