Class: Eskimo::ASCII::Truncate
- Defined in:
- gems/eskimo-ascii/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
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
13 14 15 16 17 |
# File 'gems/eskimo-ascii/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
#maxlen ⇒ Object (readonly)
Returns the value of attribute maxlen
11 12 13 |
# File 'gems/eskimo-ascii/lib/eskimo/ascii/components/truncate.rb', line 11 def maxlen @maxlen end |
Instance Method Details
#render ⇒ Object
19 20 21 22 23 24 25 26 27 |
# File 'gems/eskimo-ascii/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 |