10. pypsi.ansi - ANSI Escape Code Support

Stream classes for writing to files.

class pypsi.ansi.AnsiCode(code, s=None, end_code=None)[source]

Bases: object

A single ansi escape code.

Parameters:
  • code (str) – the ansi escape code, usually begins with ‘x1b[‘
  • s (str) – the body of the code, only useful if wrapping a string in this code
__call__(s, postfix='\x1b[0m')[source]

Wrap a given string in the escape code. This is useful for printing a word or sentence in a specific color.

Parameters:
  • s (str) – the input string
  • postfix (str) – the postfix string to (default is the ansi reset code)
prompt()[source]

Wrap non-print-able characters in readline non visible markers. This is required if the string is going to be passed into input().

class pypsi.ansi.AnsiCodesSingleton[source]

Bases: object

Holds all supported ansi escape codes.

black = None

Black

blue = None

Blue (bold)

clear_screen = None

Clear the screen

codes = None

all codes as a dict, useful for formatting an ansi string

cyan = None

Cyan (bold)

gray = None

Gray

green = None

Green (bold)

purple = None

Purple (bold)

red = None

Red (bold)

reset = None

Reset terminal color and style

underline = None

Underline text

white = None

White (bold)

yellow = None

Yellow (bold)

pypsi.ansi.ansi_center(s, width)[source]

Center the provided string for a given width.

Parameters:
  • s (str) – the input string
  • width (int) – the desired field width
pypsi.ansi.ansi_len(value)[source]

Get the length of the provided str, not counting any ansi codes.

Parameters:value (str) – the input string
pypsi.ansi.ansi_ljust(s, width)[source]

Left justify an input string, ensuring that it contains width charaters.

Parameters:
  • s (str) – the input string
  • width (int) – the desired output width
Returns str:

the output string

pypsi.ansi.ansi_rjust(s, width)[source]

Right justify the input string.

Parameters:
  • s (str) – the input string
  • width (int) – the desired width
Returns str:

the output string

pypsi.ansi.AnsiCodes = <pypsi.ansi.AnsiCodesSingleton object>

Global instance for all supported ansi codes (instance of AnsiCodesSingleton)