ganga.GangaCore.Utility.ColourText module

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.