Class: Menu

Inherits:
Object
  • Object
show all
Defined in:
lib/terminal-basic-menu.rb

Overview

Basic menu class used to create an instance of a terminal menu

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(output: $stdout, width: 50, header: nil, body: nil, footer: nil, border_color: 0) ⇒ Menu

Returns a new instance of Menu.



6
7
8
9
10
11
12
13
14
15
# File 'lib/terminal-basic-menu.rb', line 6

def initialize(output: $stdout, width: 50, header: nil, body: nil, footer: nil, border_color: 0)
  @output = output
  @width = width - 4 # adjust to allow '| ' and ' |' on each side
  @header = header
  @body = body
  @footer = footer
  @border_color = border_color
  @word_wrap = true
  @block_align = true
end

Instance Attribute Details

#block_alignObject

Returns the value of attribute block_align.



5
6
7
# File 'lib/terminal-basic-menu.rb', line 5

def block_align
  @block_align
end

#bodyObject

Returns the value of attribute body.



5
6
7
# File 'lib/terminal-basic-menu.rb', line 5

def body
  @body
end

#border_colorObject

Returns the value of attribute border_color.



5
6
7
# File 'lib/terminal-basic-menu.rb', line 5

def border_color
  @border_color
end

Returns the value of attribute footer.



5
6
7
# File 'lib/terminal-basic-menu.rb', line 5

def footer
  @footer
end

#headerObject

Returns the value of attribute header.



5
6
7
# File 'lib/terminal-basic-menu.rb', line 5

def header
  @header
end

#widthObject

Returns the value of attribute width.



5
6
7
# File 'lib/terminal-basic-menu.rb', line 5

def width
  @width
end

#word_wrap=(value) ⇒ Object

Sets the attribute word_wrap

Parameters:

  • value

    the value to set the attribute word_wrap to.



5
6
7
# File 'lib/terminal-basic-menu.rb', line 5

def word_wrap=(value)
  @word_wrap = value
end

Instance Method Details

#display_bodyObject



41
42
43
44
# File 'lib/terminal-basic-menu.rb', line 41

def display_body
  print_text(@body[:text], @body[:align], @body[:color])
  print_choices(@body[:choices], @body[:choice_align], @body[:color])
end


46
47
48
# File 'lib/terminal-basic-menu.rb', line 46

def display_footer
  print_text(@footer[:text], @footer[:align], @footer[:color])
end

#display_headerObject



37
38
39
# File 'lib/terminal-basic-menu.rb', line 37

def display_header
  print_text(@header[:text], @header[:align], @header[:color])
end

#display_menuObject



17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
# File 'lib/terminal-basic-menu.rb', line 17

def display_menu
  # Return if nothing to display
  return unless @header || @body || @footer
  # Change left to ljust etc.
  align_to_method!
  print_line_break
  if @header
    display_header
    print_line_break
  end
  if @body
    display_body
    print_line_break
  end
  if @footer
    display_footer
    print_line_break
  end
end