Parent

Included Modules

Class/Module Index [+]

Quicksearch

HighLine::Table

Represent a columnar layout of items with wrapping and flexible layout.

Constants

Width

Attributes

items[R]

Public Instance Methods

color() click to toggle source
# File lib/rhc/highline_extensions.rb, line 309
def color
  opts[:color]
end

Protected Instance Methods

align() click to toggle source
# File lib/rhc/highline_extensions.rb, line 320
def align
  opts[:align] || []
end
allocate_widths_for(available) click to toggle source
# File lib/rhc/highline_extensions.rb, line 373
def allocate_widths_for(available)
  available -= (columns-1) * joiner.length + indent.length
  return column_widths.map{ |w| w.max } if available >= column_widths.inject(0){ |sum, w| sum + w.max } || column_widths.inject(0){ |sum, w| sum + w.min } > available

  fair = available / columns
  column_widths.each do |w|
    if w.set > 0
      available -= w.set
      next
    end
    w.set = if w.max <= fair
        available -= w.max
        w.max
      else
        0
      end
  end

  remaining = column_widths.inject(0) do |sum, w|
      if w.set == 0
        sum += w.max
        available -= w.min
      end
      sum
    end
  fair = available.to_f / remaining.to_f

  column_widths.
    each do |w|
      if w.set == 0
        alloc = (w.max * fair).to_i
        overflow = alloc + w.min - w.max
        if overflow > 0
          alloc -= overflow
        end
        available -= alloc
        w.set = alloc + w.min
      end
    end.
    each do |w|
      next unless available > 0
      gap = w.max - w.set
      if gap > 0
        left = [gap,available].min
        available -= left
        w.set += left
      end
    end.map(&:set)
end
column_widths() click to toggle source
# File lib/rhc/highline_extensions.rb, line 355
def column_widths
  @column_widths ||= begin
    widths = Array.new(columns){ Width.new(0,0,0) }
    (source_rows + headers).each do |row|
      row.each_with_index do |col, i|
        w = widths[i]
        s = col.strip_ansi
        word_length = s.scan(/\b\S+/).inject(0){ |l, word| l = word.length if l <= word.length; l }
        w.min = word_length unless w.min > word_length
        w.max = s.length unless w.max > s.length
      end
    end
    widths
  end
end
columns() click to toggle source
# File lib/rhc/highline_extensions.rb, line 351
def columns
  @columns ||= source_rows.map(&:length).max || 0
end
header_rows() click to toggle source
# File lib/rhc/highline_extensions.rb, line 444
def header_rows
  @header_rows ||= begin
    headers << widths.map{ |w| '-' * w } if headers.present?
    headers
  end
end
headers() click to toggle source
# File lib/rhc/highline_extensions.rb, line 347
def headers
  @headers ||= opts[:header] ? [Array(opts[:header])] : []
end
heading() click to toggle source
# File lib/rhc/highline_extensions.rb, line 329
def heading
  @heading ||= opts[:heading] ? HighLine::Header.new(opts[:heading], max_width, indent) : nil
end
indent() click to toggle source
# File lib/rhc/highline_extensions.rb, line 326
def indent
  opts[:indent] || ''
end
joiner() click to toggle source
# File lib/rhc/highline_extensions.rb, line 323
def joiner
  opts[:join] || ' '
end
max_width() click to toggle source
# File lib/rhc/highline_extensions.rb, line 440
def max_width
  @max_width ||= opts[:width].is_a?(Array) ? opts[:width].first : (opts[:width] ? opts[:width] : 0)
end
opts() click to toggle source
# File lib/rhc/highline_extensions.rb, line 316
def opts
  @options
end
rows() click to toggle source
# File lib/rhc/highline_extensions.rb, line 451
def rows
  @rows ||= begin
    body = (header_rows + source_rows).inject([]) do |a,row|
      row = row.zip(widths).map{ |column,w| w && w > 0 ? column.textwrap_ansi(w, false) : [column] }
      (row.map(&:length).max || 0).times do |i|
        s = []
        row.each_with_index do |lines, j|
          cell = lines[i]
          l = cell ? cell.strip_ansi.length : 0
          s <<
              if align[j] == :right
                "#{' '*(widths[j]-l) if l < widths[j]}#{cell}"
              else
                "#{cell}#{' '*(widths[j]-l) if l < widths[j]}"
              end
        end
        a << "#{indent}#{s.join(joiner).rstrip}"
      end
      a
    end

    body = heading.to_a.concat(body) if heading
    body
  end
end
source_rows() click to toggle source
# File lib/rhc/highline_extensions.rb, line 333
def source_rows
  @source_rows ||= begin
    (@mapper ? (items.map &@mapper) : items).each do |row|
      row.map! do |col|
        case col
        when Array then col.join("\n")
        when String then col
        else col.to_s
        end
      end
    end
  end
end
widths() click to toggle source
# File lib/rhc/highline_extensions.rb, line 423
def widths
  @widths ||= begin
    case w = opts[:width]
    when Array
      column_widths.zip(w[1..-1]).each do |width, col|
        width.set = col || 0
        width.max = width.set if width.set > width.max
      end
      allocate_widths_for(w.first || 0)
    when Integer
      allocate_widths_for(w)
    else
      column_widths.map{ |w| w.max }
    end
  end
end

Public Class Methods

new(items=nil,options={},&mapper) click to toggle source
# File lib/rhc/highline_extensions.rb, line 305
def initialize(items=nil,options={},&mapper)
  @items, @options, @mapper = items, options, mapper
end

[Validate]

Generated with the Darkfish Rdoc Generator 2.