Skip to main content

colour_text

colour_text and colour-print command

colour-print is a command that makes printing coloured text easier to do on Unix, macOS and Windows. It uses the ColourText class to colour the text.

The command uses markup using <>colour-name text<>.

    $ colour-print "<>info Info:<> this is an <>em informational<> message"
    $ colour-print "<>error Error: This is an error message<>"

The first argument is treated as a format string if there are more arguments.

    $ colour-print "<>info Info:<> Home folder is %s" "$HOME"

screenshot of help output

class ColourText

The ColourText class converts strings with colour markup into a form suitable for printing on a terminal that support colour text. This include most terminal emulators on macOS, Windows and Unix.

To colour a section of text use the marker followed by the colour name a space and the text to be coloured ending with the marker.

    from colour_text import ColourText

    ct = ColourText()
    ct.initTerminal()

    print( ct.convert( "The next section is in green: <>green example<>." ) )

To include the marker as literal text use two adjacent markers.

    from colour_text import ColourText

    ct = ColourText()
    ct.initTerminal()

    print( ct("A <>red literal marker<> <><> in the string") )

ColoutText can be use with gettext for internationalized applications.

    from colour_text import ColourText

    ct = ColourText()
    ct.initTerminal()

    message = "<>red Error: cannot open file %s<>"
    i18n_message = _(message)
    coloured_i18n_message = ct(i18n_message)
    formatted_message = coloured_i18n_message % (file_name,)

    print( formatted_message )

    # or in one line
    print ct( _("<>red Error: cannot open file %s<>") ) % (file_name,) )

class ColourText

  • __init__( marker='<>' )

    The marker is the string used to markup the colour sections which defaults to <>.

  • initTerminal()

    Ensure the terminal can display coloured text.

    Must be called on Windows and can be safely called on macOS and Unix systems.

  • define( name, colour_def )

    Define a colour name for use in the marked up sections. The colour_def is a list of existing colour names or a single name.

    The builtin foreground colour names are:

      bold, black, brown, green, yellow, blue,
      magenta, cyan, gray, red, lightred, lightgreen,
      lightyellow, lightblue, lightmagenta, lightcyan
      and white.
    

    The builtin background colour names are:

      bg-black, bg-brown, bg-green, bg-yellow,
      bg-blue, bg-magenta, bg-cyan, bg-gray
      and bg-white.
    

    For example add the name info as green text and error as to be red text on a white background:

        ct = ColourText()
        ct.define( 'info', 'green' )
        ct.define( 'error', ('red', 'bg-white') )

        ct( "Error messages are <>error shown like this<>" ) 
  • convert( colour_text )

    Interpret the colour markup in the colour_text string and return a string suitable for printing on the terminal.

  • __call__( colour_text )

    Call convert( colour_text ) in a concise way.

Release files for colour-text 1.0.5

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

Source distribution (sdist)

Source distribution for colour-text 1.0.5
File Size Uploaded
colour-text-1.0.5.tar.gz 96.7 kB Details

Built distribution (wheel)

Table of built distributions (wheels) for colour-text 1.0.5
File Interpreter ABI Platform
colour_text-1.0.5-py3-none-any.whl Python 3 none any Details

Total release size: 106.6 kB

Release files / colour-text-1.0.5.tar.gz

Download URL colour-text-1.0.5.tar.gz
Size 96.7 kB
Tags Source
SHA-256 checksum
How to use checksums
440dbe2cb7aa3ef3abed85ee3c7efff67fd18a2764c1c2777e45b7e754bb8d1b
BLAKE2b-256 checksum
How to use checksums
d48426c7fe389231822bb84e45a764c0419f3b7412324223f01ff1dd75527063
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via twine/4.0.2 CPython/3.11.5

Release files / colour_text-1.0.5-py3-none-any.whl

Download URL colour_text-1.0.5-py3-none-any.whl
Size 10.0 kB
Tags Python 3
SHA-256 checksum
How to use checksums
78d7da9abc070449796a095f68e009e7ff2cf8ba12cbc2c051f23d2583d63f74
BLAKE2b-256 checksum
How to use checksums
ec77fba18e3257b38aed3e1be6c116f9dc13f267e61420cae59be4663190982f
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via twine/4.0.2 CPython/3.11.5

Release history Release notifications | RSS feed

This release

1.0.5 This release

2 release files

1.0.4

1 release file

1.0.3

1 release file

1.0.2

2 release files

1.0.1

2 release files

1.0.0

2 release files

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