ansicolor.ansicolor module

class ansicolor.ansicolor.Colors[source]

Bases: object

Container class for colors

class Black

Bases: object

id = 0
class Blue

Bases: object

id = 4
class Cyan

Bases: object

id = 6
class Green

Bases: object

id = 2
class Magenta

Bases: object

id = 5
class Red

Bases: object

id = 1
class White

Bases: object

id = 7
class Yellow

Bases: object

id = 3
classmethod iter()[source]
classmethod new(colorname)[source]
ansicolor.ansicolor.black(s, bold=False, reverse=False)

Colorize string in black

Parameters
  • s (string) – The string to colorize.

  • bold (bool) – Whether to mark up in bold.

  • reverse (bool) – Whether to mark up in reverse video.

Return type

string

ansicolor.ansicolor.blue(s, bold=False, reverse=False)

Colorize string in blue

Parameters
  • s (string) – The string to colorize.

  • bold (bool) – Whether to mark up in bold.

  • reverse (bool) – Whether to mark up in reverse video.

Return type

string

ansicolor.ansicolor.colordiff(x, y, color_x=<class 'ansicolor.ansicolor.Cyan'>, color_y=<class 'ansicolor.ansicolor.Green'>, debug=False)[source]

Formats a diff of two strings using the longest common subsequence by highlighting characters that differ between the strings.

Returns the strings x and y with highlighting.

Parameters
  • x (string) – The first string.

  • y (string) – The second string.

  • color_x (Colors class) – The color to use for the first string.

  • color_y (Colors class) – The color to use for the second string.

  • debug (bool) – Whether to print debug output underway.

Return type

tuple

ansicolor.ansicolor.colorize(s, color, bold=False, reverse=False, start=None, end=None)[source]

Colorize a string with the color given.

Parameters
  • s (string) – The string to colorize.

  • color (Colors class) – The color to use.

  • bold (bool) – Whether to mark up in bold.

  • reverse (bool) – Whether to mark up in reverse video.

  • start (int) – Index at which to start coloring.

  • end (int) – Index at which to end coloring.

Return type

string

ansicolor.ansicolor.cyan(s, bold=False, reverse=False)

Colorize string in cyan

Parameters
  • s (string) – The string to colorize.

  • bold (bool) – Whether to mark up in bold.

  • reverse (bool) – Whether to mark up in reverse video.

Return type

string

ansicolor.ansicolor.get_code(color, bold=False, reverse=False)[source]

Returns the escape code for styling with the given color, in bold and/or reverse.

Parameters
  • color (Colors class) – The color to use.

  • bold (bool) – Whether to mark up in bold.

  • reverse (bool) – Whether to mark up in reverse video.

Return type

string

ansicolor.ansicolor.get_highlighter(colorid)[source]

Map a color index to a highlighting color.

Parameters

colorid (int) – The index.

Return type

Colors

ansicolor.ansicolor.green(s, bold=False, reverse=False)

Colorize string in green

Parameters
  • s (string) – The string to colorize.

  • bold (bool) – Whether to mark up in bold.

  • reverse (bool) – Whether to mark up in reverse video.

Return type

string

ansicolor.ansicolor.highlight_string(s, *spanlists, **kw)[source]

Highlight spans in a string using a list of (begin, end) pairs. Each list is treated as a layer of highlighting. Up to four layers of highlighting are supported.

Parameters
  • s (string) – The string to highlight

  • spanlists (list) – A list of tuples on the form [(begin, end)*]*

  • kw – May include: bold, reverse, color, colors and nocolor

Return type

string

Deprecated since version 0.2.3: The color parameter has been deprecated in favor of colors.

ansicolor.ansicolor.justify_formatted(s, justify_func, width)[source]

Justify a formatted string to a width using a function (eg. string.ljust).

Parameters
  • s (string) – The formatted string.

  • justify_func – The justify function.

  • width (int) – The width at which to justify.

Return type

string

ansicolor.ansicolor.magenta(s, bold=False, reverse=False)

Colorize string in magenta

Parameters
  • s (string) – The string to colorize.

  • bold (bool) – Whether to mark up in bold.

  • reverse (bool) – Whether to mark up in reverse video.

Return type

string

ansicolor.ansicolor.red(s, bold=False, reverse=False)

Colorize string in red

Parameters
  • s (string) – The string to colorize.

  • bold (bool) – Whether to mark up in bold.

  • reverse (bool) – Whether to mark up in reverse video.

Return type

string

ansicolor.ansicolor.set_term_title(s)[source]

Set the title of a terminal window.

Parameters

s (string) – The title.

ansicolor.ansicolor.strip_escapes(s)[source]

Strips escapes from the string.

Parameters

s (string) – The string.

Return type

string

ansicolor.ansicolor.white(s, bold=False, reverse=False)

Colorize string in white

Parameters
  • s (string) – The string to colorize.

  • bold (bool) – Whether to mark up in bold.

  • reverse (bool) – Whether to mark up in reverse video.

Return type

string

ansicolor.ansicolor.wrap_string(s, pos, color, bold=False, reverse=False)[source]

Colorize the string up to a position.

Parameters
  • s (string) – The string to colorize.

  • pos (int) – The position at which to stop.

  • color (Colors class) – The color to use.

  • bold (bool) – Whether to mark up in bold.

  • reverse (bool) – Whether to mark up in reverse video.

Return type

string

Deprecated since version 0.2.2: This function has been deprecated in favor of colorize().

ansicolor.ansicolor.write_err(s)[source]

Write a string to sys.stderr, strip escapes if output is a pipe.

Parameters

s (string) – The title.

ansicolor.ansicolor.write_out(s)[source]

Write a string to sys.stdout, strip escapes if output is a pipe.

Parameters

s (string) – The title.

ansicolor.ansicolor.yellow(s, bold=False, reverse=False)

Colorize string in yellow

Parameters
  • s (string) – The string to colorize.

  • bold (bool) – Whether to mark up in bold.

  • reverse (bool) – Whether to mark up in reverse video.

Return type

string