class WordWrap::Wrapper

def fit

def fit
  lines = []
  next_line = ""
  @text.lines do |line|
    line.chomp! "\n"
    if line.length == 0
      if next_line.length > 0
        lines.push next_line
        next_line = ""
      end
      lines.push ""
    end
    words = line.split " "
    words.each do |word|
      word.chomp! "\n"
      if next_line.length + word.length < @width
        if next_line.length > 0
          next_line << " " << word
        else
          next_line = word
        end
      else
        if word.length >= @width
          lines.push next_line unless next_line == ""
          lines.push word
          next_line = ""
        else
          lines.push next_line
          next_line = word
        end
      end
    end
  end
  lines.push next_line
  if next_line.length <= 0
    lines.join("\n")
  else
    lines.join("\n") + "\n"
  end
end