Output Utils

class jogger.utils.output.Styler(no_color=False)

An object containing methods for generating styled text for a palette of preconfigured style roles. Text is styled by wrapping it in appropriate ANSI graphics codes.

Text styling can be disabled using the no_color constructor argument. When True, all methods will return the provided text unmodified. This enables a common API between environments that support styled text and those that do not.

PALETTE defines the name and attributes of each preconfigured role. Its entries are mapped into methods on the class that can be used as shortcuts to apply the corresponding set of style attributes to the given text. Subclasses can define their own palettes.

Additionally, the apply() method can be used to apply any arbitrary text styles if the PALETTE configuration doesn’t include a suitable role.

Usage:

styler = Styler()
success_message = styler.success('It worked!')
error_message = styler.error('It failed!')
message = styler.apply('hello', fg='red', bg='blue', opts=('blink', ))
PALETTE

A dictionary defining the name and attributes of each preconfigured role. Its entries are mapped into methods on the class that can be used as shortcuts to apply the corresponding set of style attributes to the given text. Subclasses can define their own palettes.

apply(text, fg=None, bg=None, options=(), reset=True)

Return text, enclosed in ANSI graphics codes, as dictated by fg, bg, and options. If reset is True, the returned text will be terminated by the RESET code.

If configured with no_color=True, return the text unmodified.

Valid colors:

'black', 'red', 'green', 'yellow', 'blue', 'magenta', 'cyan', 'white'

Valid options:

'bold', 'underscore', 'blink', 'reverse', 'conceal'

Examples:

styler.apply('hello', fg='red', bg='blue', opts=('blink', ))
styler.apply('goodbye', opts=('underscore', ))
print(styler.apply('first line', fg='red', reset=False))
print('this should be red too')
print(styler.apply('and so should this'))
print('this should not be red')
Parameters:
  • text – The text to style.

  • fg – The foreground colour to apply to text.

  • bg – The background colour to apply to text.

  • options – The display options to apply to text.

  • resetTrue to clear all applied styles at the end of text, False to allow them to affect subsequent output.

Returns:

The styled text.

reset()

Return the ANSI RESET graphics code. Can be used to reset a style created by calling apply(..., reset=False).

If configured with no_color=True, return an empty string.

Returns:

The ANSI RESET graphics code.