class WhirledPeas::Utils::FormattedString

Attributes

formatting[R]
raw[R]

Public Class Methods

blank() click to toggle source
# File lib/whirled_peas/utils/formatted_string.rb, line 6
def self.blank
  new('')
end
new(raw, formatting=nil) click to toggle source
# File lib/whirled_peas/utils/formatted_string.rb, line 12
def initialize(raw, formatting=nil)
  @raw = raw
  @formatting = [*formatting]
end

Public Instance Methods

==(other) click to toggle source
# 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?() click to toggle source
# File lib/whirled_peas/utils/formatted_string.rb, line 25
def blank?
  raw.empty?
end
each_char(&block) click to toggle source
# File lib/whirled_peas/utils/formatted_string.rb, line 21
def each_char(&block)
  raw.chars.each(&block)
end
hash() click to toggle source
# File lib/whirled_peas/utils/formatted_string.rb, line 29
def hash
  [raw, formatting].hash
end
inspect() click to toggle source
# 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
length() click to toggle source
# File lib/whirled_peas/utils/formatted_string.rb, line 17
def length
  raw.length
end
to_s() click to toggle source
# 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