sciris.ansicolors

Copy of Jonathan Eunice and Georgios Verigakis’ ansicolors Python module: https://pypi.org/project/ansicolors/

Included here to avoid an additional dependency.

Functions here are not intended for direct use by the user, but of course they can be.

New in version 2.0.0.

Functions

ansilen

Given a string with embedded ANSI codes, what would its length be without those codes?

color

Add ANSI colors and styles to a string.

parse_rgb

Convert string to an RGB color

strip_color

Remove ANSI color/style sequences from a string.

color(s, fg=None, bg=None, style=None)[source]

Add ANSI colors and styles to a string.

Parameters
  • s (str) – String to format.

  • fg (str|int|tuple) – Foreground color specification.

  • bg (str|int|tuple( bg) – Background color specification.

  • style (str) – Style names, separated by ‘+’

Returns

Formatted string.

strip_color(s)[source]

Remove ANSI color/style sequences from a string. The set of all possibly ANSI sequences is large, so does not try to strip every possible one. But does strip some outliers seen not just in text generated by this module, but by other ANSI colorizers in the wild. Those include  (aka EL or erase to end of line) and  a terse version of the more common .

ansilen(s)[source]

Given a string with embedded ANSI codes, what would its length be without those codes?

black(s, *, fg='black', bg=None, style=None)

Add ANSI colors and styles to a string.

Parameters
  • s (str) – String to format.

  • fg (str|int|tuple) – Foreground color specification.

  • bg (str|int|tuple( bg) – Background color specification.

  • style (str) – Style names, separated by ‘+’

Returns

Formatted string.

red(s, *, fg='red', bg=None, style=None)

Add ANSI colors and styles to a string.

Parameters
  • s (str) – String to format.

  • fg (str|int|tuple) – Foreground color specification.

  • bg (str|int|tuple( bg) – Background color specification.

  • style (str) – Style names, separated by ‘+’

Returns

Formatted string.

green(s, *, fg='green', bg=None, style=None)

Add ANSI colors and styles to a string.

Parameters
  • s (str) – String to format.

  • fg (str|int|tuple) – Foreground color specification.

  • bg (str|int|tuple( bg) – Background color specification.

  • style (str) – Style names, separated by ‘+’

Returns

Formatted string.

yellow(s, *, fg='yellow', bg=None, style=None)

Add ANSI colors and styles to a string.

Parameters
  • s (str) – String to format.

  • fg (str|int|tuple) – Foreground color specification.

  • bg (str|int|tuple( bg) – Background color specification.

  • style (str) – Style names, separated by ‘+’

Returns

Formatted string.

blue(s, *, fg='blue', bg=None, style=None)

Add ANSI colors and styles to a string.

Parameters
  • s (str) – String to format.

  • fg (str|int|tuple) – Foreground color specification.

  • bg (str|int|tuple( bg) – Background color specification.

  • style (str) – Style names, separated by ‘+’

Returns

Formatted string.

magenta(s, *, fg='magenta', bg=None, style=None)

Add ANSI colors and styles to a string.

Parameters
  • s (str) – String to format.

  • fg (str|int|tuple) – Foreground color specification.

  • bg (str|int|tuple( bg) – Background color specification.

  • style (str) – Style names, separated by ‘+’

Returns

Formatted string.

cyan(s, *, fg='cyan', bg=None, style=None)

Add ANSI colors and styles to a string.

Parameters
  • s (str) – String to format.

  • fg (str|int|tuple) – Foreground color specification.

  • bg (str|int|tuple( bg) – Background color specification.

  • style (str) – Style names, separated by ‘+’

Returns

Formatted string.

white(s, *, fg='white', bg=None, style=None)

Add ANSI colors and styles to a string.

Parameters
  • s (str) – String to format.

  • fg (str|int|tuple) – Foreground color specification.

  • bg (str|int|tuple( bg) – Background color specification.

  • style (str) – Style names, separated by ‘+’

Returns

Formatted string.

bold(s, fg=None, bg=None, *, style='bold')

Add ANSI colors and styles to a string.

Parameters
  • s (str) – String to format.

  • fg (str|int|tuple) – Foreground color specification.

  • bg (str|int|tuple( bg) – Background color specification.

  • style (str) – Style names, separated by ‘+’

Returns

Formatted string.

none(s, fg=None, bg=None, *, style='none')

Add ANSI colors and styles to a string.

Parameters
  • s (str) – String to format.

  • fg (str|int|tuple) – Foreground color specification.

  • bg (str|int|tuple( bg) – Background color specification.

  • style (str) – Style names, separated by ‘+’

Returns

Formatted string.

faint(s, fg=None, bg=None, *, style='faint')

Add ANSI colors and styles to a string.

Parameters
  • s (str) – String to format.

  • fg (str|int|tuple) – Foreground color specification.

  • bg (str|int|tuple( bg) – Background color specification.

  • style (str) – Style names, separated by ‘+’

Returns

Formatted string.

italic(s, fg=None, bg=None, *, style='italic')

Add ANSI colors and styles to a string.

Parameters
  • s (str) – String to format.

  • fg (str|int|tuple) – Foreground color specification.

  • bg (str|int|tuple( bg) – Background color specification.

  • style (str) – Style names, separated by ‘+’

Returns

Formatted string.

underline(s, fg=None, bg=None, *, style='underline')

Add ANSI colors and styles to a string.

Parameters
  • s (str) – String to format.

  • fg (str|int|tuple) – Foreground color specification.

  • bg (str|int|tuple( bg) – Background color specification.

  • style (str) – Style names, separated by ‘+’

Returns

Formatted string.

Add ANSI colors and styles to a string.

Parameters
  • s (str) – String to format.

  • fg (str|int|tuple) – Foreground color specification.

  • bg (str|int|tuple( bg) – Background color specification.

  • style (str) – Style names, separated by ‘+’

Returns

Formatted string.

blink2(s, fg=None, bg=None, *, style='blink2')

Add ANSI colors and styles to a string.

Parameters
  • s (str) – String to format.

  • fg (str|int|tuple) – Foreground color specification.

  • bg (str|int|tuple( bg) – Background color specification.

  • style (str) – Style names, separated by ‘+’

Returns

Formatted string.

negative(s, fg=None, bg=None, *, style='negative')

Add ANSI colors and styles to a string.

Parameters
  • s (str) – String to format.

  • fg (str|int|tuple) – Foreground color specification.

  • bg (str|int|tuple( bg) – Background color specification.

  • style (str) – Style names, separated by ‘+’

Returns

Formatted string.

concealed(s, fg=None, bg=None, *, style='concealed')

Add ANSI colors and styles to a string.

Parameters
  • s (str) – String to format.

  • fg (str|int|tuple) – Foreground color specification.

  • bg (str|int|tuple( bg) – Background color specification.

  • style (str) – Style names, separated by ‘+’

Returns

Formatted string.

crossed(s, fg=None, bg=None, *, style='crossed')

Add ANSI colors and styles to a string.

Parameters
  • s (str) – String to format.

  • fg (str|int|tuple) – Foreground color specification.

  • bg (str|int|tuple( bg) – Background color specification.

  • style (str) – Style names, separated by ‘+’

Returns

Formatted string.

parse_rgb(s)[source]

Convert string to an RGB color