Sha256: 189bfeca7f31fc1385720b90a60948433c0fe4b8b5b0eef55c6aa1ff42275616

Contents?: true

Size: 905 Bytes

Versions: 4

Compression:

Stored size: 905 Bytes

Contents

class CliFormat::Presenter
  class Space < Base
    extend Memoist

    def text
      @buffer << format_row(@header, max_widths) if @header
      @rows.each do |row|
        @buffer << format_row(row, max_widths)
      end

      @buffer.join("\n")
    end

    def max_widths
      all_rows = [@header, *@rows].compact
      max_widths = Array.new(all_rows[0].size, 0)

      all_rows.each do |row|
        row.each_with_index do |col, idx|
          max_widths[idx] = [max_widths[idx], col.to_s.length].max
        end
      end

      max_widths
    end
    memoize :max_widths

    def format_row(row, max_widths)
      formatted_row = row.each_with_index.map do |col, idx|
        idx == row.size - 1 ? col.to_s : col.to_s.ljust(max_widths[idx])
      end

      formatted_row.join(formatted_row_separator)
    end

    # interface method
    def formatted_row_separator
      " "
    end
  end
end

Version data entries

4 entries across 4 versions & 1 rubygems

Version Path
cli-format-0.6.1 lib/cli_format/presenter/space.rb
cli-format-0.6.0 lib/cli_format/presenter/space.rb
cli-format-0.5.0 lib/cli_format/presenter/space.rb
cli-format-0.4.0 lib/cli_format/presenter/space.rb