Module: Cinch::Formatting

Defined in:
lib/cinch/formatting.rb

Overview

This module can be used for adding and removing colors and formatting to/from messages.

The format codes used are those defined by mIRC, which are also the ones supported by most clients.

For usage instructions and examples, see Formatting.format.

List of valid colors

  • aqua

  • black

  • blue

  • brown

  • green

  • grey

  • lime

  • orange

  • pink

  • purple

  • red

  • royal

  • silver

  • teal

  • white

  • yellow

List of valid attributes

  • bold

  • italic

  • reverse/reversed

  • underline/underlined

Other

  • reset (Resets all formatting to the client’s defaults)

Since:

  • 2.0.0

Constant Summary collapse

COLORS =

Since:

  • 2.0.0

{
  white: "00",
  black: "01",
  blue: "02",
  green: "03",
  red: "04",
  brown: "05",
  purple: "06",
  orange: "07",
  yellow: "08",
  lime: "09",
  teal: "10",
  aqua: "11",
  royal: "12",
  pink: "13",
  grey: "14",
  silver: "15"
}
ATTRIBUTES =

Since:

  • 2.0.0

{
  bold: 2.chr,
  underlined: 31.chr,
  underline: 31.chr,
  reversed: 22.chr,
  reverse: 22.chr,
  italic: 29.chr,
  reset: 15.chr
}

Class Method Summary collapse

Class Method Details

.format(*settings, string) ⇒ String

Returns the formatted string.

Examples:

Nested formatting, combining text styles and colors

reply = Format(:underline, "Hello %s! Is your favourite color %s?" % [Format(:bold, "stranger"), Format(:red, "red")])

Parameters:

  • settings (Array<Symbol>)

    The colors and attributes to apply. When supplying two colors, the first will be used for the foreground and the second for the background.

  • string (String)

    The string to format.

Returns:

  • (String)

    the formatted string

Raises:

  • (ArgumentError)

    When passing more than two colors as arguments.

See Also:

Since:

  • 2.0.0



86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
# File 'lib/cinch/formatting.rb', line 86

def self.format(*settings, string)
  string = string.dup

  attributes = settings.select { |k| ATTRIBUTES.has_key?(k) }.map { |k| ATTRIBUTES[k] }
  colors = settings.select { |k| COLORS.has_key?(k) }.map { |k| COLORS[k] }
  if colors.size > 2
    raise ArgumentError, "At most two colors (foreground and background) might be specified"
  end

  attribute_string = attributes.join
  color_string = if colors.empty?
    ""
  else
    "\x03#{colors.join(",")}"
  end

  prepend = attribute_string + color_string
  append = ATTRIBUTES[:reset]

  # Attributes act as toggles, so e.g. underline+underline = no
  # underline. We thus have to delete all duplicate attributes
  # from nested strings.
  string.delete!(attribute_string)

  # Replace the reset code of nested strings to continue the
  # formattings of the outer string.
  string.gsub!(/#{ATTRIBUTES[:reset]}/, ATTRIBUTES[:reset] + prepend)
  prepend + string + append
end

.unformat(string) ⇒ String

Deletes all mIRC formatting codes from the string. This strips formatting for bold, underline and so on, as well as color codes. This does include removing the numeric arguments.

Parameters:

  • string (String)

    The string to filter

Returns:

  • (String)

    The filtered string

Since:

  • 2.2.0



123
124
125
# File 'lib/cinch/formatting.rb', line 123

def self.unformat(string)
  string.gsub(/[\x02\x0f\x16\x1f\x12]|\x03(\d{1,2}(,\d{1,2})?)?/, "")
end