Sha256: d4d50d53b8f274fdb5e0a99fd327af2959b9dac0bbb7771d15552591dee31454

Contents?: true

Size: 1.39 KB

Versions: 3

Compression:

Stored size: 1.39 KB

Contents

# encoding: utf-8

require 'verse'

module TTY
  class Table
    module Operation
      # A class which responsiblity is to align table rows and header.
      class Alignment
        DEFAULT = :left

        # Initialize an Alignment operation
        #
        # @api private
        def initialize(alignments, widths = nil)
          @alignments = alignments
          @widths     = widths
        end

        # Evaluate alignment of the provided row
        #
        # @param [Array] row
        #  the table row
        # @param [Hash] options
        #  the table options
        #
        # @return [TTY::Table::Field]
        #
        # @api public
        def call(field, row, col)
          align_field(field, col)
        end

        protected

        attr_reader :alignments

        attr_reader :widths

        # Align each field in a row
        #
        # @param [TTY::Table::Field] field
        #   the table field
        #
        # @param [Integer] co
        #   the table column index
        #
        # @param [Hash] options
        #
        # @return [TTY::Table::Field]
        #
        # @api private
        def align_field(field, col)
          column_width = widths[col]
          direction    = field.alignment || alignments[col] || DEFAULT
          Verse.align(field.content, column_width, direction)
        end
      end # Alignment
    end # Operation
  end # Table
end # TTY

Version data entries

3 entries across 3 versions & 1 rubygems

Version Path
tty-table-0.9.0 lib/tty/table/operation/alignment.rb
tty-table-0.8.0 lib/tty/table/operation/alignment.rb
tty-table-0.7.0 lib/tty/table/operation/alignment.rb