# File lib/rhc/core_ext.rb, line 56
  def wrap_line(line)
    wrapped_line = []
    i = chars_in_line = 0
    word = []

    while i < line.length
      # we have to give a length to the index because ruby 1.8 returns the
      # byte code when using a single fixednum index
      c = line[i, 1]
      color_code = nil
      # escape character probably means color code, let's check
      if c == "\e"
        color_code = line[i..i+6].match(/\e\[\d{1,2}m/)
        if color_code
          # first the existing word buffer then the color code
          wrapped_line << word.join.wrap(@wrap_at) << color_code[0]
          word.clear

          i += color_code[0].length
        end
      end

      # visible character
      if !color_code
        chars_in_line += 1
        word << c

        # time to wrap the line?
        if chars_in_line == @wrap_at
          if c == ' ' or line[i+1, 1] == ' ' or word.length == @wrap_at
            wrapped_line << word.join
            word.clear
          end

          wrapped_line[-1].rstrip!
          wrapped_line << "\n"

          # consume any spaces at the begining of the next line
          word = word.join.lstrip.split(//)
          chars_in_line = word.length

          if line[i+1, 1] == ' '
            i += 1 while line[i+1, 1] == ' '
          end

        else
          if c == ' '
            wrapped_line << word.join
            word.clear
          end
        end

        i += 1
      end
    end

    wrapped_line << word.join
    wrapped_line.join
  end