Skip to main content

Convenience functions for ANSI terminal colour sequences [color].

Project description

Convenience functions for ANSI terminal colour sequences [color].

Latest release 20260531: Add a bunch of other SGR parameters, though terminal support for these is very spotty.

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

Short summary:

  • colour_escape: Return the ANSI escape sequence to activate the colour code. code may be an int or a str which indexes COLOUR_CODES.
  • colourise: Return a string enclosed in colour-on and colour-off ANSI sequences.
  • colourise_patterns: Colourise a string s according to patterns.
  • env_no_color: Test the $NO_COLOR environment variable per the specification at https://no-color.org/.
  • make_pattern: Convert a pattern specification into a (colour,regexp) tuple.
  • make_patterns: Convert an iterable of pattern specifications into a list of (colour,regexp) tuples.
  • TerminalColors: A parser for /etc/terminal-colors.d' files.

Module contents:

  • colour_escape(code): Return the ANSI escape sequence to activate the colour code. code may be an int or a str which indexes COLOUR_CODES.

  • colourise(s, colour=None, uncolour=None, colour_padding=False): Return a string enclosed in colour-on and colour-off ANSI sequences.

    Parameters:

    • s: the string to colour
    • colour: names the desired ANSI colour.
    • uncolour: may be used to specify the colour-off colour; the default is 'normal' (from NORMAL_COLOUR).
    • colour_padding: default False; if true colour the entire text, otherwise do not colour the leading and trailing whitespace of each line.
  • 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.

  • env_no_color(environ=None): Test the $NO_COLOR environment variable per the specification at https://no-color.org/

  • 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
  • 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
  • class TerminalColors: A parser for /etc/terminal-colors.d' files.

TerminalColors.__init__(self, util_name=None, term_name=None, type_name=None, colors_dirpath=None, envvar=None): Initialise the TerminalColors instance.

Parameters:

  • util_name: optional utility name, default from sys.argv[0]
  • term_name: optional terminal name, default from the $TERM envvar
  • type_name: optional type name, default 'enable'
  • colors_dirpath: optional specification files directory path, default from TerminalColors.TERMINAL_COLORS_D
  • envvar: environment variable to override matches; the default util_name+'_COLORS', thus $LS_COLORS if util_name=='ls'. That may be the value False if no environment variable should be an override.

TerminalColors.convert_sequence(sequence): Convert a colour specification to an escape sequence.

TerminalColors.find_specfile(self): Locate the most specific specification file matching our criteria. Return None if no file matches.

TerminalColors.mapping: The mapping of name to escape sequence.

TerminalColors.scan(self, path=None): Scan the colour specification in path and yield (name,escape_sequence) tuples.

Release Log

Release 20260531: Add a bunch of other SGR parameters, though terminal support for these is very spotty.

Release 20250306: colourise: do not colour the leading and trailing whitespace, provide colour_padding=False optional flag to disable.

Release 20220227:

  • New TerminalColors class parsing /etc/terminal-colors.d and honouring $*_COLORS.
  • New colour_escape(code) to make the escape sequence for a colour code.
  • New COLOUR_CODES mapping of known colour names.

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

Project details


Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

cs_ansi_colour-20260531.tar.gz (5.6 kB view details)

Uploaded Source

Built Distribution

If you're not sure about the file name format, learn more about wheel file names.

cs_ansi_colour-20260531-py2.py3-none-any.whl (6.7 kB view details)

Uploaded Python 2Python 3

File details

Details for the file cs_ansi_colour-20260531.tar.gz.

File metadata

  • Download URL: cs_ansi_colour-20260531.tar.gz
  • Upload date:
  • Size: 5.6 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/6.1.0 CPython/3.13.1

File hashes

Hashes for cs_ansi_colour-20260531.tar.gz
Algorithm Hash digest
SHA256 b6072c4787d094e1f2a3c9e4fe43bb6bfd3f676f669015ce3a18ad225ffc2b19
MD5 1ed0e13252b5cb28b62145b691b71f66
BLAKE2b-256 c321280e57bdaf1d99d0e867d07391e5855c7b03f93f0c985904f5a7d524ea07

See more details on using hashes here.

File details

Details for the file cs_ansi_colour-20260531-py2.py3-none-any.whl.

File metadata

File hashes

Hashes for cs_ansi_colour-20260531-py2.py3-none-any.whl
Algorithm Hash digest
SHA256 388eaf409fc92777b471c6b6a2551b64975539bb212ade360d3cdc52559e5ae0
MD5 69ba5f4159f0667d548abcfb2d87e435
BLAKE2b-256 14c93cb953892a49e4acaec1c523f3d9e1bd1c8555316e81718e9c4bd17e9bdb

See more details on using hashes here.

Supported by

AWS Cloud computing and Security Sponsor Datadog Monitoring Depot Continuous Integration Fastly CDN Google Download Analytics Pingdom Monitoring Sentry Error logging StatusPage Status page