Class: Rainbow::StringUtils

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

Class Method Summary collapse

Class Method Details

.wrap_with_sgr(string, codes) ⇒ Object



4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
# File 'lib/rainbow/string_utils.rb', line 4

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

  seq = "\e[" + codes.join(";") + "m"
  match = string.match(/^(\e\[([\d;]+)m)*/)

  if match
    seq_pos = match.end(0)
    string = string[0...seq_pos] + seq + string[seq_pos..-1]
  else
    string = seq + string
  end

  string = string + "\e[0m" unless string =~ /\e\[0m$/

  string
end