diff options
| author | elijah <elijah@riseup.net> | 2016-07-09 02:48:27 -0700 | 
|---|---|---|
| committer | elijah <elijah@riseup.net> | 2016-08-23 13:35:13 -0700 | 
| commit | 760aa6e1b0d7dd1764387c05f638c886745c55e7 (patch) | |
| tree | f033c951641ec438309d8f8ac52465853144d69c /lib/leap_cli/commands | |
| parent | 8c207687e8dfa72f42f25cac7f46b99f895e4f57 (diff) | |
move console table into separate file
Diffstat (limited to 'lib/leap_cli/commands')
| -rw-r--r-- | lib/leap_cli/commands/list.rb | 108 | 
1 files changed, 31 insertions, 77 deletions
| diff --git a/lib/leap_cli/commands/list.rb b/lib/leap_cli/commands/list.rb index bfb07808..1b3efc27 100644 --- a/lib/leap_cli/commands/list.rb +++ b/lib/leap_cli/commands/list.rb @@ -13,33 +13,38 @@ module LeapCli; module Commands      c.flag 'print', :desc => 'What attributes to print (optional)'      c.switch 'disabled', :desc => 'Include disabled nodes in the list.', :negatable => false      c.action do |global_options,options,args| -      # don't rely on default manager(), because we want to pass custom options to load() -      manager = LeapCli::Config::Manager.new -      if global_options[:color] -        colors = [:cyan, nil] -      else -        colors = [nil, nil] -      end -      puts -      manager.load(:include_disabled => options['disabled'], :continue_on_error => true) -      if options['print'] -        print_node_properties(manager.filter(args), options['print']) -      else -        if args.any? -          NodeTable.new(manager.filter(args), colors).run -        else -          environment = LeapCli.leapfile.environment || '_all_' -          TagTable.new('SERVICES', manager.env(environment).services, colors).run -          TagTable.new('TAGS', manager.env(environment).tags, colors).run -          NodeTable.new(manager.filter(), colors).run -        end -      end +      do_list(global_options, options, args)      end    end    private -  def self.print_node_properties(nodes, properties) +  def do_list(global, options, args) +    require 'leap_cli/util/console_table' +    # don't rely on default manager(), because we want to pass custom options to load() +    manager = LeapCli::Config::Manager.new +    if global[:color] +      colors = [:cyan, nil] +    else +      colors = [nil, nil] +    end +    puts +    manager.load(:include_disabled => options['disabled'], :continue_on_error => true) +    if options['print'] +      print_node_properties(manager.filter(args), options['print']) +    else +      if args.any? +        NodeTable.new(manager.filter(args), colors).run +      else +        environment = LeapCli.leapfile.environment || '_all_' +        TagTable.new('SERVICES', manager.env(environment).services, colors).run +        TagTable.new('TAGS', manager.env(environment).tags, colors).run +        NodeTable.new(manager.filter(), colors).run +      end +    end +  end + +  def print_node_properties(nodes, properties)      properties = properties.split(',')      max_width = nodes.keys.inject(0) {|max,i| [i.size,max].max}      nodes.each_node do |node| @@ -60,58 +65,7 @@ module LeapCli; module Commands      puts    end -  class Table -    def table -      @rows = [] -      @row_options = [] -      @column_widths = [20] # first column at least 20 -      @column_options = [] -      @current_row = 0 -      @current_column = 0 -      yield -    end - -    def row(options=nil) -      @current_column = 0 -      @row_options[@current_row] ||= options -      yield -      @current_row += 1 -    end - -    def column(str, options=nil) -      @rows[@current_row] ||= [] -      @rows[@current_row][@current_column] = str -      @column_widths[@current_column] = [str.length, @column_widths[@current_column]||0].max -      @column_options[@current_column] ||= options -      @current_column += 1 -    end - -    def draw_table -      @rows.each_with_index do |row, i| -        color = (@row_options[i]||{})[:color] -        row.each_with_index do |column, j| -          align = (@column_options[j]||{})[:align] || "left" -          width = @column_widths[j] -          if color -            str = LeapCli.logger.colorize(column, color) -            extra_width = str.length - column.length -          else -            str = column -            extra_width = 0 -          end -          if align == "right" -            printf "  %#{width+extra_width}s" % str -          else -            printf "  %-#{width+extra_width}s" % str -          end -        end -        puts -      end -      puts -    end -  end - -  class TagTable < Table +  class TagTable < LeapCli::Util::ConsoleTable      def initialize(heading, tag_list, colors)        @heading = heading        @tag_list = tag_list @@ -121,7 +75,7 @@ module LeapCli; module Commands        tags = @tag_list.keys.select{|tag| tag !~ /^_/}.sort # sorted list of tags, excluding _partials        table do          row(color: @colors[0]) do -          column @heading, align: 'right' +          column @heading, align: 'right', min_width: 20            column "NODES"          end          tags.each do |tag| @@ -136,7 +90,7 @@ module LeapCli; module Commands      end    end -  class NodeTable < Table +  class NodeTable < LeapCli::Util::ConsoleTable      def initialize(node_list, colors)        @node_list = node_list        @colors = colors @@ -152,7 +106,7 @@ module LeapCli; module Commands        end        table do          row(color: @colors[0]) do -          column "NODES", align: 'right' +          column "NODES", align: 'right', min_width: 20            column "SERVICES"            column "TAGS"          end | 
