Package Ganga :: Package Utility :: Module ColourText
[hide private]
[frames] | no frames]

Module ColourText

source code

Module containing classes to help with creating colour text.

Classes for text markup:

   ANSIMarkup - apply ANSI codes to the text
   NoMarkup - ingore colour codes and leave the text unchanged

ANSI code classes defined are:

   Foreground - create object carrying ANSI codes for
                changing text foreground colour;

   Background - create object carrying ANSI codes for
                changing text background colour;

   Effects    - create object carrying ANSI codes for
                changing text effects.

Example usage is as follows:

   fg = Foreground()
   bg = Background()
   fx = Effects()

   if coloring_enabled:
    # text will be blue by default and colour codes are applied
    m = ANSIMarkup(fg.blue)
   else:
    # colour codes are ignored
    m = NoMarkup()

    # Text will be coloured in red if coloring_enabled,
    # otherwise text will be unchanged.
    print m('Text in red',code=fg.red)

It is a better practice to use markup objects to apply colours
because it is easier to enable/disable the markup if desired.

However inserting the codes directly also works:

# Print text in specified colour.
print fg.some_colour + 'Text in some_colour' + fx.normal

# Print text with colour of background changed as specified.
print bg.some_colour + 'Text with background in some_colour' + fx.normal

# Print text with specified effect applied.
print fx.some_effect + 'Text with some_effect applied' + fx.normal

Note that each ANSI code overrides the previous one, and their effect
isn't cumulative.  Terminating a string with fx.normal in the above
examples ensures that subsequent text is output using the terminal
defaults.

For details of the colours and effects available, see help for
individual classes.


Version: 1.0

Date: 24 August 2005

Author: K.Harrison <Harrison@hep.phy.cam.ac.uk>

Classes [hide private]
  ANSIMarkup
Apply ANSI colouring codes.
  NoMarkup
Leave text unchanged.
  Background
Class for creating objects carrying ANSI codes for changing text background colour.
  Effects
Class for creating objects carrying ANSI codes for text effects.
  Foreground
Class for creating objects carrying ANSI codes for changing text foreground colour.
Functions [hide private]
 
getColour(name)
Get a colour code from the symbolic name: fg = Foreground(), bg = Background(), fx = Effects() The name examples fg.red, fx.normal, bg.white Raise ValueError if name undefined or malformed.
source code
Variables [hide private]
  colour_objects = {'fg': Foreground(), 'bg': Background(), 'fx'...
  __package__ = None
hash(x)
Variables Details [hide private]

colour_objects

Value:
{'fg': Foreground(), 'bg': Background(), 'fx': Effects()}