Class: Text

Inherits:
Object show all
Defined in:
lib/selecta.rb

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(components) ⇒ Text

Returns a new instance of Text.



665
666
667
# File 'lib/selecta.rb', line 665

def initialize(components)
  @components = components
end

Instance Attribute Details

#componentsObject (readonly)

Returns the value of attribute components.



654
655
656
# File 'lib/selecta.rb', line 654

def components
  @components
end

Class Method Details

.[](*args) ⇒ Object



656
657
658
659
660
661
662
663
# File 'lib/selecta.rb', line 656

def self.[](*args)
  if args.length == 1 && args[0].is_a?(Text)
    # When called as `Text[some_text]`, just return the existing text.
    args[0]
  else
    new(args)
  end
end

Instance Method Details

#+(other) ⇒ Object



673
674
675
# File 'lib/selecta.rb', line 673

def +(other)
  Text[*(components + other.components)]
end

#==(other) ⇒ Object



669
670
671
# File 'lib/selecta.rb', line 669

def ==(other)
  components == other.components
end

#truncate_to_width(width) ⇒ Object



677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
# File 'lib/selecta.rb', line 677

def truncate_to_width(width)
  chars_remaining = width

  # Truncate each string to the number of characters left within our
  # allowed width. Leave anything else alone. This may result in empty
  # strings and unused ANSI control codes in the output, but that's fine.
  components = self.components.map do |component|
    if component.is_a?(String)
      component = component[0, chars_remaining]
      chars_remaining -= component.length
    end
    component
  end

  Text.new(components)
end