Class: Eskimo::Components::Truncate
- Inherits:
-
Eskimo::Component
- Object
- Eskimo::Component
- Eskimo::Components::Truncate
- 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
Instance Attribute Summary collapse
-
#maxlen ⇒ Object
readonly
Returns the value of attribute maxlen.
Instance Method Summary collapse
-
#initialize(reserve: 0, width: Constants::SCREEN_COLUMNS, &children) ⇒ Truncate
constructor
A new instance of Truncate.
- #render ⇒ Object
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
#maxlen ⇒ Object (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
#render ⇒ Object
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 |