Module: Inkjet::String

Defined in:
lib/inkjet/string.rb

Instance Method Summary collapse

Dynamic Method Handling

This class handles dynamic methods through the method_missing method

#method_missing(meth, *args) ⇒ Object



4
5
6
7
8
9
10
# File 'lib/inkjet/string.rb', line 4

def method_missing(meth, *args)
  if meth.match(/(#{%w(bold dim underline blink invert hidden).join('|')})(!?)/)
    send "apply_inkjet_code#{$2}", "Inkjet::#{meth.capitalize}".constantize, *args
  else
    super
  end
end

Instance Method Details

#apply_inkjet_code(code, wrap = false) ⇒ Object



20
21
22
# File 'lib/inkjet/string.rb', line 20

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

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



24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
# File 'lib/inkjet/string.rb', line 24

def apply_inkjet_code!(code, wrap=false)
  if inkjet_strings.length > 0
    replace(inkjet_strings.map do |istr|
      if wrap # apply the code to every chunk within the string, formatted or unformatted
        
        (istr[0].empty? ? "" : apply_inkjet_codes_to_substring([code], istr[0])) +
        apply_inkjet_codes_to_substring(istr[1].push(code), istr[2]) +
        (istr[3].empty? ? "" : apply_inkjet_codes_to_substring([code], istr[3]))
      
      elsif !inkjet_codes.empty? && istr[1] == inkjet_codes # apply the code only to the formatted chunks of the 'primary string'
        
        istr[0] + apply_inkjet_codes_to_substring(istr[1].push(code), istr[2]) + istr[3]
      
      else # apply the code only to the unformatted chunks of the 'primary string' leaving any formatted substrings untouched
        
        (istr[0].empty? ? "" : apply_inkjet_codes_to_substring([code], istr[0])) +
        apply_inkjet_codes_to_substring(istr[1], istr[2]) +
        (istr[3].empty? ? "" : apply_inkjet_codes_to_substring([code], istr[3]))
      
      end
    end.join(""))
  else # wrap 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



50
51
52
53
54
# File 'lib/inkjet/string.rb', line 50

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



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

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

#cleanObject



12
13
14
# File 'lib/inkjet/string.rb', line 12

def clean
  clone.clean!
end

#clean!Object



16
17
18
# File 'lib/inkjet/string.rb', line 16

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