colour_text that makes printing coloured text easier to do on Unix, macOS and Windows
Project description
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"
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. Thecolour_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 anderror
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.
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
Built Distribution
File details
Details for the file colour-text-1.0.5.tar.gz
.
File metadata
- Download URL: colour-text-1.0.5.tar.gz
- Upload date:
- Size: 96.7 kB
- Tags: Source
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/4.0.2 CPython/3.11.5
File hashes
Algorithm | Hash digest | |
---|---|---|
SHA256 | 440dbe2cb7aa3ef3abed85ee3c7efff67fd18a2764c1c2777e45b7e754bb8d1b |
|
MD5 | d41255827413f0692b434240d60802b4 |
|
BLAKE2b-256 | d48426c7fe389231822bb84e45a764c0419f3b7412324223f01ff1dd75527063 |
File details
Details for the file colour_text-1.0.5-py3-none-any.whl
.
File metadata
- Download URL: colour_text-1.0.5-py3-none-any.whl
- Upload date:
- Size: 10.0 kB
- Tags: Python 3
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/4.0.2 CPython/3.11.5
File hashes
Algorithm | Hash digest | |
---|---|---|
SHA256 | 78d7da9abc070449796a095f68e009e7ff2cf8ba12cbc2c051f23d2583d63f74 |
|
MD5 | 2418d96926a665ed5e71bd02f2573be9 |
|
BLAKE2b-256 | ec77fba18e3257b38aed3e1be6c116f9dc13f267e61420cae59be4663190982f |