Module: Bini::Prompts

Extended by:
Prompts
Included in:
Prompts
Defined in:
lib/bini/prompts.rb

Overview

A few prompts I may need in making CLI’s.

Instance Method Summary collapse

Instance Method Details

#ask(*params) ⇒ Object

Raises:

  • (RuntimeError)


5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
# File 'lib/bini/prompts.rb', line 5

def ask(*params)
  params = params[0] if params[0]
  raise RuntimeError, 'No interactive terminal present.' unless $stdin.tty?
  if params[:prompt]
    $stderr.print params[:prompt]
    $stderr.flush
  end
  if params[:password]
    raise RuntimeError 'Could not disable echo to ask for password security' unless system 'stty -echo -icanon'
  end
  output = $stdin.gets
  output.chomp! if output
  #unmuck the prompt.
  `stty echo icanon`
  output
end