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
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
# File lib/terminal-basic-menu.rb, line 74 def print_choices(choices, align, color) print_text(choices, align, color, choices: true) unless choices.nil? end
print_line_break()
click to toggle source
# File lib/terminal-basic-menu.rb, line 78 def print_line_break @output.puts Rainbow('+' + '-' * (@width + 2) + '+').color(border_color) end
print_text(text, align, color, choices: false)
click to toggle source
# File lib/terminal-basic-menu.rb, line 52 def print_text(text, align, color, choices: false) color = 0 if color.nil? # Default to center align align = 'center' if align.nil? # Split for normal text not for choices lines = choices ? text.dup : text.split("\n") # Wrap words or text that is too long lines.map! { |line| word_wrap(line) } if @word_wrap lines.flatten! # Check for longest line to allow for grouped alignment if enabled max_length = @block_align ? lines.map(&:length).max : 0 # Add extra length for choices '1) ' max_length += lines.count.to_s.length + 2 if choices lines.each_with_index do |line, i| line.prepend("#{i + 1}) ") if choices output = Rainbow('| ').fg(border_color) output += Rainbow(line.ljust(max_length).send(align, @width)).fg(color) output += Rainbow(' |').fg(border_color) @output.puts output end end