aboutsummaryrefslogtreecommitdiff
path: root/lib/leap_cli/commands/util.rb
blob: b5a102fd58c933acbb9b653e9bbc48f0cc07988b (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
module LeapCli
  module Commands
    extend self
    extend LeapCli::Util

    #
    # keeps prompting the user for a numbered choice, until they pick a good one or bail out.
    #
    # block is yielded and is responsible for rendering the choices.
    #
    def numbered_choice_menu(msg, items, &block)
      while true
        say("\n" + msg + ':')
        items.each_with_index &block
        say("q.  quit")
        index = ask("number 1-#{items.length}> ")
        if index.empty?
          next
        elsif index =~ /q/
          bail!
        else
          i = index.to_i - 1
          if i < 0 || i >= items.length
            bail!
          else
            return i
          end
        end
      end
    end


  end
end