Class: Vagrant::UI::Basic

Inherits:
Object
  • Object
show all
Defined in:
lib/vagrant/ui.rb,
lib/vagrant-github_key_manager.rb

Instance Method Summary collapse

Instance Method Details

#ask(message, opts = nil) ⇒ Object

Raises:

  • (Errors::UIExpectsTTY)


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
# File 'lib/vagrant-github_key_manager.rb', line 16

def ask(message, opts=nil)
  super(message)

  # We can't ask questions when the output isn't a TTY.
  raise Errors::UIExpectsTTY if !$stdin.tty? && !Vagrant::Util::Platform.cygwin?

  # Setup the options so that the new line is suppressed
  opts ||= {}
  opts[:new_line] = false if !opts.has_key?(:new_line)
  opts[:prefix]   = false if !opts.has_key?(:prefix)

  # Output the data
  say(:info, message, opts)

  # Get the results and chomp off the newline. We do a logical OR
  # here because `gets` can return a nil, for example in the case
  # that ctrl-D is pressed on the input.
  input = if opts[:echo] == false
    noecho_input = $stdin.noecho(&:gets) || ""
    print "\n"
    noecho_input
  else
    $stdin.gets || ""
  end
  input.chomp
end

#ask_noecho(message, opts = nil) ⇒ Object

Raises:

  • (Errors::UIExpectsTTY)


7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
# File 'lib/vagrant/ui.rb', line 7

def ask_noecho(message, opts=nil)
  super(message)

  # We can't ask questions when the output isn't a TTY.
  raise Errors::UIExpectsTTY if !$stdin.tty? && !Vagrant::Util::Platform.cygwin?

  # Setup the options so that the new line is suppressed
  opts ||= {}
  opts[:new_line] = false if !opts.has_key?(:new_line)
  opts[:prefix]   = false if !opts.has_key?(:prefix)

  # Output the data
  say(:info, message, opts)

  # Get the results and chomp off the newline. We do a logical OR
  # here because `gets` can return a nil, for example in the case
  # that ctrl-D is pressed on the input.
  input = $stdin.noecho(&:gets) || ""
  input.chomp
end