Sha256: 229946ba9fd8c472f884539e8f52ff17abb64457f2cc06a344a7ff938b894645

Contents?: true

Size: 1.82 KB

Versions: 5

Compression:

Stored size: 1.82 KB

Contents

module Helium
  class Console
    define_formatter_for Array do
      def call
        return "[]" if object.none?
        return format_inline_with_truncation if force_inline?

        format_inline_with_no_truncation || format_as_table
      end

      def is_simple
        object.none?
      end

    private

      def format_as_table
        table = Table.new(runner: '  ', format_keys: false)
        object.each.with_index do |element, index|
          table.row(light_black("[#{index}]:"), element)
        end

        [
          "[",
          format(table),
          "]"
        ].join($/)
      end

      def format_inline_with_truncation
        joined = nil
        trunc = nil
        total = object.length

        object.each.with_index do |element, index|
          formatted = format_nested(element, max_lines: 1, nesting: 1, max_width: 15)

          new_joined = [joined, formatted].compact.join(" | ")
          new_trunc = (" | (#{total - index - 1} #{index.zero? ? 'elements' : 'more'})" unless index == total - 1)

          break if new_joined.length > max_width - (new_trunc&.length || 0) - 4

          joined = new_joined
          trunc = new_trunc
        end

        if joined
          joined = [" ", joined, trunc, " "].compact.join if joined
          ["[", joined, "]"].compact.join
        else
          "[...(#{object.length})]"
        end
      end

      def format_inline_with_no_truncation
        joined = nil

        object.each do |element|
          return unless simple?(element)

          formatted = format_nested(element)
          joined = [joined, formatted].compact.join(" | ")

          return if joined.length > max_width - 4
        end
        joined = " #{joined} " if joined
        ["[", joined, "]"].compact.join
      end

      def force_inline?
        level > 2
      end
    end
  end
end

Version data entries

5 entries across 5 versions & 1 rubygems

Version Path
helium-console-0.1.7 lib/helium/console/registry/array.rb
helium-console-0.1.6 lib/helium/console/registry/array.rb
helium-console-0.1.5 lib/helium/console/registry/array.rb
helium-console-0.1.4 lib/helium/console/registry/array.rb
helium-console-0.1.3 lib/helium/console/registry/array.rb