Module: Inkjet::String

Defined in:
lib/inkjet/string.rb

Instance Method Summary collapse

Instance Method Details

#apply_inkjet_code(code, wrap = false) ⇒ Object



88
89
90
# File 'lib/inkjet/string.rb', line 88

def apply_inkjet_code(code, wrap=false)
  clone.apply_inkjet_code!(code, wrap)
end

#apply_inkjet_code!(code, wrap = false) ⇒ Object

Apply a formatting code to the appropriate chunks in the string.

If forcing a wrap, all chunks within the string get formatted with the new code, regardless of whether they were previously formatted

If there are codes applied to the first chunk of the string, apply the new code to all previously formatted chunks with matching codes

If the first chunk of the string is unformatted, apply the new code to all unformatted chunks

If the string is completely unformatted, enclose self within the provided code



70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
# File 'lib/inkjet/string.rb', line 70

def apply_inkjet_code!(code, wrap=false)
  if inkjet_substrings.length > 0
    
    replace(inkjet_substrings.map do |substr|
      if wrap # apply the code to every chunk within the string, formatted or unformatted
        substr.push(code).format(code)
      elsif !inkjet_codes.empty? && substr.codes == inkjet_codes # apply the code only to formatted chunks with matching codes
        substr.push(code).format
      else # apply the code only to the unformatted chunks
        substr.format(code)
      end
    end.join)

  else # enclose the entire unformatted string w/ the code
    replace("\e[#{code}m#{self}\e[#{Inkjet::Close}m")
  end
end

#apply_inkjet_codes(codes, wrap = false) ⇒ Object



92
93
94
95
96
# File 'lib/inkjet/string.rb', line 92

def apply_inkjet_codes(codes, wrap=false)
  cloned = clone
  codes.each { |code| cloned.apply_inkjet_code!(code, wrap) }
  cloned
end

#apply_inkjet_codes!(codes, wrap = false) ⇒ Object



98
99
100
101
# File 'lib/inkjet/string.rb', line 98

def apply_inkjet_codes!(codes, wrap=false)
  codes.each { |code| apply_inkjet_code!(code, wrap) }
  self
end

#cleanObject



57
58
59
# File 'lib/inkjet/string.rb', line 57

def clean
  clone.clean!
end

#clean!Object

Clean out all escape codes from the formatted string



53
54
55
# File 'lib/inkjet/string.rb', line 53

def clean!
  gsub!(/\e\[[\d;]+m/, '')
end