Sha256: 981b026ea5a74db07b973a5795ef94fcd4a4cf15f29fe8b6fbb5b77cfa850f0b

Contents?: true

Size: 1.02 KB

Versions: 6

Compression:

Stored size: 1.02 KB

Contents

# frozen_string_literal: true

module MakeMenu
  # A column of text with a fixed with
  class TextColumn
    # @param [Integer] width Width of the column, in characters
    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
    # @param [String] text The text to add, may be multi-line
    def add(text)
      self.rows += text.split("\n").map do |row|
        row.gsub("\r", '')
      end
      self.row_index += text.lines.size
    end

    # Return the specified row of text
    # @param [Integer] index The index of the row
    # @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

Version data entries

6 entries across 6 versions & 1 rubygems

Version Path
make_menu-0.1.1 lib/make_menu/text_column.rb
make_menu-0.1.0 lib/make_menu/text_column.rb
make_menu-0.0.5 lib/make_menu/text_column.rb
make_menu-0.0.3 lib/make_menu/text_column.rb
make_menu-0.0.2 lib/make_menu/text_column.rb
make_menu-0.0.1 lib/make_menu/text_column.rb