Styling A String#
Styling strings is fundamental when using adorable and there are multiple ways of doing so.
paint()
and printc()
#
One method is using the paint
function (and
the equivalent version printc
in order to
directly print it).
import sys
from adorable import color, paint, printc
RED = color.from_name("red").fg
YELLOW = color.from_name("yellow").bg
The snippets below have the same result.
- adorable.ansi.paint(*args, style=None, sep=' ')[source]
Styles a string.
- Parameters:
args (Any) – Objects to style. The
str()
function will be called on each object.style (Optional[Ansi]) – Ansi object that styles the string.
sep (str) – String that separates
args
.
- Return type:
The styled string.
print(paint("Hello, World!", style = RED + YELLOW), file = sys.stderr)
- adorable.ansi.printc(*args, **kwargs)[source]
Prints a styled string.
This function takes the same arguments as the built-in print function. It also provides an extra parameter.
printc("Hello, World!", style = RED + YELLOW, file = sys.stderr)
Calling the ansi object#
Another method is to call the ansi object like it would be a function.
from adorable import color
RED = color.from_name("red").fg
print(RED("Hello, World!"))
Using markup#
It is often necessary to only style specific parts of a string. Instead of concatenating these parts, markup can be used.
from adorable import color
RED = color.from_name("red").fg
print(f"Hello {RED:colorful} World")