Class: MakeMenu::TextColumn

Inherits:
Object
  • Object
show all
Defined in:
lib/make_menu/text_column.rb

Overview

A column of text with a fixed with

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(width) ⇒ TextColumn

Returns a new instance of TextColumn.

Parameters:

  • width (Integer)

    Width of the column, in characters



7
8
9
10
11
# File 'lib/make_menu/text_column.rb', line 7

def initialize(width)
  @width = width
  @rows = []
  @row_index = 0
end

Instance Attribute Details

#row_indexObject

Returns the value of attribute row_index.



15
16
17
# File 'lib/make_menu/text_column.rb', line 15

def row_index
  @row_index
end

#rowsObject

Returns the value of attribute rows.



15
16
17
# File 'lib/make_menu/text_column.rb', line 15

def rows
  @rows
end

#widthObject (readonly)

Returns the value of attribute width.



13
14
15
# File 'lib/make_menu/text_column.rb', line 13

def width
  @width
end

Instance Method Details

#add(text) ⇒ Object

Add a block of text to the column. Each row will be padded to the column width

Parameters:

  • text (String)

    The text to add, may be multi-line



19
20
21
22
23
24
# File 'lib/make_menu/text_column.rb', line 19

def add(text)
  self.rows += text.split("\n").map do |row|
    row.gsub("\r", '')
  end
  self.row_index += text.lines.size
end

#empty?Boolean

Returns True if the column is empty.

Returns:

  • (Boolean)

    True if the column is empty



39
40
41
# File 'lib/make_menu/text_column.rb', line 39

def empty?
  row_index.zero?
end

#heightInteger

Returns The number of rows in the column.

Returns:

  • (Integer)

    The number of rows in the column



34
35
36
# File 'lib/make_menu/text_column.rb', line 34

def height
  row_index
end

#row(index) ⇒ String

Return the specified row of text

Parameters:

  • index (Integer)

    The index of the row

Returns:

  • (String)

    The row at the specified index



29
30
31
# File 'lib/make_menu/text_column.rb', line 29

def row(index)
  (rows[index] || '').align(width: width)
end