Class: Inkjet::Substring

Inherits:
Object
  • Object
show all
Defined in:
lib/inkjet/string.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Instance Attribute Details

#codesObject

Returns the value of attribute codes.



3
4
5
# File 'lib/inkjet/string.rb', line 3

def codes
  @codes
end

#postObject

Returns the value of attribute post.



3
4
5
# File 'lib/inkjet/string.rb', line 3

def post
  @post
end

#preObject

Returns the value of attribute pre.



3
4
5
# File 'lib/inkjet/string.rb', line 3

def pre
  @pre
end

#stringObject

Returns the value of attribute string.



3
4
5
# File 'lib/inkjet/string.rb', line 3

def string
  @string
end

Instance Method Details

#format(super_code = nil) ⇒ Object

Format the string by applying the proper codes to various chunks of the string.

The primary string is formatted with this object’s @codes

If a super_code is provided, it is used to format the superstring (pre/post) chunks, otherwise they are left unformatted.



34
35
36
37
38
# File 'lib/inkjet/string.rb', line 34

def format(super_code=nil)
  format_chunk(pre, super_code) +
  format_chunk(string, codes) +
  format_chunk(post, super_code)
end

#format_chunk(chunk, codes) ⇒ Object

Formats a chunk of the substring

If no codes are provided, or the chunk is empty, it si not formatted



22
23
24
25
26
# File 'lib/inkjet/string.rb', line 22

def format_chunk(chunk, codes)
  codes = [codes].flatten
  return chunk if !formattable?(chunk) || (codes.nil? || codes.empty?)
  "\e[#{codes.join(";")}m#{chunk}\e[#{Inkjet::Close}m"
end

#formattable?(chunk) ⇒ Boolean

Returns:

  • (Boolean)


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

def formattable?(chunk)
  !chunk.nil? && !chunk.empty? #&& !chunk.match(/\A[\r\n]+\Z/)
end

#push(code) ⇒ Object



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

def push(code)
  codes.push(code)
  self
end

#unshift(code) ⇒ Object



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

def unshift(code)
  codes.unshift(code)
  self
end