Class: WhirledPeas::Utils::FormattedString

Inherits:
Object
  • Object
show all
Defined in:
lib/whirled_peas/utils/formatted_string.rb

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(raw, formatting = nil) ⇒ FormattedString

Returns a new instance of FormattedString.



12
13
14
15
# File 'lib/whirled_peas/utils/formatted_string.rb', line 12

def initialize(raw, formatting=nil)
  @raw = raw
  @formatting = [*formatting]
end

Instance Attribute Details

#formattingObject (readonly)

Returns the value of attribute formatting.



10
11
12
# File 'lib/whirled_peas/utils/formatted_string.rb', line 10

def formatting
  @formatting
end

Class Method Details

.blankObject



6
7
8
# File 'lib/whirled_peas/utils/formatted_string.rb', line 6

def self.blank
  new('')
end

Instance Method Details

#==(other) ⇒ Object



33
34
35
36
37
38
39
40
41
42
# File 'lib/whirled_peas/utils/formatted_string.rb', line 33

def ==(other)
  case other
  when self.class
    hash == other.hash
  when String
    formatting.empty? && raw == other
  else
    false
  end
end

#blank?Boolean

Returns:

  • (Boolean)


25
26
27
# File 'lib/whirled_peas/utils/formatted_string.rb', line 25

def blank?
  raw.empty?
end

#each_char(&block) ⇒ Object



21
22
23
# File 'lib/whirled_peas/utils/formatted_string.rb', line 21

def each_char(&block)
  raw.chars.each(&block)
end

#hashObject



29
30
31
# File 'lib/whirled_peas/utils/formatted_string.rb', line 29

def hash
  [raw, formatting].hash
end

#inspectObject



53
54
55
56
57
58
59
# File 'lib/whirled_peas/utils/formatted_string.rb', line 53

def inspect
  if raw.empty? || formatting.length == 0
    raw.inspect
  else
    "<#{formatting.join(', ')}>#{raw}<0>".inspect
  end
end

#lengthObject



17
18
19
# File 'lib/whirled_peas/utils/formatted_string.rb', line 17

def length
  raw.length
end

#to_sObject



44
45
46
47
48
49
50
51
# File 'lib/whirled_peas/utils/formatted_string.rb', line 44

def to_s
  if raw.empty? || formatting.length == 0
    raw
  else
    start_formatting = formatting.map { |code| Ansi.esc_seq(code) }.join
    "#{start_formatting}#{raw}#{Ansi.clear}"
  end
end