Module: MakeMenu::Console::Prompter

Defined in:
lib/make_menu/console/prompter.rb

Constant Summary collapse

PressedEscape =
Class.new(StandardError)

Class Method Summary collapse

Class Method Details

.prompt(text = '', obscure: false) ⇒ Object



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
35
36
37
38
39
40
41
42
43
44
# File 'lib/make_menu/console/prompter.rb', line 10

def self.prompt(text = '', obscure: false)
  print text

  input = ''
  char = ''

  until !char.empty? && char.ord == 13
    char = $stdin.getch

    case char.ord
    when 127
      # BACKSPACE
      input = input[0..-2]
      print "\r#{text}#{' ' * input.size} "
      print "\r#{text}#{obscure ? '*' * input.size : input}"

    when 27
      # ESC
      raise PressedEscape

    when 13
      # ENTER

    else
      input += char
      if obscure
        print '*'
      else
        print char
      end
    end
  end

  input
end