Sha256: 21057ffeeba8c6734e1b12a0691df43fd0dbf642eb2b5b85623ea91251f068c7

Contents?: true

Size: 904 Bytes

Versions: 4

Compression:

Stored size: 904 Bytes

Contents

# frozen_string_literal: true

module MakeMenu
  module Text
    # A column of text with a fixed with
    class Column
      def initialize(width)
        @width = width
        @rows = []
        @row_index = 0
      end

      attr_reader :width

      attr_accessor :rows, :row_index

      # 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|
          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

4 entries across 4 versions & 1 rubygems

Version Path
make_menu-2.1.0 lib/make_menu/text/column.rb
make_menu-2.0.0 lib/make_menu/text/column.rb
make_menu-1.1.0 lib/make_menu/text/column.rb
make_menu-1.0.0 lib/make_menu/text/column.rb