Class: Debrew::Menu Private

Inherits:
Object
  • Object
show all
Defined in:
Library/Homebrew/debrew.rb

Overview

This class is part of a private API. You should avoid using this class if possible, as it may be removed or be changed in the future.

Module for displaying a debugging menu.

Defined Under Namespace

Classes: Entry

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initializeMenu

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.

Returns a new instance of Menu.


47
48
49
# File 'Library/Homebrew/debrew.rb', line 47

def initialize
  @entries = []
end

Instance Attribute Details

#entriesObject

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.


45
46
47
# File 'Library/Homebrew/debrew.rb', line 45

def entries
  @entries
end

#promptObject

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.


45
46
47
# File 'Library/Homebrew/debrew.rb', line 45

def prompt
  @prompt
end

Class Method Details

.choose {|menu| ... } ⇒ Object

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.

Yields:

  • (menu)

55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
# File 'Library/Homebrew/debrew.rb', line 55

def self.choose
  menu = new
  yield menu

  choice = nil
  while choice.nil?
    menu.entries.each_with_index { |e, i| puts "#{i + 1}. #{e.name}" }
    print menu.prompt unless menu.prompt.nil?

    input = $stdin.gets || exit
    input.chomp!

    i = input.to_i
    if i.positive?
      choice = menu.entries[i - 1]
    else
      possible = menu.entries.select { |e| e.name.start_with?(input) }

      case possible.size
      when 0 then puts "No such option"
      when 1 then choice = possible.first
      else puts "Multiple options match: #{possible.map(&:name).join(" ")}"
      end
    end
  end

  choice[:action].call
end

Instance Method Details

#choice(name, &action) ⇒ Object

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.


51
52
53
# File 'Library/Homebrew/debrew.rb', line 51

def choice(name, &action)
  entries << Entry.new(name.to_s, action)
end