Class: Helium::Console::Formatters::MaxLines

Inherits:
Object
  • Object
show all
Defined in:
lib/helium/console/formatters/max_lines.rb

Constant Summary collapse

ELLIPSES =
'..."'

Instance Method Summary collapse

Constructor Details

#initialize(max_lines:, max_width:, ellipses:) ⇒ MaxLines

Returns a new instance of MaxLines.



9
10
11
12
13
# File 'lib/helium/console/formatters/max_lines.rb', line 9

def initialize(max_lines:, max_width:, ellipses:)
  @max_lines = max_lines
  @max_width = max_width
  @ellipses = ellipses
end

Instance Method Details

#call(string) ⇒ Object



15
16
17
18
19
20
21
22
# File 'lib/helium/console/formatters/max_lines.rb', line 15

def call(string)
  return string if !@max_lines || string.lines.count <= @max_lines

  lines = string.lines.first(@max_lines)
  last_line = lines.pop
  lines << last_line.chars.first(@max_width - @ellipses.length).join + @ellipses
  lines.join
end