Sha256: acd082bf9c0aa22a22337999595e7cfb91f7bd26ddb3b3f2cda8216f419e74eb

Contents?: true

Size: 1.27 KB

Versions: 7

Compression:

Stored size: 1.27 KB

Contents

# frozen_string_literal: true

require_relative 'indent'

module CommandKit
  module Printing
    #
    # Supports printing aligned key/value fields.
    #
    # @since 0.4.0
    #
    module Fields
      include Indent

      #
      # Prints a Hash as left-justified `:` separated fields.
      #
      # @param [Hash, Array<(Object, Object)>] fields
      #   The fields to print.
      #
      # @example
      #   print_fields('Name' => 'foo', 'Version' => '0.1.0')
      #   # Name:    foo
      #   # Version: 0.1.0
      #
      # @api public
      #
      def print_fields(fields)
        max_length = 0

        fields = fields.map { |name,value|
          name       = name.to_s
          value      = value.to_s
          max_length = name.length if name.length > max_length

          [name, value]
        }

        fields.each do |name,value|
          first_line, *rest = value.to_s.lines(chomp: true)

          # print the first line with the header
          header = "#{name}:".ljust(max_length + 1)
          puts "#{header} #{first_line}"

          # indent and print the rest of the lines
          indent(max_length + 2) do
            rest.each do |line|
              puts line
            end
          end
        end

        return nil
      end
    end
  end
end

Version data entries

7 entries across 7 versions & 1 rubygems

Version Path
command_kit-0.6.0 lib/command_kit/printing/fields.rb
command_kit-0.5.6 lib/command_kit/printing/fields.rb
command_kit-0.5.5 lib/command_kit/printing/fields.rb
command_kit-0.5.4 lib/command_kit/printing/fields.rb
command_kit-0.5.3 lib/command_kit/printing/fields.rb
command_kit-0.5.2 lib/command_kit/printing/fields.rb
command_kit-0.5.1 lib/command_kit/printing/fields.rb