class RbFind::Table

Attributes

heads[R]

Public Class Methods

new(*heads) click to toggle source
# File lib/rbfind/table.rb, line 10
def initialize *heads
  heads.flatten!
  @heads = heads.map { |h|
    a = case h
      when />\z/  then +1
      when /\^\z/ then  0
      when /<?\z/ then -1
      end
    [ $`, a]
  }
  @rows = []
end

Public Instance Methods

add(*row) click to toggle source
# File lib/rbfind/table.rb, line 30
def add *row
  row.flatten!
  n = @heads.size
  row.map! { |r| break if n.zero? ; n -= 1 ; r.to_s }
  @rows.push row
end
empty?() click to toggle source
# File lib/rbfind/table.rb, line 41
def empty?
  @rows.empty?
end
initialize_copy(oth) click to toggle source
# File lib/rbfind/table.rb, line 25
def initialize_copy oth
  @heads = oth.heads
  @rows = []
end
make_html(table: nil, row: nil) click to toggle source
# File lib/rbfind/table.rb, line 73
def make_html table: nil, row: nil
  @html = ""
  tag :table, table, nl: 2 do
    tag :tr, row, nl: 1 do
      @heads.each { |(h,a)|
        tag :td, h.downcase, align: a do @html << h end
      }
    end
    @rows.each { |r|
      tag :tr, table, nl: 1 do
        (@heads.zip r).each { |(g,a),c|
          tag :td, g.downcase, align: a do @html << c end
        }
      end
    }
  end
  @html
ensure
  @html = nil
end
make_lines(head: false) { |l| ... } click to toggle source
# File lib/rbfind/table.rb, line 53
def make_lines head: false
  rs = @rows
  rs.unshift @heads.map { |(h,a)| h } if head
  w = calc_widths
  rs.each { |r|
    j = (w.zip @heads, r).map { |v,(_,a),c|
      c ||= ""
      case a
      when -1 then c.ljust  v
      when  0 then c.center v
      when +1 then c.rjust  v
      end
    }
    l = j.join " "
    l.rstrip!
    yield l
  }
  nil
end
output(head: false, ifempty: nil) click to toggle source
# File lib/rbfind/table.rb, line 45
def output head: false, ifempty: nil
  if empty? and ifempty then
    puts ifempty
    return
  end
  make_lines head: head do |l| puts l end
end
sort_by!(*nums) click to toggle source
# File lib/rbfind/table.rb, line 37
def sort_by! *nums
  @rows.sort_by! { |x| nums.map { |i| x[i] } }
end

Private Instance Methods

calc_widths() click to toggle source
# File lib/rbfind/table.rb, line 96
def calc_widths
  w = @heads.map { 0 }
  @rows.each { |r|
    w = (w.zip r).map { |i,c|
      if c then
        j = c.length
        i = j if j > i
      end
      i
    }
  }
  w
end
html_align(a) click to toggle source
# File lib/rbfind/table.rb, line 121
def html_align a
  case a
  when -1 then "left"
  when  0 then "center"
  when +1 then "right"
  end
end
tag(name, cls, nl: 0, align: nil) { || ... } click to toggle source
# File lib/rbfind/table.rb, line 110
def tag name, cls, nl: 0, align: nil
  @html << "<#{name}"
  @html << " style=\"text-align: " << (html_align align) << ";\"" if align
  @html << " class=\"" << cls << "\"" if cls
  @html << ">"
  @html << $/ if nl > 1
  yield
  @html << "</#{name}>"
  @html << $/ if nl > 0
end