Class: LeapCli::Commands::NodeTable

Inherits:
Object
  • Object
show all
Includes:
CommandLineReporter
Defined in:
lib/leap_cli/commands/list.rb

Overview

might be handy: HighLine::SystemExtensions.terminal_size.first

Instance Method Summary collapse

Constructor Details

#initialize(node_list, colors) ⇒ NodeTable

Returns a new instance of NodeTable.



94
95
96
97
# File 'lib/leap_cli/commands/list.rb', line 94

def initialize(node_list, colors)
  @node_list = node_list
  @colors = colors
end

Instance Method Details

#runObject



98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
# File 'lib/leap_cli/commands/list.rb', line 98

def run
  rows = @node_list.keys.sort.collect do |node_name|
    [node_name, @node_list[node_name].services.sort.join(', '), @node_list[node_name].tags.sort.join(', ')]
  end
  unless rows.any?
    puts Paint["no results", :red]
    puts
    return
  end
  padding = 2
  max_node_width    = [20, (rows.map{|i|i[0]} + ["NODES"]   ).inject(0) {|max,i| [i.size,max].max}].max
  max_service_width = (rows.map{|i|i[1]} + ["SERVICES"]).inject(0) {|max,i| [i.size+padding+padding,max].max}
  max_tag_width     = (rows.map{|i|i[2]} + ["TAGS"]    ).inject(0) {|max,i| [i.size,max].max}
  table :border => false do
    row :color => @colors[0]  do
      column "NODES", :align => 'right', :width => max_node_width
      column "SERVICES", :width => max_service_width, :padding => 2
      column "TAGS", :width => max_tag_width
    end
    rows.each do |r|
      row :color => @colors[1] do
        column r[0]
        column r[1]
        column r[2]
      end
    end
  end
  vertical_spacing
end