Module: GenomerPluginSummary::Format

Included in:
Contigs, Gaps, Genome, Sequences
Defined in:
lib/genomer-plugin-summary/format.rb

Constant Summary collapse

DEFAULTS =
{
  :justification => [],
  :width         => {},
  :format        => {}
}

Instance Method Summary collapse

Instance Method Details

#create_cells(data, opts) ⇒ Object



20
21
22
23
24
25
26
27
28
29
30
31
32
33
# File 'lib/genomer-plugin-summary/format.rb', line 20

def create_cells(data,opts)
  data.map do |row|
    if row == :separator
      :separator
    else
      row.each_with_index.map do |cell,index|
        format_cell(cell,
                    opts[:width][index],
                    opts[:justification][index],
                    opts[:format][index])
      end
    end
  end
end

#csv(data, opts) ⇒ Object



51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
# File 'lib/genomer-plugin-summary/format.rb', line 51

def csv(data,opts)
  opts[:width] = {}
  opts[:justification] = {}

  cells = create_cells(data,opts)

  cells.unshift opts[:headers] if opts[:headers]

  cells.compact.
    rejecting{|i| i == :separator}.
    mapping{|i| i.join(',')}.
    mapping{|i| i.gsub(' ','_')}.
    mapping{|i| i.gsub(/[()]/,'')}.
    mapping{|i| i.downcase}.
    to_a. join("\n") + "\n"
end

#format_cell(cell, width, justification, format = nil) ⇒ Object



35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
# File 'lib/genomer-plugin-summary/format.rb', line 35

def format_cell(cell,width,justification,format = nil)
  formatted = case format
              when String  then sprintf(format,cell)
              when Proc    then format.call(cell).to_s
              when nil     then cell.to_s
              end

  return formatted if width.nil?

  case justification
  when :right  then formatted.rjust(width)
  when :center then formatted.center(width)
  else              formatted.ljust(width)
  end
end

#pretty(data, opts) ⇒ Object



68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
# File 'lib/genomer-plugin-summary/format.rb', line 68

def pretty(data,opts)
  cells = create_cells(data,opts)

  if opts[:headers]
    cells.unshift :separator
    cells.unshift(opts[:headers].each_with_index.map do |header,index|
      width = opts[:width][index] || cells.mapping{|c| c[index].length }.max
      format_cell(header, width, :center)
    end)
  end

  table = Terminal::Table.new do |t|
    cells.each{|c| t << c}
  end
  opts[:justification].each{|(k,v)| table.align_column k, v }
  table.title ||= opts[:title]
  table.to_s + "\n"
end

#table(data, opts = {}) ⇒ Object



12
13
14
15
16
17
18
# File 'lib/genomer-plugin-summary/format.rb', line 12

def table(data,opts = {})
  opts = DEFAULTS.merge opts
  case opts[:output]
  when 'csv' then csv(data,opts)
  else            pretty(data,opts)
  end
end