Module: Cinch::Formatting
- Defined in:
- lib/cinch/formatting.rb
Overview
This module can be used for adding colors and formatting to 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)
Constant Summary
Class Method Summary (collapse)
-
+ (String) format(*settings, string)
The formatted string.
Class Method Details
+ (String) format(*settings, string)
The formatted string
84 85 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 |
# File 'lib/cinch/formatting.rb', line 84 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) return prepend + string + append end |