Some useful tool functions

This module implements various tool functions.

Table of contents

color()

pycalculator.tools.color(string)[source]

Add colors to string output in terminal.

Parse the given string to replace every <symbol> block by its corresponding ANSI escape code, following this conversion table:

symbol ANSI escape code meaning
</> '\x1b[0m' reset
<+> '\x1b[1m' bold
<r> '\x1b[31m' red
<g> '\x1b[32m' green
<b> '\x1b[34m' blue

Warning

This method will not work on Windows terminal.

Note

This method automatically adds '\x1b[0m' (a reset escape sequence) to the end of the returned string to preserve
the terminal output state.
Parameters:string (str) – String to parse and print.
Returns:ANSI encoded string.
Return type:str

Examples

>>> color('<r>ERROR: critical error.')
'\x1b[31mERROR: critical error.\x1b[0m'

>>> print(color('<g><+>All checks passed!')) 
All checks passed! # in green and bold in the terminal