Sha256: fe146e56e30a9dbb38638f2f57fba9376756beb7afb85e71cb9b543eb3df90a3

Contents?: true

Size: 895 Bytes

Versions: 3

Compression:

Stored size: 895 Bytes

Contents

class Cl
  class Help
    class Table
      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 { |cell, ix| cell.to_s.ljust(widths[ix]) }
      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|
          width = col.compact.max_by(&:size)&.size
          ix < cols.size - 2 ? width.to_i : width.to_i + padding.to_i
        end
      end

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

Version data entries

3 entries across 3 versions & 1 rubygems

Version Path
cl-0.1.2 lib/cl/help/table.rb
cl-0.1.1 lib/cl/help/table.rb
cl-0.1.0 lib/cl/help/table.rb