diff options
Diffstat (limited to 'Library/Homebrew/debrew/menu.rb')
| -rw-r--r-- | Library/Homebrew/debrew/menu.rb | 39 |
1 files changed, 39 insertions, 0 deletions
diff --git a/Library/Homebrew/debrew/menu.rb b/Library/Homebrew/debrew/menu.rb new file mode 100644 index 000000000..c59c500b8 --- /dev/null +++ b/Library/Homebrew/debrew/menu.rb @@ -0,0 +1,39 @@ +class Menu + attr_accessor :prompt + attr_accessor :entries + + def initialize + @entries = [] + end + + def choice(name, &action) + entries << { :name => name, :action => action } + end +end + +def choose + menu = Menu.new + yield menu + + choice = nil + while choice.nil? + menu.entries.each_with_index do |entry, i| + puts "#{i+1}. #{entry[:name]}" + end + print menu.prompt unless menu.prompt.nil? + reply = $stdin.gets.chomp + + i = reply.to_i + if i > 0 + choice = menu.entries[i-1] + else + possible = menu.entries.find_all {|e| e[:name].to_s.start_with? reply } + case possible.size + when 0 then puts "No such option" + when 1 then choice = possible.first + else puts "Multiple options match: #{possible.map{|e| e[:name]}.join(' ')}" + end + end + end + choice[:action].call +end |
