0

I have gem/cli that uses highline and I was wondering if you can set your own command so it is always available (similar to 'help').

require 'rubygems'
require 'highline/import'

say("\nThis is the new mode (default)...")
choose do |menu|
  menu.prompt = "Please choose your favorite programming language?  "

  menu.choice :ruby do say("Good choice!") end
  menu.choices(:python, :perl) do say("Not from around here, are you?") end
end

say("\nThis is letter indexing...")
choose do |menu|
  menu.index        = :letter
  menu.index_suffix = ") "

  menu.prompt = "Please choose your favorite programming language?  "

  menu.choice :ruby do say("Good choice!") end
  menu.choices(:python, :perl) do say("Not from around here, are you?") end
end

say("\nThis is with a different layout...")
choose do |menu|
  menu.layout = :one_line

  menu.header = "Languages"
  menu.prompt = "Favorite?  "

  menu.choice :ruby do say("Good choice!") end
  menu.choices(:python, :perl) do say("Not from around here, are you?") end
end

thanks!

kreek
  • 8,774
  • 8
  • 44
  • 69
  • @Felix hopefully the above code makes it more clear :) If run the above, at any time you type 'help' to get the help menu. I want to make my own different commands like 'quit' or 'menu' to exit or bring up a different menu at any time. – kreek Nov 03 '13 at 21:30
  • doh, sorry I must have forgotten to hit save, it's there now – kreek Nov 04 '13 at 07:35

1 Answers1

2

I think this is only possible with rather spooky monkey-patching of the highline gem, except you want to add your commands to every choice (What is your favorite programming language? 1. ruby 2. perl 3. help 4. menu 5. quit...), which you could extract to do in a method like:

def add_custom_choices(menu)
  menu.choice(:quit) do
    say "Ok, see you."
    exit 0
  end
  menu.choice(:dostuff) do call_do_stuff_method end
end


# and later ...
choose do |menu|
  # ...
  menu.choice :ruby do say("Good choice!") end
  add_custom_choices menu
  # ....
end
Felix
  • 4,510
  • 2
  • 31
  • 46