Class: Debrew::Menu Private

Inherits:
Object show all
Defined in:
debrew.rb

Overview

This class is part of a private API. This class may only be used in the Homebrew/brew repository. Third parties should avoid using this class if possible, as it may be removed or changed without warning.

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

#initializevoid

This method is part of a private API. This method may only be used in the Homebrew/brew repository. Third parties should avoid using this method if possible, as it may be removed or changed without warning.



43
44
45
# File 'debrew.rb', line 43

def initialize
  @entries = T.let([], T::Array[Entry])
end

Instance Attribute Details

#entriesArray<Entry>

This method is part of a private API. This method may only be used in the Homebrew/brew repository. Third parties should avoid using this method if possible, as it may be removed or changed without warning.

Returns:



40
41
42
# File 'debrew.rb', line 40

def entries
  @entries
end

#promptString?

This method is part of a private API. This method may only be used in the Homebrew/brew repository. Third parties should avoid using this method if possible, as it may be removed or changed without warning.

Returns:



37
38
39
# File 'debrew.rb', line 37

def prompt
  @prompt
end

Class Method Details

.choose(&_block) {|menu| ... } ⇒ void

This method is part of a private API. This method may only be used in the Homebrew/brew repository. Third parties should avoid using this method if possible, as it may be removed or changed without warning.

This method returns an undefined value.

Parameters:

  • _block (T.proc.params(menu: Menu).void)

Yields:

  • (menu)


53
54
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
# File 'debrew.rb', line 53

def self.choose(&_block)
  menu = new
  yield menu

  choice = T.let(nil, T.nilable(Entry))
  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) ⇒ void

This method is part of a private API. This method may only be used in the Homebrew/brew repository. Third parties should avoid using this method if possible, as it may be removed or changed without warning.

This method returns an undefined value.

Parameters:

  • name (Symbol)
  • action (T.proc.void)


48
49
50
# File 'debrew.rb', line 48

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