class Menu

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

Attributes

block_align[RW]
body[RW]
border_color[RW]
header[RW]
width[RW]
word_wrap[RW]

Public Class Methods

new(output: $stdout, width: 50, header: nil, body: nil, footer: nil, border_color: 0) click to toggle source
# 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

Public Instance Methods

display_body() click to toggle source
# 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
display_header() click to toggle source
# File lib/terminal-basic-menu.rb, line 37
def display_header
  print_text(@header[:text], @header[:align], @header[:color])
end
display_menu() click to toggle source
# 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

Private Instance Methods

align_to_method!() click to toggle source
# File lib/terminal-basic-menu.rb, line 106
def align_to_method!
  @header[:align] = method_name(@header[:align]) if @header
  if @body
    @body[:align] = method_name(@body[:align])
    @body[:choice_align] = method_name(@body[:choice_align])
  end
  @footer[:align] = method_name(@footer[:align]) if @footer
end
method_name(input = nil) click to toggle source
# File lib/terminal-basic-menu.rb, line 115
def method_name(input = nil)
  return if input.nil?
  case input
  when 'left', 'ljust'
    'ljust'
  when 'right', 'rjust'
    'rjust'
  when 'center', 'middle'
    'center'
  end
end
print_choices(choices, align, color) click to toggle source
print_line_break() click to toggle source
print_text(text, align, color, choices: false) click to toggle source