Miscellaneous Helpers

Clap has a few extra tools that may be useful when creating your applications.

ANSI Formatting

The clap.ansi.ANSI class can be used to format your output with ANSI color and text effects.

class clap.ansi.ANSI(stream=None)

This is capable of handling the majority of widely-supported SGR codes. It is instantiated with a stream, but if the stream is None, it will merely return the codes generated instead of write them.

Parameter:stream – The stream to write to. If it is None, the codes will be returned.
bgcolor(color)

Sets the background color to the given color.

Parameter:color – The color to set. (Can be an int from 1-7 or a str.)
black()
Sets the foreground color to black.
blackbg()
Sets the background color to black.
blue()
Sets the foreground color to blue.
bluebg()
Sets the background color to blue.
bright()
Sets “bright” text, which is bold, brighter than normal, or both.
color(color)

Sets the foreground color to the given color.

Parameter:color – The color to set. (Can be an int from 1-7 or a str.)
cyan()
Sets the foreground color to cyan.
cyanbg()
Sets the background color to cyan.
dim()
Sets “dim” text, which is darker than normal.
green()
Sets the foreground color to green.
greenbg()
Sets the background color to green.
inverse()
Sets the text to be in reverse video (background is foreground and vice versa).
magenta()
Sets the foreground color to magenta.
magentabg()
Sets the background color to magenta.
red()
Sets the foreground color to red.
redbg()
Sets the background color to red.
reset()
Resets all colors and font effects.
underline()
Sets the text to be underlined.
white()
Sets the foreground color to white.
whitebg()
Sets the background color to white.
write(data)

This writes data to the stream. If the stream is None, it just returns the data. If the data is an int, it will wrap it in an escape code before printing.

Parameter:data – The data to write, or an int SGR code.
yellow()
Sets the foreground color to yellow.
yellowbg()
Sets the background color to yellow.

Two instances of this class are provided.

clap.ansi.ansi
An ANSI instance set to sys.stdout.
clap.ansi.ansi_codes
An ANSI instance set to None, so you can access the codes.

Table Of Contents

This Page