Class: Helium::Console::Formatters::Overflow::Wrap

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

Instance Method Summary collapse

Constructor Details

#initialize(max_width:) ⇒ Wrap

Returns a new instance of Wrap.



8
9
10
# File 'lib/helium/console/formatters/overflow/wrap.rb', line 8

def initialize(max_width:)
  @max_width = max_width
end

Instance Method Details

#call(string) ⇒ Object



12
13
14
15
16
17
18
19
20
21
# File 'lib/helium/console/formatters/overflow/wrap.rb', line 12

def call(string)
  return string unless @max_width

  result = string.lines.flat_map do |line|
    line.chomp.chars.each_slice(@max_width).map(&:join)
  end
  result = result.join("\n")
  result += "\n" if string.end_with?("\n")
  result
end