Class: Zillabyte::Helpers::TableOutputBuilder

Inherits:
Object
  • Object
show all
Defined in:
lib/zillabyte/cli/helpers/table_output_builder.rb

Class Method Summary collapse

Class Method Details

.build_json_table(headings, rows) ⇒ Object



11
12
13
14
15
16
17
18
19
# File 'lib/zillabyte/cli/helpers/table_output_builder.rb', line 11

def self.build_json_table(headings, rows)
  out = []
  rows.map { |row|
    h = {}
    row.each_with_index.map{|x,i| h[headings[i]] = x}
    out << h
  }
  return out.to_json
end

.build_table(headings, rows, format = nil) ⇒ Object



3
4
5
6
7
8
9
# File 'lib/zillabyte/cli/helpers/table_output_builder.rb', line 3

def self.build_table(headings, rows, format = nil)
  if format == "json"
    self.build_json_table(headings, rows)
  else
    self.build_terminal_table(headings, rows)
  end
end

.build_terminal_table(headings, rows) ⇒ Object



21
22
23
24
25
# File 'lib/zillabyte/cli/helpers/table_output_builder.rb', line 21

def self.build_terminal_table(headings, rows)
    require("terminal-table")

    table = Terminal::Table.new(:headings => headings, :rows => rows).to_s
end

.format_row_count(val) ⇒ Object



27
28
29
30
31
32
33
34
35
36
37
38
# File 'lib/zillabyte/cli/helpers/table_output_builder.rb', line 27

def self.format_row_count(val)

  val = val.to_i
  if val > 1000000000
    val = (val/1000000000).to_s + "B"
  elsif val > 1000000
    val = (val/1000000).to_s + "M"
  elsif val > 1000
    val = (val/1000).to_s + "K"
  end
  val
end