Effects in ANSI Escape Codes#
Structure#
For effects and text styling, the escape codes are similar to the standard colors.
For example, the sequence \x1b[4m
for underlined styling can be broken down into the following parts:
Part | \x1b[ | 4 | m |
---|---|---|---|
Description | Starts sequence, also called the Control Sequence Introducer (CSI). | Effect on or off, respectively between 1-8 or 21-28. | Ends sequence and calls the graphics function Select Graphic Rendition (SGR). |
Cheat Sheet#
Effect | On | Escape Code | Off | Escape Code | Output Example |
---|---|---|---|---|---|
Bold | 1 | \x1b[1m | 21 | \x1b[21m | This is BOLD |
Dim | 2 | \x1b[2m | 22 | \x1b[22m | This is DIMMED |
Underline | 4 | \x1b[4m | 24 | \x1b[24m | This is UNDERLINED |
Blink | 5 | \x1b[5m | 25 | \x1b[25m | This is BLINKING |
Reverse | 7 | \x1b[7m | 27 | \x1b[27m | This is REVERSED |
Hide | 8 | \x1b[7m | 28 | \x1b[28m | This is HIDDEN |
Different Color Schemes in Different Terminals
Most terminals apply different color schemes so \x1b[31m
or Color.RED
won't produce the exact same screen color of red. Some straight, others with an orange tint. For further reading, refer to this list of common terminals and their color schemes.
Examples#
For example, \x1b[4m
is underlined text, and \x1b[24m
turns off the underline effect. When wrapped in a print statement, you can write this:
How it appears in the terminal:
% This is UNDERLINED text
Another example with blinking text:
How it appears in the terminal:
% This is BLINKING text
How to Use Effects with Colorist
Instead of using raw ANSI escape codes, it's convenient to use Colorist to generate them while keeping the code readable.
Simply use the Effect
class for effects and styling. For example:
Python | |
---|---|
How it appears in the terminal:
% This is UNDERLINED text
% This is BLINKING text