Formatting#

The formatting module includes functions to apply IRC formatting to text.

Availability: 4.5+

sopel.formatting.CONTROL_BOLD = '\x02'#

The control code to start or end bold formatting.

sopel.formatting.CONTROL_COLOR = '\x03'#

The control code to start or end color formatting.

sopel.formatting.CONTROL_FORMATTING = ['\x0f', '\x03', '\x04', '\x02', '\x1d', '\x1f', '\x1e', '\x11', '\x16']#

A list of all control characters expected to appear as formatting.

sopel.formatting.CONTROL_HEX_COLOR = '\x04'#

The control code to start or end hexadecimal color formatting.

sopel.formatting.CONTROL_ITALIC = '\x1d'#

The control code to start or end italic formatting.

sopel.formatting.CONTROL_MONOSPACE = '\x11'#

The control code to start or end monospace formatting.

sopel.formatting.CONTROL_NORMAL = '\x0f'#

The control code to reset formatting.

sopel.formatting.CONTROL_REVERSE = '\x16'#

The control code to start or end reverse-color formatting.

sopel.formatting.CONTROL_STRIKETHROUGH = '\x1e'#

The control code to start or end strikethrough formatting.

sopel.formatting.CONTROL_UNDERLINE = '\x1f'#

The control code to start or end underlining.

sopel.formatting.bold(text)#

Return the text, with bold IRC formatting.

Parameters:

text (str) – the text to format

Raises:

TypeError – if text is not a string

Return type:

str

sopel.formatting.color(text, fg=None, bg=None)#

Return the text, with the given colors applied in IRC formatting.

Parameters:
  • text (str) – the text to format

  • fg (mixed) – the foreground color

  • bg (mixed) – the background color

Raises:
  • TypeError – if text is not a string

  • ValueError – if fg or bg is an unrecognized color value

Return type:

str

The color can be a string of the color name, or an integer in the range 0-99. The known color names can be found in the colors class of this module.

class sopel.formatting.colors(value)#

Mapping of color names to mIRC code values.

BLACK = '01'#
BLUE = '02'#
BROWN = '05'#
CYAN = '11'#
FUCHSIA = '13'#
GRAY = '14'#
GREEN = '03'#
GREY = '14'#
LIGHT_BLUE = '12'#
LIGHT_CYAN = '11'#
LIGHT_GRAY = '15'#
LIGHT_GREEN = '09'#
LIGHT_GREY = '15'#
LIGHT_PURPLE = '13'#
LIGHT_RED = '04'#
LIME = '09'#
MAROON = '05'#
NAVY = '02'#
OLIVE = '07'#
ORANGE = '07'#
PINK = '13'#
PURPLE = '06'#
RED = '04'#
ROYAL = '12'#
SILVER = '15'#
TEAL = '10'#
WHITE = '00'#
YELLOW = '08'#
sopel.formatting.hex_color(text, fg=None, bg=None)#

Return the text, with the given colors applied in IRC formatting.

Parameters:
  • text (str) – the text to format

  • fg (str) – the foreground color

  • bg (str) – the background color

Raises:
  • TypeError – if text is not a string

  • ValueError – if fg or bg is an unrecognized color value

Return type:

str

The color can be provided with a string of either 3 or 6 hexadecimal digits. As in CSS, 3-digit colors will be interpreted as if they were 6-digit colors with each digit repeated (e.g. color c90 is identical to cc9900). Do not include the leading # symbol.

Note

This is a relatively new IRC formatting convention. Use it only when you can afford to have its meaning lost, as not many clients support it yet.

sopel.formatting.italic(text)#

Return the text, with italic IRC formatting.

Parameters:

text (str) – the text to format

Raises:

TypeError – if text is not a string

Return type:

str

sopel.formatting.monospace(text)#

Return the text, with monospace IRC formatting.

Parameters:

text (str) – the text to format

Raises:

TypeError – if text is not a string

Return type:

str

Note

This is a relatively new IRC formatting convention. Use it only when you can afford to have its meaning lost, as not many clients support it yet.

sopel.formatting.plain(text)#

Return the text without any IRC formatting.

Parameters:

text (str) – text with potential IRC formatting control code(s)

Raises:

TypeError – if text is not a string

Return type:

str

sopel.formatting.reverse(text)#

Return the text, with reverse-color IRC formatting.

Parameters:

text (str) – the text to format

Raises:

TypeError – if text is not a string

Return type:

str

Note

This code isn’t super well supported, and its behavior even in clients that understand it (e.g. mIRC) can be unpredictable. Use it carefully.

sopel.formatting.strikethrough(text)#

Return the text, with strikethrough IRC formatting.

Parameters:

text (str) – the text to format

Raises:

TypeError – if text is not a string

Return type:

str

Note

This is a relatively new IRC formatting convention. Use it only when you can afford to have its meaning lost, as not many clients support it yet.

sopel.formatting.underline(text)#

Return the text, with underline IRC formatting.

Parameters:

text (str) – the text to format

Raises:

TypeError – if text is not a string

Return type:

str