Module: IRCSupport::Formatting
- Defined in:
- lib/ircsupport/formatting.rb
Constant Summary collapse
- @@color =
/[\x03\x04\x1b]/
- @@formatting =
/[\x02\x06\x11\x16\x1d\x1f]/
- @@mirc_color =
/ \x03 (?: ,\d{1,2} | \d{1,2} (?: ,\d{1,2})? )? /x
- @@rgb_color =
/\x04[0-9a-fA-F]{0,6}/
- @@ecma48_color =
/\x1b\[.*?[\x00-\x1f\x40-\x7e]/
- @@normal =
/\x0f/
- @@attributes =
{ reset: 15.chr, bold: 2.chr, underline: 31.chr, underlined: 31.chr, inverse: 22.chr, reverse: 22.chr, reversed: 22.chr, italic: 29.chr, fixed: 17.chr, blink: 6.chr, }
- @@colors =
{ 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", }
Class Method Summary collapse
-
.has_color?(string) ⇒ Boolean
Check if string has IRC color codes.
-
.has_formatting?(string) ⇒ Boolean
Check if string has IRC formatting codes.
-
.irc_format(*settings, string) ⇒ String
Apply IRC formatting codes to a string.
-
.irc_format!(*settings, string) ⇒ String
Apply IRC formatting codes to a string, modifying it in place.
-
.strip_color(string) ⇒ String
Strip IRC color codes from a string.
-
.strip_color!(string) ⇒ String
Strip IRC color codes from a string, modifying it in place.
-
.strip_formatting(string) ⇒ String
Strip IRC formatting codes from a string.
-
.strip_formatting!(string) ⇒ String
Strip IRC formatting codes from a string, modifying it in place.
Class Method Details
.has_color?(string) ⇒ Boolean
Check if string has IRC color codes.
55 56 57 58 |
# File 'lib/ircsupport/formatting.rb', line 55 def has_color?(string) return true if string =~ @@color return false end |
.has_formatting?(string) ⇒ Boolean
Check if string has IRC formatting codes.
63 64 65 66 |
# File 'lib/ircsupport/formatting.rb', line 63 def has_formatting?(string) return true if string =~ @@formatting return false end |
.irc_format(*settings, string) ⇒ String
Apply IRC formatting codes to a string.
148 149 150 151 |
# File 'lib/ircsupport/formatting.rb', line 148 def irc_format(*settings, string) string = string.dup return irc_format!(*settings, string) end |
.irc_format!(*settings, string) ⇒ String
Apply IRC formatting codes to a string, modifying it in place.
111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 |
# File 'lib/ircsupport/formatting.rb', line 111 def irc_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) string.insert(0, prepend) string << append return string end |
.strip_color(string) ⇒ String
Strip IRC color codes from a string.
83 84 85 86 |
# File 'lib/ircsupport/formatting.rb', line 83 def strip_color(string) string = string.dup return strip_color!(string) end |
.strip_color!(string) ⇒ String
Strip IRC color codes from a string, modifying it in place.
71 72 73 74 75 76 77 78 |
# File 'lib/ircsupport/formatting.rb', line 71 def strip_color!(string) [@@mirc_color, @@rgb_color, @@ecma48_color].each do |pattern| string.gsub!(pattern, '') end # strip cancellation codes too if there are no formatting codes string.gsub!(@@normal) if !has_color?(string) return string end |
.strip_formatting(string) ⇒ String
Strip IRC formatting codes from a string.
101 102 103 104 |
# File 'lib/ircsupport/formatting.rb', line 101 def strip_formatting(string) string = string.dup return strip_formatting!(string) end |
.strip_formatting!(string) ⇒ String
Strip IRC formatting codes from a string, modifying it in place.
91 92 93 94 95 96 |
# File 'lib/ircsupport/formatting.rb', line 91 def strip_formatting!(string) string.gsub!(@@formatting, '') # strip cancellation codes too if there are no color codes string.gsub!(@@normal) if !has_color?(string) return string end |