Class: Eskimo::ASCII::Truncate

Inherits:
Component show all
Defined in:
lib/eskimo/ascii/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.



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

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.



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

def maxlen
  @maxlen
end

Instance Method Details

#renderObject



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

def render(**)
  text = super

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