Sha256: 9bef5d555528f9d16ad3927c262c44364dde3a243459c68066bffa0d58982341

Contents?: true

Size: 1.36 KB

Versions: 31

Compression:

Stored size: 1.36 KB

Contents

module HammerCLI
  module Help
    class List < AbstractItem
      LIST_INDENT = 20

      def initialize(items, options = {})
        super(options)
        @indent_size = options[:indent_size] || indent_size(items)
        build_definition(items || [])
      end

      def build_string
        out = StringIO.new
        @definition.each do |item|
          out.puts item.build_string
        end
        out.string
      end

      protected

      def build_definition(items)
        @definition = Definition.new
        items.each do |item|
          @definition << Text.new(format_item(item))
        end
        @definition
      end

      private

      def indent_size(items)
        items = normalize(items)
        max_len = items.map { |i| i[0].to_s.length }.max
        (max_len + INDENT_STEP > LIST_INDENT) ? (max_len + INDENT_STEP) : LIST_INDENT
      end

      def format_item(item)
        col1, col2, options = item
        options ||= {}
        col1 = HighLine.color(col1.to_s, :bold) if options[:bold]
        col2 = indent(col2.to_s, ' ' * @indent_size).lstrip
        padding = col1.length - HammerCLI::Output::Utils.real_length(col1)
        line = "%-#{@indent_size + padding}s%s" % [col1, col2]
        line.strip!
        line
      end

      def normalize(items)
        items.map do |i|
          i.is_a?(Array) ? i : [i]
        end
      end
    end
  end
end

Version data entries

31 entries across 31 versions & 1 rubygems

Version Path
hammer_cli-3.13.0 lib/hammer_cli/help/definition/list.rb
hammer_cli-3.12.0 lib/hammer_cli/help/definition/list.rb
hammer_cli-3.10.0 lib/hammer_cli/help/definition/list.rb
hammer_cli-3.9.0 lib/hammer_cli/help/definition/list.rb
hammer_cli-3.8.0 lib/hammer_cli/help/definition/list.rb
hammer_cli-3.7.0 lib/hammer_cli/help/definition/list.rb
hammer_cli-3.5.1 lib/hammer_cli/help/definition/list.rb
hammer_cli-3.6.0 lib/hammer_cli/help/definition/list.rb
hammer_cli-3.5.0 lib/hammer_cli/help/definition/list.rb
hammer_cli-3.4.0 lib/hammer_cli/help/definition/list.rb
hammer_cli-3.3.0 lib/hammer_cli/help/definition/list.rb
hammer_cli-3.2.0 lib/hammer_cli/help/definition/list.rb
hammer_cli-3.1.1 lib/hammer_cli/help/definition/list.rb
hammer_cli-3.0.2 lib/hammer_cli/help/definition/list.rb
hammer_cli-3.1.0 lib/hammer_cli/help/definition/list.rb
hammer_cli-3.0.1 lib/hammer_cli/help/definition/list.rb
hammer_cli-3.0.0 lib/hammer_cli/help/definition/list.rb
hammer_cli-2.5.1 lib/hammer_cli/help/definition/list.rb
hammer_cli-2.5.0 lib/hammer_cli/help/definition/list.rb
hammer_cli-2.3.1 lib/hammer_cli/help/definition/list.rb