Class: Rainbow::StringUtils

Inherits:
Object
  • Object
show all
Defined in:
lib/rainbow/string_utils.rb

Class Method Summary collapse

Class Method Details

.uncolor(string) ⇒ Object



17
18
19
20
# File 'lib/rainbow/string_utils.rb', line 17

def self.uncolor(string)
  # See http://www.commandlinefu.com/commands/view/3584/remove-color-codes-special-characters-with-sed
  string.gsub(/\e\[[0-9;]*m/, '')
end

.wrap_with_sgr(string, codes) ⇒ Object



5
6
7
8
9
10
11
12
13
14
15
# File 'lib/rainbow/string_utils.rb', line 5

def self.wrap_with_sgr(string, codes)
  return string if codes.empty?

  seq = "\e[" + codes.join(";") + "m"

  string = string.sub(/^(\e\[([\d;]+)m)*/) { |m| m + seq }

  return string if string.end_with? "\e[0m"

  string + "\e[0m"
end