Class: String
Constant Summary collapse
- ANSI_ESCAPE_SEQUENCE_RX =
/\e([#{Regexp::escape prefix}]?)([0-9;\{\?\}]*)([0-#{Regexp::escape 176.chr}])/
Instance Method Summary collapse
-
#ansi_sequences ⇒ Object
returns an array listing all detected ANSI sequences in self.
-
#replace_ansi ⇒ Object
Creates a new String that is a copy of this String.
Instance Method Details
#ansi_sequences ⇒ Object
returns an array listing all detected ANSI sequences in self. These are instances of ANSI::Code.
6 7 8 9 10 |
# File 'lib/core_ext/string.rb', line 6 def ansi_sequences ANSI_ESCAPE_SEQUENCE_RX.each_match(self).collect do |match| ANSI.recognize(match[0]) end end |
#replace_ansi ⇒ Object
Creates a new String that is a copy of this String. Takes a block which will receive each occurrance of an ANSI escape sequence; the escape sequence is replaced by the return value of the block.
Example:
ANSI.red { "hello" }.replace_ansi do |match|
case match
when RED then "(red)"
when RESET_COLOR then "(normal)"
end
end
#=> "(red)hello(normal)"
25 26 27 28 29 30 31 32 33 |
# File 'lib/core_ext/string.rb', line 25 def replace_ansi copy = self.dup ANSI_ESCAPE_SEQUENCE_RX.each_match(copy).collect do |match| ansi_match = ANSI.recognize(match[0]) result = yield ansi_match copy.gsub!(match[0], result) end copy end |