Skip to main content

Convenience functions for ANSI terminal colour sequences [color].

Latest release 20200729:

  • New env_no_color() test function honouring the $NO_COLOUR envvar per https://no-color.org/
  • colourise: get the "normal" default from the global NORMAL_COLOUR.

Mapping and function for adding ANSI terminal colour escape sequences to strings for colour highlighting of output.

Function colourise(s, colour=None, uncolour=None)

Return a string enclosed in colour-on and colour-off ANSI sequences.

  • colour: names the desired ANSI colour.
  • uncolour: may be used to specify the colour-off colour; the default is 'normal' (from NORMAL_COLOUR).

Function colourise_patterns(s, patterns, default_colour=None)

Colourise a string s according to patterns.

Parameters:

  • s: the string.
  • patterns: a sequence of patterns.
  • default_colour: if a string pattern has no colon, or starts with a colon, use this colour; default "cyan" (from DEFAULT_HIGHLIGHT).

Each pattern may be:

  • a string of the form "[colour]:regexp"
  • a string containing no colon, taken to be a regexp
  • a tuple of the form (colour,regexp)
  • a regexp object

Returns the string with ANSI colour escapes embedded.

Function env_no_color(environ=None)

Test the $NO_COLOR environment variable per the specification at https://no-color.org/

Function make_pattern(pattern, default_colour=None)

Convert a pattern specification into a (colour,regexp) tuple.

Parameters:

  • pattern: the pattern to parse
  • default_colour: the highlight colour, default "cyan" (from DEFAULT_HIGHLIGHT).

Each pattern may be:

  • a string of the form "[colour]:regexp"
  • a string containing no colon, taken to be a regexp
  • a tuple of the form (colour,regexp)
  • a regexp object

Function make_patterns(patterns, default_colour=None)

Convert an iterable of pattern specifications into a list of (colour,regexp) tuples.

Parameters:

  • patterns: an iterable of patterns to parse
  • default_colour: the highlight colour, default "cyan" (from DEFAULT_HIGHLIGHT).

Each pattern may be:

  • a string of the form "[colour]:regexp"
  • a string containing no colon, taken to be a regexp
  • a tuple of the form (colour, regexp)
  • a regexp object

Release Log

Release 20200729:

  • New env_no_color() test function honouring the $NO_COLOUR envvar per https://no-color.org/
  • colourise: get the "normal" default from the global NORMAL_COLOUR.

Release 20180726: greater markdowning of docstrings

Release 20180725.2: some markdown, still release testing

Release 20180725.1: Trivial changes, testing release process.

Release 20180725: rename second colourise function to colourise_patterns, was shadowing things

Release 20180422:

  • New function colourise to colour strings with regexp matches.
  • New helper functions make_pattern and make_patterns to construct (colour, regexp)s from specifications.
  • New public names NORMAL_COLOUR and DEFAULT_HIGHLIGHT.

Release 20170902: Release metadata changes.

Release 20160828: Add PyPI category, other minor tweaks.

Release 20150112: PyPI release.

Release 20150107: first standalone pypi release

Release files for cs-ansi-colour 20200729

For a detailed explanation of source distributions (sdists) and built distributions (wheels), please see the package formats documentation.

Source distribution (sdist)

Source distribution for cs-ansi-colour 20200729
File Size Uploaded
cs.ansi_colour-20200729.tar.gz 4.1 kB Details

Release files / cs.ansi_colour-20200729.tar.gz

Download URL cs.ansi_colour-20200729.tar.gz
Size 4.1 kB
Tags Source
SHA-256 checksum
How to use checksums
291c2f8edc8e470448d5969e291164951e17ee37282e1c544500308220152850
BLAKE2b-256 checksum
How to use checksums
b351b44423dffbfea6eaa7c8ff2aeca71b9aba334b3906b47ff762bf239adb01
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via twine/3.2.0 pkginfo/1.5.0.1 requests/2.24.0 setuptools/47.1.0 requests-toolbelt/0.9.1 tqdm/4.48.0 CPython/3.8.5

Release history Release notifications | RSS feed

This release

20200729 This release

1 release file

Anthropic, PBC Visionary sponsor Bloomberg Visionary sponsor Hudson River Trading Visionary sponsor Meta Visionary sponsor NVIDIA Visionary sponsor Microsoft Sustainability sponsor Depot Continuous Integration AWS Cloud computing and Security Sponsor Datadog Monitoring Fastly CDN Google Download Analytics Sentry Error logging StatusPage Status page