Sha256: 7280a247bff706cf5bd05df370808bc044a6d27b912eca58b47f48d28f06c080

Contents?: true

Size: 1020 Bytes

Versions: 2

Compression:

Stored size: 1020 Bytes

Contents

# frozen_string_literal: true

module MakeMenu
  module Text
    SPACING = 2

    # A column of text with a fixed with
    class Column
      def initialize
        @width = nil
        @rows = []
        @row_index = 0
      end

      attr_accessor :rows, :row_index, :width

      # Add a block of text to the column. Each row will be padded to the column width
      def add(text)
        self.rows += text.split("\n").map do |row|
          self.width = row.decolor.size + SPACING unless width
          self.width = [width, row.decolor.size + SPACING].max
          row.gsub("\r", '')
        end
        self.row_index += text.lines.size
      end

      # @return [String] The row at the specified index
      def row(index)
        (rows[index] || '').align(width: width)
      end

      # @return [Integer] The number of rows in the column
      def height
        row_index
      end

      # @return [Boolean] True if the column is empty
      def empty?
        row_index.zero?
      end
    end
  end
end

Version data entries

2 entries across 2 versions & 1 rubygems

Version Path
make_menu-2.1.2 lib/make_menu/text/column.rb
make_menu-2.1.1 lib/make_menu/text/column.rb