Class: CF::Table::CLI
- Inherits:
-
Object
- Object
- CF::Table::CLI
- Defined in:
- lib/cf/table/cli.rb
Class Method Summary collapse
- .columns ⇒ Object
- .default_columns ⇒ Object
- .entities ⇒ Object
- .entity_keys ⇒ Object
- .keys_table ⇒ Object
- .rows ⇒ Object
- .run ⇒ Object
- .table ⇒ Object
- .term_cols ⇒ Object
Class Method Details
.columns ⇒ Object
33 34 35 |
# File 'lib/cf/table/cli.rb', line 33 def self.columns @columns ||= ARGV.empty? ? default_columns : ARGV end |
.default_columns ⇒ Object
37 38 39 |
# File 'lib/cf/table/cli.rb', line 37 def self.default_columns entity_keys.select { |k| !k.match(/url$/) } end |
.entities ⇒ Object
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_keys ⇒ Object
41 42 43 |
# File 'lib/cf/table/cli.rb', line 41 def self.entity_keys @entity_keys || entities.first.keys end |
.keys_table ⇒ Object
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 |
.rows ⇒ Object
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 |
.run ⇒ Object
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 |
.table ⇒ Object
16 17 18 |
# File 'lib/cf/table/cli.rb', line 16 def self.table @table ||= Terminal::Table.new headings: columns, rows: rows end |
.term_cols ⇒ Object
51 52 53 |
# File 'lib/cf/table/cli.rb', line 51 def self.term_cols `tput cols`.strip.to_i end |