Class: Menu

Inherits:
Object
  • Object
show all
Includes:
Prompt
Defined in:
lib/bunch/url_generator.rb

Overview

Collection of menu items

Instance Attribute Summary collapse

Instance Method Summary collapse

Methods included from Prompt

#choose_number, #get_line, #get_text, #url_encode_text

Constructor Details

#initialize(items) ⇒ Menu

Returns a new instance of Menu.



106
107
108
# File 'lib/bunch/url_generator.rb', line 106

def initialize(items)
  @items = items
end

Instance Attribute Details

#itemsObject

Returns the value of attribute items.



104
105
106
# File 'lib/bunch/url_generator.rb', line 104

def items
  @items
end

Instance Method Details

#choose(query = 'Select an item') ⇒ Object



110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
# File 'lib/bunch/url_generator.rb', line 110

def choose(query = 'Select an item')
  throw 'No items initialized' if @items.nil?
  STDERR.puts
  STDERR.puts "#{("" * 74)}"
  intpad = Math::log10(@items.length).to_i + 1
  @items.each_with_index do |item, idx|
    idxstr = "%#{intpad}d" % (idx + 1)
    line = "#{idxstr}: #{item.title}"
    pad = 74 - line.length
    STDERR.puts "#{line}#{" " * pad}"
  end
  STDERR.puts "└┤ #{query}#{"" * (70 - query.length)}"
  sel = choose_number("> ", @items.length)
  sel ? @items[sel.to_i - 1] : nil
end