Skip to content

anthonymr/auto-menu

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

13 Commits
 
 
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

AutoMenu - CLI recursive menu generator

Built with ruby Ruby version 3.1.1

AutoMenu is a CLI recursive menu generator built with Ruby.

Main features

  1. Print the menu and wait for the user to select an option:
    1
  2. Add recursive multi-level sub-menus and navigate between them:
    2
  3. Get user selected option:
    3

Usage

  1. Include auto_menu.rb file
  require_relative 'auto_menu'
  1. Define the menu using an array of hash:
    • You can add as many levels as you want.
    • Each hash can have three keys:
      • key: [required] option displayed in the menu
      • value: [required] value returned when selected
      • title: [optional] the menu/sub-menu name, only valid in the first element
  hash_menu = [
    { key: 'Option 1', title: 'Main menu', value: [
      { key: 'Option 1.1', title: 'Sub-menu 1', value: 'option_1_1' },
      { key: 'Option 1.2', value: [
        { key: 'Option 1.2.1', title: 'Sub-menu 1.2', value: 'option_1_2_1' },
        { key: 'Option 1.2.2', value: 'option_1_2_2' },
        { key: 'Option 1.2.3', value: 'option_1_2_3' }
      ] }
    ] },
    { key: 'Option 2' }, # This option will be ignored
    { value: 'option_3' }, # This option will be ignored
    { key: 'Option 4', value: 'option_4' },
    { key: 'Option 5', value: 'option_5' },
    { key: 'Option 6', value: 'option_6' },
    { key: 'Option 7', value: 'option_7' },
    { key: 'Option 8', value: 'option_8' }
  ]
  1. Define configurations hash [optional]:
  configurations = {
    clear_screen: true, # Default: true
    display_title: true, # Default: true
    display_title_history: true, # Default: true
    display_app_name: true, # Default: true
    app_name: 'Wellcome to My App' # Default: 'Wellcome!'
  }
  1. Put it all together using an AutoMenu object:
  menu = AutoMenu.new(hash_menu, configurations)

  loop do
    selection = menu.ask #AutoMenu.ask: print menu  and return user selected option

    puts "You selected: #{selection}"

    menu.pause #AutoMenu.pause([message]): pause program excecution unitl user press enter

    break if menu.exit? #AutoMenu.exit?: evaluate if the user selected 'exit' option
  end

About

Library to generate interactive command line interface menus built and for Ruby using recursion.

Resources

License

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published

Languages