astra.utils.color_print

colour_print.py

Created by José Sánchez-Gallego on 19 Nov 2013. Copyright (c) 2013. All rights reserved. Licensed under a 3-clause BSD license.

This module includes astropy-based functions for colour printing.

Module Contents

Functions

_write_with_fallback(s, write, fileobj) Write the supplied string with the given write function like
isatty(file) Returns True if file is a tty.
color_text(text, color) Returns a string wrapped in ANSI color codes for coloring the
_decode_preferred_encoding(s) Decode the supplied byte string using the preferred encoding
color_print(*args, **kwargs) Prints colors and styles to the terminal uses ANSI escape
astra.utils.color_print.OutStream
astra.utils.color_print.IS_PY3
astra.utils.color_print._DEFAULT_ENCODING = utf-8
astra.utils.color_print._write_with_fallback(s, write, fileobj)

Write the supplied string with the given write function like write(s), but use a writer for the locale’s preferred encoding in case of a UnicodeEncodeError. Failing that attempt to write with ‘utf-8’ or ‘latin-1’.

astra.utils.color_print.isatty(file)

Returns True if file is a tty.

Most built-in Python file-like objects have an isatty member, but some user-defined types may not, so this assumes those are not ttys.

astra.utils.color_print.color_text(text, color)

Returns a string wrapped in ANSI color codes for coloring the text in a terminal:

colored_text = color_text('Here is a message', 'blue')

This won’t actually effect the text until it is printed to the terminal.

Parameters:
  • text (str) – The string to return, bounded by the color codes.
  • color (str) – An ANSI terminal color name. Must be one of: black, red, green, brown, blue, magenta, cyan, lightgrey, default, darkgrey, lightred, lightgreen, yellow, lightblue, lightmagenta, lightcyan, white, or ‘’ (the empty string).
astra.utils.color_print._decode_preferred_encoding(s)

Decode the supplied byte string using the preferred encoding for the locale (locale.getpreferredencoding) or, if the default encoding is invalid, fall back first on utf-8, then on latin-1 if the message cannot be decoded with utf-8.

astra.utils.color_print.color_print(*args, **kwargs)

Prints colors and styles to the terminal uses ANSI escape sequences.

color_print('This is the color ', 'default', 'GREEN', 'green')
Parameters:
  • args (positional) –

    The positional arguments come in pairs (msg, color), where msg is the string to display and color is the color to display it in.

    color is an ANSI terminal color name. Must be one of: black, red, green, brown, blue, magenta, cyan, lightgrey, default, darkgrey, lightred, lightgreen, yellow, lightblue, lightmagenta, lightcyan, white, or ‘’ (the empty string).

  • file (writeable file-like object, optional) – Where to write to. Defaults to sys.stdout. If file is not a tty (as determined by calling its isatty member, if one exists), no coloring will be included.
  • end (str, optional) – The ending of the message. Defaults to \n. The end will be printed after resetting any color or font state.