Module: Oschii::Helpers::Prompt

Included in:
Device
Defined in:
lib/oschii/helpers/prompt.rb

Instance Method Summary collapse

Instance Method Details

#prompt(text, obscure: false) ⇒ Object



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
# File 'lib/oschii/helpers/prompt.rb', line 4

def prompt(text, obscure: false)
  print "#{text}: "
  input = ''
  char = ''
  until !char.empty? && char.ord == 13
    char = STDIN.getch
    if char.ord == 127
      # BACKSPACE
      input = input[0..-2]
      print "\r#{text}: #{' ' * input.size} "
      print "\r#{text}: #{obscure ? '*' * input.size : input}"
    elsif char.ord == 27
      # ESC
      raise CancelSerialQuery
    elsif char.ord == 13
      # ENTER
    else
      input += char
      if obscure
        print '*'
      else
        print char
      end
    end
  end
  puts
  input
end