class Eskimo::ASCII::Truncate

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

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

Attributes

maxlen[R]

Public Class Methods

new(reserve: 0, width: Constants::SCREEN_COLUMNS, &children) click to toggle source
Calls superclass method Eskimo::ASCII::Component::new
# 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

Public Instance Methods

render(**) click to toggle source
Calls superclass method Eskimo::ASCII::Component#render
# 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