Method: Howzit::StringUtils#split_line

Defined in:
lib/howzit/stringutils.rb

#split_line(width, indent = '') ⇒ Object

Splits a line at nearest word break

Parameters:

  • width (Integer)

    The width of the first segment

  • indent (String) (defaults to: '')

    The indent string



187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
# File 'lib/howzit/stringutils.rb', line 187

def split_line(width, indent = '')
  line = dup
  at = line.index(/\s/)
  last_at = at

  while !at.nil? && at < width
    last_at = at
    at = line.index(/\s/, last_at + 1)
  end

  if last_at.nil?
    [indent + line[0, width], line[width, line.length]]
  else
    [indent + line[0, last_at], line[last_at + 1, line.length]]
  end
end