class HighLine::Header

Protected Instance Methods

rows() click to toggle source
# File lib/rhc/highline_extensions.rb, line 245
def rows
  @rows ||= begin
    if !width || width == 0
      [text.is_a?(Array) ? text.join(' ') : text]

    elsif text.is_a? Array
      widths = text.map{ |s| s.strip_ansi.length }
      chars, join, indented = 0, 1, (indent || '').length
      narrow = width - indented
      text.zip(widths).inject([]) do |rows, (section, w)|
        if rows.empty?
          if w > width
            rows.concat(section.textwrap_ansi(width))
          else
            rows << section.dup
            chars += w
          end
        else
          if w + chars + join > narrow
            rows.concat(section.textwrap_ansi(narrow).map{ |s| "#{indent}#{s}" })
            chars = 0
          elsif chars == 0
            rows << "#{indent}#{section}"
            chars += w + indented
          else
            rows[-1] << " #{section}"
            chars += w + join
          end
        end
        rows
      end
    else
      text.textwrap_ansi(width)
    end
  end.tap do |rows|
    rows << '-' * (rows.map{ |s| s.strip_ansi.length }.max || 0)
  end
end