Class: CF::Table::CLI

Inherits:
Object
  • Object
show all
Defined in:
lib/cf/table/cli.rb

Class Method Summary collapse

Class Method Details

.columnsObject



33
34
35
# File 'lib/cf/table/cli.rb', line 33

def self.columns
  @columns ||= ARGV.empty? ? default_columns : ARGV
end

.default_columnsObject



37
38
39
# File 'lib/cf/table/cli.rb', line 37

def self.default_columns
  entity_keys.select { |k| !k.match(/url$/) }
end

.entitiesObject



45
46
47
48
49
# File 'lib/cf/table/cli.rb', line 45

def self.entities
  @entities ||= begin
    JSON.parse($stdin.read)['resources'].map { |entity| entity['entity'] }
  end
end

.entity_keysObject



41
42
43
# File 'lib/cf/table/cli.rb', line 41

def self.entity_keys
  @entity_keys || entities.first.keys
end

.keys_tableObject



20
21
22
23
# File 'lib/cf/table/cli.rb', line 20

def self.keys_table
  rows = (entity_keys + Array.new(5 - (entity_keys.size % 5))).each_slice(5)
  Terminal::Table.new rows: rows
end

.rowsObject



25
26
27
28
29
30
31
# File 'lib/cf/table/cli.rb', line 25

def self.rows
  entities.map do |entry|
    columns.map do |column|
      entry[column]
    end
  end
end

.runObject



7
8
9
10
11
12
13
14
# File 'lib/cf/table/cli.rb', line 7

def self.run
  if ARGV.empty? && table.send(:columns_width) > term_cols
    puts "Table too wide, please select some columns:"
    puts keys_table
  else
    puts table
  end
end

.tableObject



16
17
18
# File 'lib/cf/table/cli.rb', line 16

def self.table
  @table ||= Terminal::Table.new headings: columns, rows: rows
end

.term_colsObject



51
52
53
# File 'lib/cf/table/cli.rb', line 51

def self.term_cols
  `tput cols`.strip.to_i
end