class HighLine

Some versions of highline get in an infinite loop when trying to wrap. Fixes BZ 866530.

Public Instance Methods

wrap(text) click to toggle source
# File lib/rhc/core_ext.rb, line 167
def wrap(text)
  wrapped_text = []
  lines = text.split(%r\r?\n/)
  lines.each_with_index do |line, i|
    wrapped_text << wrap_line(i == lines.length - 1 ? line : line.rstrip)
  end

  return wrapped_text.join("\n")
end
wrap_line(line) click to toggle source
# File lib/rhc/core_ext.rb, line 107
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(%r\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(%r/)
        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