Class: Eskimo::Components::Truncate

Inherits:
Eskimo::Component show all
Defined in:
lib/eskimo/components/truncate.rb

Overview

Truncate text from the beginning if it exceeds a certain width.

Truncate.new(width: 3) do
  "foo bar"
end
# => "... bar"

Direct Known Subclasses

TruncateRear

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(reserve: 0, width: Constants::SCREEN_COLUMNS, &children) ⇒ Truncate

Returns a new instance of Truncate



14
15
16
17
18
# File 'lib/eskimo/components/truncate.rb', line 14

def initialize(reserve: 0, width: Constants::SCREEN_COLUMNS, &children)
  @maxlen = [0, width - reserve].max

  super
end

Instance Attribute Details

#maxlenObject (readonly)

Returns the value of attribute maxlen



12
13
14
# File 'lib/eskimo/components/truncate.rb', line 12

def maxlen
  @maxlen
end

Instance Method Details

#renderObject



20
21
22
23
24
25
26
27
28
# File 'lib/eskimo/components/truncate.rb', line 20

def render(**)
  text = super

  if text.length >= maxlen
    '...' + text[text.length - maxlen - 1 .. -1]
  else
    text
  end
end