-
-
Notifications
You must be signed in to change notification settings - Fork 0
format_codes
This class includes methods to easily transform formatting codes to ANSI and use them for pretty console output.
First, let's take a look at a small example of what a highly styled print string with formatting could look like using this module:
This here is just unformatted text. [b|u|br:blue](Next we have text that is bright blue + bold + underlined.)\n
[#000|bg:#F67](Then there's also black text with a red background.) And finally the ([i](boring)) plain text again.
How all of this exactly works is explained in the sections below. 🠫
In this module, you can apply styles and colors using simple formatting codes. These formatting codes consist of one or multiple different formatting keys in between square brackets.
If a formatting code is placed in a print-string, the formatting of that code will be applied to everything behind it until its formatting is reset.
If applying multiple styles and colors in the same place, instead of writing the formatting keys all into separate brackets (e.g. [x][y][z]
),
they can also be put in a single pair of brackets, separated by pipes (e.g. [x|y|z]
).
A list of all possible formatting keys can be found under all possible formatting keys.
Certain formatting can automatically be reset, behind a certain amount of text, just like shown in the following example:
This is plain text, [br:blue](which is bright blue now.) Now it was automatically reset to plain again.
This will only reset formatting codes, that have a specific reset (listed below, under all possible formatting keys). That means if you use it where another formatting is already applied, that formatting is still there after the automatic reset:
[cyan]This is cyan text, [dim](which is dimmed now.) Now it's not dimmed any more but still cyan.
If you want to ignore the auto-reset functionality of ()
brackets, you can put a /
or \\
between them and the formatting code:
[cyan]This is cyan text, [u]\\(which is underlined now.) And now it is still underlined and cyan.
You can also escape formatting codes themselves by putting a /
or \\
at the beginning, inside the square brackets:
This is a formatting code: [br:green]([\\b|#000|bg:br:green])\n
And here's what it looks like: [b|#000|bg:br:green]Sample text.
-
RGB Colors:
Change the text color directly with an RGB color inside the square brackets. (With or withoutrgb()
brackets doesn't matter.)
Examples:[rgb(115, 117, 255)]
[(255, 0, 136)]
[255, 0, 136]
-
HEX Colors:
Change the text color directly with a HEX color inside the square brackets. (Whether theRGB
orRRGGBB
HEX format is used, and if there's a#
or0x
prefix, doesn't matter.)
Examples:[0x7788FF]
[#7788FF]
[7788FF]
[0x78F]
[#78F]
[78F]
-
Background RGB / HEX Colors:
Change the background color directly with an RGB or HEX color inside the square brackets, using thebackground:
BG:
prefix. (Same RGB / HEX formatting code rules as for text color.)
Examples:[background:rgb(115, 117, 255)]
[BG:(255, 0, 136)]
[background:#7788FF]
[BG:#78F]
-
Standard Console Colors:
Change the text color to one of the standard console colors by just writing the color name in the square brackets.[black]
[red]
[green]
[yellow]
[blue]
[magenta]
[cyan]
[white]
-
Bright Console Colors:
Use the prefixbright:
BR:
to use the bright variant of the standard console color.
Examples:-
[bright:black]
[BR:black]
-
[bright:red]
[BR:red]
- ...
-
-
Background Console Colors:
Use the prefixbackground:
BG:
to set the background to a standard console color. (Not all consoles support bright standard colors.)
Examples:-
[background:black]
[BG:black]
-
[background:red]
[BG:red]
- ...
-
-
Bright Background Console Colors:
Combine the prefixesbackground:
/BG:
andbright:
/BR:
to set the background to a bright console color. (The order of the prefixes doesn't matter.)
Examples:-
[background:bright:black]
[BG:BR:black]
-
[background:bright:red]
[BG:BR:red]
- ...
-
-
Text Styles:
Use the built-in text formatting to change the style of the text. There are long and short forms for each formatting code. (Not all consoles support all text styles.)-
[bold]
[b]
[dim]
-
[italic]
[i]
-
[underline]
[u]
-
[inverse]
[invert]
[in]
-
[hidden]
[hide]
[h]
-
[strikethrough]
[s]
-
[double-underline]
[du]
-
-
Specific Reset:
Use these reset codes to remove a specific style, color or background. Again, there are long and short forms for each reset code.-
[_bold]
[_b]
[_dim]
-
[_italic]
[_i]
-
[_underline]
[_u]
-
[_inverse]
[_invert]
[_in]
-
[_hidden]
[_hide]
[_h]
-
[_strikethrough]
[_s]
-
[_double-underline]
[_du]
-
[_color]
[_c]
-
[_background]
[_bg]
-
-
Total Reset:
This will reset all previously applied formatting codes.[_]
-
[*]
resets everything, just like[_]
, but the text color will remain indefault_color
(if nodefault_color
is set, it resets everything, exactly like[_]
) -
[*color]
will reset the text color, just like[_color]
, but then also make itdefault_color
(if nodefault_color
is set, both are treated as invalid formatting codes) -
[default]
will just color the text indefault_color
(if nodefault_color
is set, it's treated as an invalid formatting code) -
[background:default]
[BG:default]
will color the background indefault_color
(if nodefault_color
is set, both are treated as invalid formatting codes)
Unlike the standard console colors (under all possible formatting keys), the default color can be changed by using the following modifiers:
-
[l]
will lighten thedefault_color
text bybrightness_steps
% -
[ll]
will lighten thedefault_color
text by2 × brightness_steps
% -
[lll]
will lighten thedefault_color
text by3 × brightness_steps
% - ... etc. Same thing for darkening:
-
[d]
will darken thedefault_color
text bybrightness_steps
% -
[dd]
will darken thedefault_color
text by2 × brightness_steps
% -
[ddd]
will darken thedefault_color
text by3 × brightness_steps
% - ... etc.
Per default, you can also use
+
and-
to get lighter and darkerdefault_color
versions. All of these lighten/darken formatting codes are treated as invalid if nodefault_color
is set.
This is a print function, whose print values can be formatted using formatting codes.
Params:
-
*values: object
the values to format and print -
default_color: Optional[Rgba | Hexa] = None
the default text color to use if no other text color was applied -
brightness_steps: int = 20
the amount (%) to lighten/darken the default color after a[l]
or[d]
formatting code -
sep: str = " "
the separator to join the*values
with -
end: str = "\n"
the string to print after the*values
-
flush: bool = True
whether to flush the output
Returns: no return value
This is an input function, whose input prompt can be formatted using formatting codes.
Params:
-
prompt: object = ""
the prompt to display -
default_color: Optional[Rgba | Hexa] = None
the default text color to use if no other text color was applied -
brightness_steps: int = 20
the amount (%) to lighten/darken the default color after a[l]
or[d]
formatting code -
reset_ansi: bool = False
whether the ANSI formatting should be reset after the user confirms the input and the program continues to run
Returns: the user's entry as a string
This method converts the formatting codes in a string to ANSI codes.
Params:
-
string: str
the string that contains the formatting codes to convert -
default_color: Optional[Rgba | Hexa] = None
the default text color to use if no other text color was applied -
brightness_steps: int = 20
the amount (%) to lighten/darken the default color after a[l]
or[d]
formatting code
Returns: the string with the valid formatting codes converted to ANSI codes
This method escapes all ANSI codes in a string, so they are visible when output to the console.
Param: ansi_string: str
the string that contains the ANSI codes to escape
Returns: the string with the ANSI codes escaped
This method removes all ANSI codes from a string.
Params:
-
ansi_string: str
the string that contains the ANSI codes to remove -
get_removals: bool = False
whether to additionally return a tuple with the removed codes and their removal position -
_ignore_linebreaks: bool = False
whether to ignore line breaks for the removal positions
Returns: the cleaned string or a tuple with the cleaned string and the removals
If get_removals
is true, the ANSI removals will be returned as a tuple.
Inside the tuple is another tuple for each removed ANSI code. This tuple contains the removal position and the removed ANSI code.
Example:
If we use the method on the string "\x1b[1mHello \x1b[0mWorld!"
, the returned tuple from the method would look like this:
(
# CLEANED STRING
"Hello World!",
# REMOVED CODES
(
(0, "\x1b[1m"),
(6, "\x1b[0m"),
),
)
This method removes all formatting codes from a string.
Params:
-
string: str
the string that contains the formatting codes to remove -
get_removals: bool = False
whether to additionally return a tuple with the removed codes and their removal position -
_ignore_linebreaks: bool = False
whether to ignore line breaks for the removal positions
Returns: the cleaned string or a tuple with the cleaned string and the removals
If get_removals
is true, the formatting code removals (already translated to ANSI codes) will be returned as a tuple.
Inside the tuple is another tuple for each removed formatting code. This tuple contains the removal position and the removed ANSI code.
Example:
If we use the method on the string "[br:red](Hello) [b]World!"
, the returned tuple from the method would look like this:
(
# CLEANED STRING
"Hello World!",
# REMOVED CODES
(
(0, "\x1b[91m"),
(5, "\x1b[39m"), # AUTO RESET CODE
(6, "\x1b[1m"),
),
)
★⠀Python Library by XulbuX⠀★
-
Intended Audience:
- Developers
-
License:
- OSI Approved
- MIT License
-
Operating Systems:
- Full Library: OS Independent
-
Supported Python Versions:
- Python 3.13
- Python 3.12
- Python 3.11
- Python 3.10
-
Topics:
- Libraries
- Python Modules
- Software Development