Sha256: c4a8e466de01a7393eb1ea0f288fdad24fd8497234a87e7294ac2c2999e82e15

Contents?: true

Size: 1.17 KB

Versions: 17

Compression:

Stored size: 1.17 KB

Contents

class Cl
  class Help
    class Table
      include Wrap

      attr_reader :data, :padding

      def initialize(data)
        @data = data
      end

      def any?
        data.any?
      end

      def format(padding = 8)
        @padding = padding
        rows.join("\n")
      end
      alias to_s format

      def rows
        data.map { |row| cells(row).join(' ').rstrip }
      end

      def cells(row)
        row.map.with_index do |cell, ix|
          indent(wrap(cell.to_s), widths[ix - 1]).ljust(widths[ix])
        end
      end

      def indent(str, width)
        return str if str.empty? || !width
        [str.lines[0], *str.lines[1..-1].map { |str| ' ' * (width + 1) + str }].join.rstrip
      end

      def width
        widths = cols[0..-2].map { |col| col.max_by(&:size).size }.inject(&:+).to_i
        widths + cols.size - 1
      end

      def widths
        cols.map.with_index do |col, ix|
          max = col.compact.max_by(&:size)
          pad(max ? max.size : 0, ix)
        end
      end

      def pad(width, ix)
        ix < cols.size - 2 ? width : width + padding.to_i
      end

      def cols
        @cols ||= data.transpose
      end
    end
  end
end

Version data entries

17 entries across 17 versions & 2 rubygems

Version Path
travis-cl-1.2.4 lib/cl/help/table.rb
cl-1.2.4 lib/cl/help/table.rb
cl-1.2.3 lib/cl/help/table.rb
cl-1.2.2 lib/cl/help/table.rb
cl-1.2.1 lib/cl/help/table.rb
cl-1.2.0 lib/cl/help/table.rb
cl-1.1.5 lib/cl/help/table.rb
cl-1.1.4 lib/cl/help/table.rb
cl-1.1.3 lib/cl/help/table.rb
cl-1.1.2 lib/cl/help/table.rb
cl-1.1.1 lib/cl/help/table.rb
cl-1.1.0 lib/cl/help/table.rb
cl-1.0.5 lib/cl/help/table.rb
cl-1.0.4 lib/cl/help/table.rb
cl-1.0.3 lib/cl/help/table.rb
cl-1.0.2 lib/cl/help/table.rb
cl-1.0.1 lib/cl/help/table.rb