Class: Eskimo::Components::Truncate

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

Overview

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

...bar

Direct Known Subclasses

TruncateRear

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

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

Returns a new instance of Truncate.



68
69
70
71
72
# File 'lib/eskimo/components.rb', line 68

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

  super(&children)
end

Instance Attribute Details

#maxlenObject (readonly)

Returns the value of attribute maxlen.



66
67
68
# File 'lib/eskimo/components.rb', line 66

def maxlen
  @maxlen
end

Instance Method Details

#renderObject



74
75
76
77
78
79
80
81
82
# File 'lib/eskimo/components.rb', line 74

def render(**)
  text = super

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