Sha256: c8f2f1af3638c3165f6c2959ec0c06b103ed5637102f20312d739ad6e13c2cb9

Contents?: true

Size: 878 Bytes

Versions: 10

Compression:

Stored size: 878 Bytes

Contents

# frozen_string_literal: true

module Nanoc::Telemetry
  class Table
    def initialize(rows)
      @rows = rows
    end

    def to_s
      columns = @rows.transpose
      column_lengths = columns.map { |c| c.map(&:size).max }

      [].tap do |lines|
        lines << row_to_s(@rows[0], column_lengths)
        lines << separator(column_lengths)
        lines.concat(@rows.drop(1).map { |r| row_to_s(r, column_lengths) })
      end.join("\n")
    end

    private

    def row_to_s(row, column_lengths)
      values = row.zip(column_lengths).map { |text, length| text.rjust(length) }
      values[0] + ' │ ' + values[1..-1].join('   ')
    end

    def separator(column_lengths)
      String.new.tap do |s|
        s << '─' * column_lengths[0]
        s << '─┼─'
        s << column_lengths[1..-1].map { |l| '─' * l }.join('───')
      end
    end
  end
end

Version data entries

10 entries across 10 versions & 1 rubygems

Version Path
nanoc-4.8.5 lib/nanoc/telemetry/table.rb
nanoc-4.8.4 lib/nanoc/telemetry/table.rb
nanoc-4.8.3 lib/nanoc/telemetry/table.rb
nanoc-4.8.2 lib/nanoc/telemetry/table.rb
nanoc-4.8.1 lib/nanoc/telemetry/table.rb
nanoc-4.8.0 lib/nanoc/telemetry/table.rb
nanoc-4.7.14 lib/nanoc/telemetry/table.rb
nanoc-4.7.13 lib/nanoc/telemetry/table.rb
nanoc-4.7.12 lib/nanoc/telemetry/table.rb
nanoc-4.7.11 lib/nanoc/telemetry/table.rb